Skip to content

Commit 0725f5a

Browse files
committed
Fixed false positive about unreferenced template type in a parameter with nested generics
1 parent 48aea56 commit 0725f5a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Rules/FunctionDefinitionCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private function checkParametersAcceptor(
324324
TypeTraverser::map($parameter->getType(), static function (Type $type, callable $traverse) use (&$templateTypes): Type {
325325
if ($type instanceof TemplateType) {
326326
unset($templateTypes[$type->getName()]);
327-
return $type;
327+
return $traverse($type);
328328
}
329329

330330
return $traverse($type);

tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,14 @@ public function testRequiredParameterAfterOptional(int $phpVersionId, array $err
247247
$this->analyse([__DIR__ . '/data/required-parameter-after-optional.php'], $errors);
248248
}
249249

250+
public function testBug4641(): void
251+
{
252+
$this->analyse([__DIR__ . '/data/bug-4641.php'], [
253+
[
254+
'Template type U of method Bug4641\I::getRepository() is not referenced in a parameter.',
255+
26,
256+
],
257+
]);
258+
}
259+
250260
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug4641;
4+
5+
interface IEntity
6+
{
7+
8+
}
9+
10+
/** @template E of IEntity */
11+
interface IRepository
12+
{
13+
14+
}
15+
16+
interface I
17+
{
18+
19+
/**
20+
* @template E of IEntity
21+
* @template T of IRepository<E>
22+
* @template U
23+
* @phpstan-param class-string<T> $className
24+
* @phpstan-return T
25+
*/
26+
function getRepository(string $className): IRepository;
27+
28+
}

0 commit comments

Comments
 (0)