-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6de2d13
commit 0362504
Showing
21 changed files
with
387 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,11 @@ jobs: | |
matrix: | ||
php: | ||
- 7.2 | ||
- 7.4 | ||
- 8.0 | ||
- 8.1 | ||
- 8.2 | ||
- 8.3 | ||
|
||
name: "Install on PHP ${{ matrix.php }}" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TomasVotruba\CognitiveComplexity; | ||
|
||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\NodeFinder; | ||
use PhpParser\Parser; | ||
use PhpParser\ParserFactory; | ||
use PHPStan\Reflection\ClassReflection; | ||
|
||
final readonly class ClassReflectionParser | ||
{ | ||
private Parser $phpParser; | ||
|
||
private NodeFinder $nodeFinder; | ||
|
||
public function __construct() | ||
{ | ||
$parserFactory = new ParserFactory(); | ||
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7); | ||
|
||
$this->nodeFinder = new NodeFinder(); | ||
} | ||
|
||
public function parse(ClassReflection $classReflection): ?Class_ | ||
{ | ||
$fileName = $classReflection->getFileName(); | ||
if (! is_string($fileName)) { | ||
return null; | ||
} | ||
|
||
/** @var string $fileContents */ | ||
$fileContents = file_get_contents($fileName); | ||
|
||
$stmts = $this->phpParser->parse($fileContents); | ||
if ($stmts === null) { | ||
return null; | ||
} | ||
|
||
$foundClass = $this->nodeFinder->findFirstInstanceOf($stmts, Class_::class); | ||
if (! $foundClass instanceof Class_) { | ||
return null; | ||
} | ||
|
||
return $foundClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TomasVotruba\CognitiveComplexity\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
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 TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer; | ||
use TomasVotruba\CognitiveComplexity\ClassReflectionParser; | ||
use TomasVotruba\CognitiveComplexity\Configuration; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
* | ||
* Find classes with complex constructor dependency tree = current class complexity + complexity of all __construct() dependencies. | ||
*/ | ||
final readonly class ClassDependencyTreeRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Dependency tree complexity %d is over %d. Refactor __construct() dependencies or split up.'; | ||
|
||
public function __construct( | ||
private AstCognitiveComplexityAnalyzer $astCognitiveComplexityAnalyzer, | ||
private ClassReflectionParser $classReflectionParser, | ||
private Configuration $configuration | ||
) { | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
/** | ||
* @param InClassNode $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $this->configuration->isDependencyTreeEnabled()) { | ||
return []; | ||
} | ||
|
||
$classReflection = $node->getClassReflection(); | ||
|
||
// nothing to check | ||
if (! $classReflection->hasConstructor()) { | ||
return []; | ||
} | ||
|
||
// only check | ||
$originalClassLike = $node->getOriginalNode(); | ||
if (! $originalClassLike instanceof Class_) { | ||
return []; | ||
} | ||
|
||
if (! $this->isTypeToAnalyse($classReflection)) { | ||
return []; | ||
} | ||
|
||
$extendedMethodReflection = $classReflection->getConstructor(); | ||
|
||
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle( | ||
$extendedMethodReflection->getVariants() | ||
); | ||
|
||
$totaDependencyTreeComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($originalClassLike); | ||
|
||
foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) { | ||
$dependencyClass = $this->resolveParameterTypeClass($parameterReflectionWithPhpDoc); | ||
if (! $dependencyClass instanceof Class_) { | ||
continue; | ||
} | ||
|
||
$dependencyComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($dependencyClass); | ||
$totaDependencyTreeComplexity += $dependencyComplexity; | ||
} | ||
|
||
if ($totaDependencyTreeComplexity <= $this->configuration->getMaxDependencyTreeComplexity()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
sprintf( | ||
self::ERROR_MESSAGE, | ||
$totaDependencyTreeComplexity, | ||
$this->configuration->getMaxDependencyTreeComplexity() | ||
), | ||
]; | ||
} | ||
|
||
private function isTypeToAnalyse(ClassReflection $classReflection): bool | ||
{ | ||
foreach ($this->configuration->getDependencyTreeTypes() as $dependencyTreeType) { | ||
if ($classReflection->isSubclassOf($dependencyTreeType)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function resolveParameterTypeClass(ParameterReflection $parameterReflection): ?Class_ | ||
{ | ||
$parameterType = $parameterReflection->getType(); | ||
if (! $parameterType instanceof TypeWithClassName) { | ||
return null; | ||
} | ||
|
||
$parameterClassReflection = $parameterType->getClassReflection(); | ||
if (! $parameterClassReflection instanceof ClassReflection) { | ||
return null; | ||
} | ||
|
||
return $this->classReflectionParser->parse($parameterClassReflection); | ||
} | ||
} |
Oops, something went wrong.