Skip to content
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 26 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7c9fbe3
Implement IncompatibleRequireExtendsTypeRule
staabm Jan 7, 2024
ff66719
Implement RequireExtendsRule
staabm Jan 7, 2024
65dde9f
report final classes
staabm Jan 7, 2024
c6f272f
php <8.1 compat
staabm Jan 7, 2024
b535c9b
use ClassLike instead of InClassNode for phpdoc validation
staabm Jan 8, 2024
06d3343
implement require-implements rules
staabm Jan 8, 2024
a901c62
Update config.level2.neon
staabm Jan 8, 2024
fee7d1a
fix php 7.2 compat
staabm Jan 8, 2024
c44d540
fix name collision
staabm Jan 8, 2024
e6bcbe7
Rules that just have the tag should be in the rules section above.
staabm Jan 9, 2024
2295b93
Utilize ClassReflection->implementsInterface/isSubclassOf
staabm Jan 9, 2024
8d3d9d3
Use instanceof ObjectType
staabm Jan 9, 2024
619790d
cover anonymous classes in tests
staabm Jan 9, 2024
682c4a0
Work on InClassNode
staabm Jan 9, 2024
bb3ee3e
Separate InClassNode and InTraitNode rules
staabm Jan 9, 2024
7f7334e
Utilize ObjectType->getClassReflection()
staabm Jan 9, 2024
1374549
fix php 7.x expectations
staabm Jan 9, 2024
a1ed5d8
error on trait phpdocs only once per declaration
staabm Jan 9, 2024
131c5ca
Rename rule classes
ondrejmirtes Jan 10, 2024
df9cf28
Cosmetic change
ondrejmirtes Jan 10, 2024
fc125fe
Use Trait_
ondrejmirtes Jan 10, 2024
fdab25b
Improve error messages
ondrejmirtes Jan 10, 2024
ec3fc94
Support psalm prefix
staabm Jan 10, 2024
ea57e39
Refactoring: extract RequireExtendsCheck
staabm Jan 10, 2024
f7bda5c
use getDisplayName()
staabm Jan 10, 2024
e31c497
require-extends can only be used once
staabm Jan 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Rules/PhpDoc/RequireExtendsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function count;
use function sprintf;

final class RequireExtendsCheck
Expand All @@ -30,6 +31,11 @@ public function __construct(
public function checkExtendsTags(Node $node, array $extendsTags): array
{
$errors = [];

if (count($extendsTags) > 1) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @phpstan-require-extends can only be used once.'))->build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, this makes sense because you can't have multiple parents :)

}

foreach ($extendsTags as $extendsTag) {
$type = $extendsTag->getType();
if (!$type instanceof ObjectType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function testRule(): void
'PHPDoc tag @phpstan-require-extends contains non-object type IncompatibleRequireExtends\UnresolvableExtendsInterface&stdClass.',
135,
],
[
'PHPDoc tag @phpstan-require-extends can only be used once.',
178,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testRule(): void
'PHPDoc tag @phpstan-require-extends contains non-object type *NEVER*.',
140,
],
[
'PHPDoc tag @phpstan-require-extends can only be used once.',
171,
],
]);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/incompatible-require-extends.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,16 @@ trait ValidPsalmTrait {}
new class {
use ValidPsalmTrait;
};

/**
* @phpstan-require-extends SomeClass
* @phpstan-require-extends SomeOtherClass
*/
trait TooMuchExtends {}

/**
* @phpstan-require-extends SomeClass
* @phpstan-require-extends SomeOtherClass
* @phpstan-require-extends SomeOtherClass
*/
interface TooMuchExtendsIface {}