Skip to content

Commit 1b5710a

Browse files
committed
Fix inferring TemplateUnionType
1 parent f6e4f87 commit 1b5710a

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/Type/Generic/TemplateTypeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function isSubTypeOf(Type $type): TrinaryLogic
162162

163163
public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
164164
{
165-
if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
165+
if (!$receivedType instanceof TemplateType && ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType)) {
166166
return $receivedType->inferTemplateTypesOn($this);
167167
}
168168

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ public function dataFileAsserts(): iterable
499499
yield from $this->gatherAssertTypes(__DIR__ . '/data/math.php');
500500

501501
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1870.php');
502+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5562.php');
502503
}
503504

504505
/**

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,4 +2124,12 @@ public function testBug3530(): void
21242124
$this->analyse([__DIR__ . '/data/bug-3530.php'], []);
21252125
}
21262126

2127+
public function testBug5562(): void
2128+
{
2129+
$this->checkThisOnly = false;
2130+
$this->checkNullables = true;
2131+
$this->checkUnionTypes = true;
2132+
$this->analyse([__DIR__ . '/data/bug-5562.php'], []);
2133+
}
2134+
21272135
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug5562;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @template T of int|string
12+
* @param T $test
13+
* @return T
14+
*/
15+
public function foo($test)
16+
{
17+
assertType('T of int|string (method Bug5562\Foo::foo(), argument)', $test);
18+
$bar = $this->bar($test);
19+
assertType('T of int|string (method Bug5562\Foo::foo(), argument)', $bar);
20+
21+
return $bar;
22+
}
23+
24+
/**
25+
* @template T of int|string
26+
* @param T $test
27+
* @return T
28+
*/
29+
public function bar($test)
30+
{
31+
return $test;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)