Skip to content

Commit

Permalink
IterableType - fix getReferencedTemplateTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 11, 2023
1 parent 0e2e1b8 commit 07e32cd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Generics/data/bug-8880.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bug8880;

function putStrLn(string $s): void {
echo $s, PHP_EOL;
}

/**
* @template-covariant T
*/
interface IProcessor {
/**
* @param iterable<T> $items
* @return void
*/
function processItems($items);
}

/** @implements IProcessor<string> */
final class StringPrinter implements IProcessor {
function processItems($items) {
foreach ($items as $s)
putStrLn($s);
}
}

/**
* @param IProcessor<string | int> $p
* @return void
*/
function callWithInt($p) {
$p->processItems([1]);
}

0 comments on commit 07e32cd

Please sign in to comment.