Skip to content

Commit

Permalink
push now
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 19, 2023
1 parent 2b4bb16 commit e1af418
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -555,8 +556,6 @@ parameters:

- '#class\-string<Rector\\Core\\Contract\\Rector\\CollectorRectorInterface\|Rector\\Core\\Contract\\Rector\\RectorInterface#'

- '#Parameter \#1 \$node \(PhpParser\\Node\\Stmt\\Class_\) of method Rector\\(.*?)Collector\:\:processNode\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method PHPStan\\Collectors\\Collector<PhpParser\\Node,mixed>\:\:processNode\(\)#'

- '#Access to an undefined property (.*?)\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
- '#Property Rector\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts \(array<PhpParser\\Node\\Stmt>\|null\) does not accept array<PhpParser\\Node\\Stmt\|null>#'
- '#Class "Rector\\Utils\\PHPStan\\Rule\\LongAndDependentComplexRectorRule" is missing @see annotation with test case class reference#'
24 changes: 12 additions & 12 deletions utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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_) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e1af418

Please sign in to comment.