Skip to content

Commit

Permalink
Added Rule error identifiers (#10)
Browse files Browse the repository at this point in the history
* Added Rule error identifiers

* Declare nikic/php-parser v4 dependency

* Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated.

Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.
  • Loading branch information
staabm authored Dec 12, 2024
1 parent 0362504 commit 1c9472c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"require": {
"php": "^8.2",
"phpstan/phpstan": "^1.10.50"
"phpstan/phpstan": "^1.10.50",
"nikic/php-parser": "^4"
},
"require-dev": {
"phpstan/extension-installer": "^1.3",
Expand Down
26 changes: 12 additions & 14 deletions src/Rules/ClassDependencyTreeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeWithClassName;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\ClassReflectionParser;
Expand Down Expand Up @@ -89,13 +90,13 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [
sprintf(
self::ERROR_MESSAGE,
$totaDependencyTreeComplexity,
$this->configuration->getMaxDependencyTreeComplexity()
),
];
$message = sprintf(
self::ERROR_MESSAGE,
$totaDependencyTreeComplexity,
$this->configuration->getMaxDependencyTreeComplexity()
);

return [RuleErrorBuilder::message($message)->identifier('complexity.dependencyTree')->build()];
}

private function isTypeToAnalyse(ClassReflection $classReflection): bool
Expand All @@ -112,15 +113,12 @@ private function isTypeToAnalyse(ClassReflection $classReflection): bool
private function resolveParameterTypeClass(ParameterReflection $parameterReflection): ?Class_
{
$parameterType = $parameterReflection->getType();
if (! $parameterType instanceof TypeWithClassName) {
return null;
}

$parameterClassReflection = $parameterType->getClassReflection();
if (! $parameterClassReflection instanceof ClassReflection) {
$classReflections = $parameterType->getObjectClassReflections();
// XXX add support for union types
if (count($classReflections) !== 1) {
return null;
}

return $this->classReflectionParser->parse($parameterClassReflection);
return $this->classReflectionParser->parse($classReflections[0]);
}
}
4 changes: 2 additions & 2 deletions src/Rules/ClassLikeCognitiveComplexityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\Configuration;

Expand Down Expand Up @@ -39,7 +40,6 @@ public function getNodeType(): string

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -59,6 +59,6 @@ public function processNode(Node $node, Scope $scope): array
$this->configuration->getMaxClassCognitiveComplexity()
);

return [$message];
return [RuleErrorBuilder::message($message)->identifier('complexity.classLike')->build()];
}
}
4 changes: 2 additions & 2 deletions src/Rules/FunctionLikeCognitiveComplexityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\Configuration;
use TomasVotruba\CognitiveComplexity\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -53,7 +54,6 @@ public function getNodeType(): string

/**
* @param FunctionLike $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -75,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
$this->configuration->getMaxFunctionCognitiveComplexity()
);

return [$message];
return [RuleErrorBuilder::message($message)->identifier('complexity.functionLike')->build()];
}

private function resolveFunctionName(FunctionLike $functionLike, Scope $scope): string
Expand Down

0 comments on commit 1c9472c

Please sign in to comment.