-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement require-extends rules #2859
Merged
Merged
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7c9fbe3
Implement IncompatibleRequireExtendsTypeRule
staabm ff66719
Implement RequireExtendsRule
staabm 65dde9f
report final classes
staabm c6f272f
php <8.1 compat
staabm b535c9b
use ClassLike instead of InClassNode for phpdoc validation
staabm 06d3343
implement require-implements rules
staabm a901c62
Update config.level2.neon
staabm fee7d1a
fix php 7.2 compat
staabm c44d540
fix name collision
staabm e6bcbe7
Rules that just have the tag should be in the rules section above.
staabm 2295b93
Utilize ClassReflection->implementsInterface/isSubclassOf
staabm 8d3d9d3
Use instanceof ObjectType
staabm 619790d
cover anonymous classes in tests
staabm 682c4a0
Work on InClassNode
staabm bb3ee3e
Separate InClassNode and InTraitNode rules
staabm 7f7334e
Utilize ObjectType->getClassReflection()
staabm 1374549
fix php 7.x expectations
staabm a1ed5d8
error on trait phpdocs only once per declaration
staabm 131c5ca
Rename rule classes
ondrejmirtes df9cf28
Cosmetic change
ondrejmirtes fc125fe
Use Trait_
ondrejmirtes fdab25b
Improve error messages
ondrejmirtes ec3fc94
Support psalm prefix
staabm ea57e39
Refactoring: extract RequireExtendsCheck
staabm f7bda5c
use getDisplayName()
staabm e31c497
require-extends can only be used once
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class RequireExtendsRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
|
||
$errors = []; | ||
foreach ($classReflection->getImmediateInterfaces() as $interface) { | ||
$extendsTags = $interface->getRequireExtendsTags(); | ||
foreach ($extendsTags as $extendsTag) { | ||
$type = $extendsTag->getType(); | ||
if (!$type instanceof ObjectType) { | ||
continue; | ||
} | ||
|
||
if ($classReflection->isSubclassOf($type->getClassName())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
'Interface %s requires implementing class to extend %s, but %s does not.', | ||
$interface->getName(), | ||
$type->describe(VerbosityLevel::typeOnly()), | ||
$classReflection->getName(), | ||
), | ||
)->build(); | ||
} | ||
} | ||
|
||
foreach ($classReflection->getTraits() as $trait) { | ||
$extendsTags = $trait->getRequireExtendsTags(); | ||
foreach ($extendsTags as $extendsTag) { | ||
$type = $extendsTag->getType(); | ||
if (!$type instanceof ObjectType) { | ||
continue; | ||
} | ||
|
||
if ($classReflection->isSubclassOf($type->getClassName())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
'Trait %s requires using class to extend %s, but %s does not.', | ||
$trait->getName(), | ||
$type->describe(VerbosityLevel::typeOnly()), | ||
$classReflection->getName(), | ||
), | ||
)->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class RequireImplementsRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
|
||
$errors = []; | ||
foreach ($classReflection->getTraits() as $trait) { | ||
$implementsTags = $trait->getRequireImplementsTags(); | ||
foreach ($implementsTags as $implementsTag) { | ||
$type = $implementsTag->getType(); | ||
if (!$type instanceof ObjectType) { | ||
continue; | ||
} | ||
|
||
if ($classReflection->implementsInterface($type->getClassName())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
'Trait %s requires using class to implement %s, but %s does not.', | ||
$trait->getName(), | ||
$type->describe(VerbosityLevel::typeOnly()), | ||
$classReflection->getName(), | ||
), | ||
)->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PhpDoc; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\PhpDoc\Tag\RequireExtendsTag; | ||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\ClassNameNodePair; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function array_merge; | ||
use function sprintf; | ||
|
||
final class RequireExtendsCheck | ||
{ | ||
|
||
public function __construct( | ||
private ClassCaseSensitivityCheck $classCaseSensitivityCheck, | ||
private bool $checkClassCaseSensitivity, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<RequireExtendsTag> $extendsTags | ||
* @return RuleError[] | ||
*/ | ||
public function checkExtendsTags(Node $node, array $extendsTags): array | ||
{ | ||
$errors = []; | ||
foreach ($extendsTags as $extendsTag) { | ||
$type = $extendsTag->getType(); | ||
if (!$type instanceof ObjectType) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends contains non-object type %s.', $type->describe(VerbosityLevel::typeOnly())))->build(); | ||
continue; | ||
} | ||
|
||
$class = $type->getClassName(); | ||
$referencedClassReflection = $type->getClassReflection(); | ||
|
||
if ($referencedClassReflection === null) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends contains unknown class %s.', $class))->discoveringSymbolsTip()->build(); | ||
continue; | ||
} | ||
|
||
if (!$referencedClassReflection->isClass()) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends cannot contain non-class type %s.', $class))->build(); | ||
} elseif ($referencedClassReflection->isFinal()) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends cannot contain final class %s.', $class))->build(); | ||
} elseif ($this->checkClassCaseSensitivity) { | ||
$errors = array_merge( | ||
$errors, | ||
$this->classCaseSensitivityCheck->checkClassNames([ | ||
new ClassNameNodePair($class, $node), | ||
]), | ||
); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PhpDoc; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function count; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class RequireExtendsDefinitionClassRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private RequireExtendsCheck $requireExtendsCheck, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
$extendsTags = $classReflection->getRequireExtendsTags(); | ||
|
||
if (count($extendsTags) === 0) { | ||
return []; | ||
} | ||
|
||
if (!$classReflection->isInterface()) { | ||
return [ | ||
RuleErrorBuilder::message('PHPDoc tag @phpstan-require-extends is only valid on trait or interface.')->build(), | ||
]; | ||
} | ||
|
||
return $this->requireExtendsCheck->checkExtendsTags($node, $extendsTags); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should error when a class defines 2 or more
@*-require-extends
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not error, but support and check all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I misunderstood you. Please add a test and fix the rule.