Skip to content

Commit 527269a

Browse files
committed
Fix @template name clash from different scopes
1 parent 3c43c54 commit 527269a

4 files changed

Lines changed: 94 additions & 2 deletions

File tree

src/Type/Generic/GenericObjectType.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,32 @@ public function getClassReflection(): ?ClassReflection
187187
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection
188188
{
189189
$reflection = parent::getProperty($propertyName, $scope);
190+
$ancestor = $this->getAncestorWithClassName($reflection->getDeclaringClass()->getName());
191+
if ($ancestor === null) {
192+
$classReflection = $reflection->getDeclaringClass();
193+
} else {
194+
$classReflection = $ancestor->getClassReflection();
195+
}
190196

191197
return new ResolvedPropertyReflection(
192198
$reflection,
193-
$this->getClassReflection()->getActiveTemplateTypeMap()
199+
$classReflection->getActiveTemplateTypeMap()
194200
);
195201
}
196202

197203
public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): MethodReflection
198204
{
199205
$reflection = parent::getMethod($methodName, $scope);
206+
$ancestor = $this->getAncestorWithClassName($reflection->getDeclaringClass()->getName());
207+
if ($ancestor === null) {
208+
$classReflection = $reflection->getDeclaringClass();
209+
} else {
210+
$classReflection = $ancestor->getClassReflection();
211+
}
200212

201213
return new ResolvedMethodReflection(
202214
$reflection,
203-
$this->getClassReflection()->getActiveTemplateTypeMap()
215+
$classReflection->getActiveTemplateTypeMap()
204216
);
205217
}
206218

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10837,6 +10837,11 @@ public function dataBug4558(): array
1083710837
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4558.php');
1083810838
}
1083910839

10840+
public function dataBug4557(): array
10841+
{
10842+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4557.php');
10843+
}
10844+
1084010845
/**
1084110846
* @param string $file
1084210847
* @return array<string, mixed[]>
@@ -11066,6 +11071,7 @@ private function gatherAssertTypes(string $file): array
1106611071
* @dataProvider dataBug1801
1106711072
* @dataProvider dataBug2927
1106811073
* @dataProvider dataBug4558
11074+
* @dataProvider dataBug4557
1106911075
* @param string $assertType
1107011076
* @param string $file
1107111077
* @param mixed ...$args
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Bug4557;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Lorem
8+
{
9+
}
10+
11+
class Ipsum extends Lorem
12+
{
13+
}
14+
15+
interface MockObject
16+
{
17+
}
18+
19+
class Foo
20+
{
21+
22+
/**
23+
* @template T
24+
* @param class-string<T> $class
25+
* @return T&MockObject
26+
*/
27+
public function createMock($class)
28+
{
29+
}
30+
31+
}
32+
33+
/**
34+
* @template T of Lorem
35+
*/
36+
class Bar extends Foo
37+
{
38+
39+
public function doBar(): void
40+
{
41+
$mock = $this->createMock(\stdClass::class);
42+
assertType('Bug4557\\MockObject&stdClass', $mock);
43+
}
44+
45+
/** @return T */
46+
public function doBaz()
47+
{
48+
49+
}
50+
51+
}
52+
53+
class Baz
54+
{
55+
56+
/**
57+
* @param Bar<Lorem> $barLorem
58+
* @param Bar<Ipsum> $barIpsum
59+
*/
60+
public function doFoo(Bar $barLorem, Bar $barIpsum): void
61+
{
62+
assertType('Bug4557\\Lorem', $barLorem->doBaz());
63+
assertType('Bug4557\\Ipsum', $barIpsum->doBaz());
64+
}
65+
66+
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,4 +1818,12 @@ public function testBug3534(): void
18181818
$this->analyse([__DIR__ . '/data/bug-3534.php'], []);
18191819
}
18201820

1821+
public function testBug4557(): void
1822+
{
1823+
$this->checkThisOnly = false;
1824+
$this->checkNullables = true;
1825+
$this->checkUnionTypes = true;
1826+
$this->analyse([__DIR__ . '/../../Analyser/data/bug-4557.php'], []);
1827+
}
1828+
18211829
}

0 commit comments

Comments
 (0)