Skip to content

Commit

Permalink
Fix GenericAncestorsCheck variance and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Dec 5, 2022
1 parent c3b33ac commit 32f466a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Rules/Generics/GenericAncestorsCheck.php
Expand Up @@ -98,7 +98,7 @@ public function check(
$messages[] = RuleErrorBuilder::message(sprintf($invalidTypeMessage, $referencedClass))->build();
}

$variance = TemplateTypeVariance::createInvariant();
$variance = TemplateTypeVariance::createStatic();
$messageContext = sprintf(
$invalidVarianceMessage,
$ancestorType->describe(VerbosityLevel::typeOnly()),
Expand Down
10 changes: 9 additions & 1 deletion tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php
Expand Up @@ -95,9 +95,17 @@ public function testRuleExtends(): void
'Template type T is declared as covariant, but occurs in invariant position in extended type ClassAncestorsExtends\FooGeneric8<T, T> of class ClassAncestorsExtends\FooGeneric9.',
192,
],
[
'Template type T is declared as contravariant, but occurs in covariant position in extended type ClassAncestorsExtends\FooGeneric8<T, T> of class ClassAncestorsExtends\FooGeneric10.',
201,
],
[
'Template type T is declared as contravariant, but occurs in invariant position in extended type ClassAncestorsExtends\FooGeneric8<T, T> of class ClassAncestorsExtends\FooGeneric10.',
201,
],
[
'Class ClassAncestorsExtends\FilterIteratorChild extends generic class FilterIterator but does not specify its types: TKey, TValue, TIterator',
197,
215,
'You can turn this off by setting <fg=cyan>checkGenericClassInNonGenericObjectType: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Generics/data/class-ancestors-extends.php
Expand Up @@ -194,6 +194,24 @@ class FooGeneric9 extends FooGeneric8

}

/**
* @template-contravariant T
* @extends FooGeneric8<T, T>
*/
class FooGeneric10 extends FooGeneric8
{

}

/**
* @template T
* @extends FooGeneric8<T, T>
*/
class FooGeneric11 extends FooGeneric8
{

}

class FilterIteratorChild extends \FilterIterator
{

Expand Down

0 comments on commit 32f466a

Please sign in to comment.