-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal PHPStan rule - class must be abstract or final
- Loading branch information
1 parent
5baa146
commit d631120
Showing
2 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Build; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Reflection\FunctionVariant; | ||
use PHPStan\Reflection\FunctionVariantWithPhpDocs; | ||
use PHPStan\Reflection\Php\DummyParameter; | ||
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Type; | ||
use function in_array; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
final class FinalClassRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
if (!$classReflection->isClass()) { | ||
return []; | ||
} | ||
if ($classReflection->isAbstract()) { | ||
return []; | ||
} | ||
if ($classReflection->isFinal()) { | ||
return []; | ||
} | ||
if ($classReflection->isSubclassOf(Type::class)) { | ||
return []; | ||
} | ||
|
||
// exceptions | ||
if (in_array($classReflection->getName(), [ | ||
FunctionVariant::class, | ||
FunctionVariantWithPhpDocs::class, | ||
DummyParameter::class, | ||
PhpFunctionFromParserNodeReflection::class, | ||
], true)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('Class %s must be abstract or final.', $classReflection->getDisplayName()), | ||
) | ||
->identifier('phpstan.finalClass') | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
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