Skip to content

Commit

Permalink
make LongAndDependentComplexRectorRule work with complex transitional…
Browse files Browse the repository at this point in the history
… construcotr dependenciesů

gs
  • Loading branch information
TomasVotruba committed Sep 19, 2023
1 parent f8e1a4b commit a3765f4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 33 deletions.
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ parameters:
message: '#Parameter \#1 \$useType of callable callable\(0\|1\|2\|3, PhpParser\\Node\\Stmt\\UseUse, string\): void expects 0\|1\|2\|3, int given.#'
path: rules/CodingStyle/ClassNameImport/UseImportsTraverser.php

<<<<<<< HEAD
# rector collectors
- '#Creating new PHPStan\\Collectors\\CollectedData is not covered by backward compatibility promise\. The class might change in a minor PHPStan version#'

Expand All @@ -560,6 +559,4 @@ parameters:

- '#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#'
>>>>>>> e4bb45f52e ([Internal] Experiment with internal PHPStan rule to spot heavy Rector rules)
111 changes: 81 additions & 30 deletions utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

namespace Rector\Utils\PHPStan\Rule;

use Nette\Utils\FileSystem;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeFinder;
use PhpParser\Parser;
use PhpParser\ParserFactory;
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;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\ValueObject\MethodName;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;

/**
* @implements Rule<InClassNode>
Expand All @@ -22,12 +29,20 @@ final class LongAndDependentComplexRectorRule implements Rule
/**
* @var int
*/
private const MAX_CLASS_LINES = 320;
private const ALLOWED_TRANSITIONAL_COMPLEXITY = 120;

/**
* @var int
*/
private const MAX_DEPENDENCY_COUNT = 7;
private Parser $phpParser;

private NodeFinder $nodeFinder;

public function __construct(
private AstCognitiveComplexityAnalyzer $astCognitiveComplexityAnalyzer,
) {
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7);

$this->nodeFinder = new NodeFinder();
}

public function getNodeType(): string
{
Expand All @@ -45,37 +60,73 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$class = $node->getOriginalNode();
$errorMessages = [];
// not much complex
if (! $classReflection->hasConstructor()) {
return [];
}

$constructorMethodReflection = $classReflection->getConstructor();
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($constructorMethodReflection->getVariants());

$constructorParameterCount = $this->resolveConstructorParameterCount($class);
if ($constructorParameterCount > self::MAX_DEPENDENCY_COUNT) {
$errorMessages[] = sprintf(
'Class "%s" has too many constructor parameters (%d), consider using value objects',
$classReflection->getName(),
$constructorParameterCount
);
$originalClassLike = $node->getOriginalNode();
if (! $originalClassLike instanceof Class_) {
return [];
}

$classLineCount = $class->getEndLine() - $class->getStartLine();
if ($classLineCount > self::MAX_CLASS_LINES) {
$errorMessages[] = sprintf(
'Class "%s" is too long (%d lines), consider splitting it to smaller classes',
$classReflection->getName(),
$classLineCount
);
$currentClassLikeComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($originalClassLike);
$totalTransitionalComplexity = $currentClassLikeComplexity;

foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
/** @var ParameterReflection $parameterReflection */
$parameterType = $parameterReflection->getType();
if (! $parameterType instanceof TypeWithClassName) {
continue;
}

$parameterClassReflection = $parameterType->getClassReflection();
if (! $parameterClassReflection instanceof ClassReflection) {
continue;
}

$dependencyClass = $this->parseClassReflectionToClassNode($parameterClassReflection);
if (! $dependencyClass instanceof Class_) {
continue;
}

$dependencyComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($dependencyClass);
$totalTransitionalComplexity += $dependencyComplexity;
}

if ($totalTransitionalComplexity < self::ALLOWED_TRANSITIONAL_COMPLEXITY) {
return [];
}

return $errorMessages;
return [sprintf(
'Transitional dependency complexity %d is over %d, please consider splitting it up.',
$totalTransitionalComplexity,
self::ALLOWED_TRANSITIONAL_COMPLEXITY
)];
}

private function resolveConstructorParameterCount(ClassLike $classLike): int
private function parseClassReflectionToClassNode(ClassReflection $classReflection): ?Class_
{
$constructorClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
if (! $constructorClassMethod instanceof ClassMethod) {
return 0;
$fileName = $classReflection->getFileName();
if (! is_string($fileName)) {
return null;
}

$fileContents = FileSystem::read($fileName);

$stmts = $this->phpParser->parse($fileContents);
if ($stmts === null) {
return null;
}

$dependencyClass = $this->nodeFinder->findFirstInstanceOf($stmts, Class_::class);
if (! $dependencyClass instanceof Class_) {
return null;
}

return count($constructorClassMethod->getParams());
return $dependencyClass;
}
}

0 comments on commit a3765f4

Please sign in to comment.