Skip to content

Commit 07e32cd

Browse files
committedFeb 11, 2023
IterableType - fix getReferencedTemplateTypes
1 parent 0e2e1b8 commit 07e32cd

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
 

‎src/Type/IterableType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
359359
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
360360
{
361361
return array_merge(
362-
$this->getIterableKeyType()->getReferencedTemplateTypes(TemplateTypeVariance::createCovariant()),
363-
$this->getIterableValueType()->getReferencedTemplateTypes(TemplateTypeVariance::createCovariant()),
362+
$this->getIterableKeyType()->getReferencedTemplateTypes($positionVariance),
363+
$this->getIterableValueType()->getReferencedTemplateTypes($positionVariance),
364364
);
365365
}
366366

‎tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,14 @@ public function testRule(): void
176176
]);
177177
}
178178

179+
public function testBug8880(): void
180+
{
181+
$this->analyse([__DIR__ . '/data/bug-8880.php'], [
182+
[
183+
'Template type T is declared as covariant, but occurs in contravariant position in parameter items of method Bug8880\IProcessor::processItems().',
184+
17,
185+
],
186+
]);
187+
}
188+
179189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug8880;
4+
5+
function putStrLn(string $s): void {
6+
echo $s, PHP_EOL;
7+
}
8+
9+
/**
10+
* @template-covariant T
11+
*/
12+
interface IProcessor {
13+
/**
14+
* @param iterable<T> $items
15+
* @return void
16+
*/
17+
function processItems($items);
18+
}
19+
20+
/** @implements IProcessor<string> */
21+
final class StringPrinter implements IProcessor {
22+
function processItems($items) {
23+
foreach ($items as $s)
24+
putStrLn($s);
25+
}
26+
}
27+
28+
/**
29+
* @param IProcessor<string | int> $p
30+
* @return void
31+
*/
32+
function callWithInt($p) {
33+
$p->processItems([1]);
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.