From 07e32cdbe49f660891d558b243beed9121ef4679 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 11 Feb 2023 15:45:06 +0100 Subject: [PATCH] IterableType - fix getReferencedTemplateTypes --- src/Type/IterableType.php | 4 +-- .../MethodSignatureVarianceRuleTest.php | 10 ++++++ .../PHPStan/Rules/Generics/data/bug-8880.php | 34 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Generics/data/bug-8880.php diff --git a/src/Type/IterableType.php b/src/Type/IterableType.php index ec388b12c4..9c0e26050a 100644 --- a/src/Type/IterableType.php +++ b/src/Type/IterableType.php @@ -359,8 +359,8 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array { return array_merge( - $this->getIterableKeyType()->getReferencedTemplateTypes(TemplateTypeVariance::createCovariant()), - $this->getIterableValueType()->getReferencedTemplateTypes(TemplateTypeVariance::createCovariant()), + $this->getIterableKeyType()->getReferencedTemplateTypes($positionVariance), + $this->getIterableValueType()->getReferencedTemplateTypes($positionVariance), ); } diff --git a/tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php b/tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php index 07d3c237b8..7bb03804e5 100644 --- a/tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php +++ b/tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php @@ -176,4 +176,14 @@ public function testRule(): void ]); } + public function testBug8880(): void + { + $this->analyse([__DIR__ . '/data/bug-8880.php'], [ + [ + 'Template type T is declared as covariant, but occurs in contravariant position in parameter items of method Bug8880\IProcessor::processItems().', + 17, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Generics/data/bug-8880.php b/tests/PHPStan/Rules/Generics/data/bug-8880.php new file mode 100644 index 0000000000..4f3b1d39f5 --- /dev/null +++ b/tests/PHPStan/Rules/Generics/data/bug-8880.php @@ -0,0 +1,34 @@ + $items + * @return void + */ + function processItems($items); +} + +/** @implements IProcessor */ +final class StringPrinter implements IProcessor { + function processItems($items) { + foreach ($items as $s) + putStrLn($s); + } +} + +/** + * @param IProcessor $p + * @return void + */ +function callWithInt($p) { + $p->processItems([1]); +}