diff --git a/phpstan.neon b/phpstan.neon index 91cb03a4755..bd692e25e0b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,7 +2,8 @@ includes: - vendor/symplify/phpstan-rules/config/symplify-rules.neon rules: - - Rector\Utils\PHPStan\Rule\LongAndDependentComplexRectorRule + # @todo this rule is costly and designed for hand-on refactoring; enable only when needed + # - Rector\Utils\PHPStan\Rule\LongAndDependentComplexRectorRule parameters: reportUnmatchedIgnoredErrors: false @@ -555,8 +556,6 @@ parameters: - '#class\-string\:\:processNode\(\)#' - - '#Access to an undefined property (.*?)\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#' - '#Property Rector\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts \(array\|null\) does not accept array#' - '#Class "Rector\\Utils\\PHPStan\\Rule\\LongAndDependentComplexRectorRule" is missing @see annotation with test case class reference#' diff --git a/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php b/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php index 95b49164f22..15ac1ec22e9 100644 --- a/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php +++ b/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php @@ -13,7 +13,6 @@ use PHPStan\Analyser\Scope; use PHPStan\Node\InClassNode; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Rules\Rule; use PHPStan\Type\TypeWithClassName; @@ -31,12 +30,12 @@ final class LongAndDependentComplexRectorRule implements Rule */ private const ALLOWED_TRANSITIONAL_COMPLEXITY = 120; - private Parser $phpParser; + private readonly Parser $phpParser; - private NodeFinder $nodeFinder; + private readonly NodeFinder $nodeFinder; public function __construct( - private AstCognitiveComplexityAnalyzer $astCognitiveComplexityAnalyzer, + private readonly AstCognitiveComplexityAnalyzer $astCognitiveComplexityAnalyzer, ) { $parserFactory = new ParserFactory(); $this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7); @@ -65,8 +64,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - $constructorMethodReflection = $classReflection->getConstructor(); - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($constructorMethodReflection->getVariants()); + $extendedMethodReflection = $classReflection->getConstructor(); + $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle( + $extendedMethodReflection->getVariants() + ); $originalClassLike = $node->getOriginalNode(); if (! $originalClassLike instanceof Class_) { @@ -76,9 +77,8 @@ public function processNode(Node $node, Scope $scope): array $currentClassLikeComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($originalClassLike); $totalTransitionalComplexity = $currentClassLikeComplexity; - foreach ($parametersAcceptor->getParameters() as $parameterReflection) { - /** @var ParameterReflection $parameterReflection */ - $parameterType = $parameterReflection->getType(); + foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) { + $parameterType = $parameterReflectionWithPhpDoc->getType(); if (! $parameterType instanceof TypeWithClassName) { continue; } @@ -122,11 +122,11 @@ private function parseClassReflectionToClassNode(ClassReflection $classReflectio return null; } - $dependencyClass = $this->nodeFinder->findFirstInstanceOf($stmts, Class_::class); - if (! $dependencyClass instanceof Class_) { + $node = $this->nodeFinder->findFirstInstanceOf($stmts, Class_::class); + if (! $node instanceof Class_) { return null; } - return $dependencyClass; + return $node; } }