Skip to content

Commit

Permalink
Bleeding edge - GenericAncestorsCheck - do not allow trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 25, 2024
1 parent 4ffbb3b commit bfbc401
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Rules/Generics/GenericAncestorsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,28 @@ public function check(
}

foreach ($ancestorType->getReferencedClasses() as $referencedClass) {
if ($this->reflectionProvider->hasClass($referencedClass)) {
if (!$this->reflectionProvider->hasClass($referencedClass)) {
$messages[] = RuleErrorBuilder::message(sprintf($invalidTypeMessage, $referencedClass))
->identifier('class.notFound')
->build();
continue;
}

if (!$this->absentTypeChecks) {
continue;
}

if ($referencedClass === $ancestorType->getClassName()) {
continue;
}

$classReflection = $this->reflectionProvider->getClass($referencedClass);
if (!$classReflection->isTrait()) {
continue;
}

$messages[] = RuleErrorBuilder::message(sprintf($invalidTypeMessage, $referencedClass))
->identifier('class.notFound')
->identifier('generics.trait')
->build();
}

Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function testRuleExtends(): void
'Call-site variance annotation of covariant Throwable in generic type ClassAncestorsExtends\FooGeneric<covariant Throwable, InvalidArgumentException> in PHPDoc tag @extends is not allowed.',
246,
],
[
'PHPDoc tag @extends has invalid type ClassAncestorsExtends\FooTrait.',
259,
],
]);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Generics/data/class-ancestors-extends.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,16 @@ class FooTypeProjection extends FooGeneric
{

}

trait FooTrait
{

}

/**
* @extends FooGeneric<FooTrait, \InvalidArgumentException>
*/
class TraitInExtends extends FooGeneric
{

}

0 comments on commit bfbc401

Please sign in to comment.