Skip to content

Commit

Permalink
Fixed false positive about unreferenced template type in a parameter …
Browse files Browse the repository at this point in the history
…with nested generics
  • Loading branch information
ondrejmirtes committed Mar 8, 2021
1 parent 48aea56 commit 0725f5a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private function checkParametersAcceptor(
TypeTraverser::map($parameter->getType(), static function (Type $type, callable $traverse) use (&$templateTypes): Type {
if ($type instanceof TemplateType) {
unset($templateTypes[$type->getName()]);
return $type;
return $traverse($type);
}

return $traverse($type);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,14 @@ public function testRequiredParameterAfterOptional(int $phpVersionId, array $err
$this->analyse([__DIR__ . '/data/required-parameter-after-optional.php'], $errors);
}

public function testBug4641(): void
{
$this->analyse([__DIR__ . '/data/bug-4641.php'], [
[
'Template type U of method Bug4641\I::getRepository() is not referenced in a parameter.',
26,
],
]);
}

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

namespace Bug4641;

interface IEntity
{

}

/** @template E of IEntity */
interface IRepository
{

}

interface I
{

/**
* @template E of IEntity
* @template T of IRepository<E>
* @template U
* @phpstan-param class-string<T> $className
* @phpstan-return T
*/
function getRepository(string $className): IRepository;

}

0 comments on commit 0725f5a

Please sign in to comment.