Skip to content

Commit a843d87

Browse files
committed
Fixed inferring template types from ThisType
1 parent 8f0150d commit a843d87

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/Type/Generic/GenericObjectType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,15 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
224224

225225
$ancestor = $receivedType->getAncestorWithClassName($this->getClassName());
226226

227-
if ($ancestor === null || !$ancestor instanceof GenericObjectType) {
227+
if ($ancestor === null) {
228+
return TemplateTypeMap::createEmpty();
229+
}
230+
$ancestorClassReflection = $ancestor->getClassReflection();
231+
if ($ancestorClassReflection === null) {
228232
return TemplateTypeMap::createEmpty();
229233
}
230234

231-
$otherTypes = $ancestor->getTypes();
235+
$otherTypes = $ancestorClassReflection->typeMapToList($ancestorClassReflection->getActiveTemplateTypeMap());
232236
$typeMap = TemplateTypeMap::createEmpty();
233237

234238
foreach ($this->getTypes() as $i => $type) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5705,6 +5705,11 @@ public function dataBug4714(): array
57055705
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4714.php');
57065706
}
57075707

5708+
public function dataBug4725(): array
5709+
{
5710+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4725.php');
5711+
}
5712+
57085713
/**
57095714
* @dataProvider dataArrayFunctions
57105715
* @param string $description
@@ -11329,6 +11334,7 @@ private function gatherAssertTypes(string $file): array
1132911334
* @dataProvider dataBug4707
1133011335
* @dataProvider dataBug4545
1133111336
* @dataProvider dataBug4714
11337+
* @dataProvider dataBug4725
1133211338
* @param string $assertType
1133311339
* @param string $file
1133411340
* @param mixed ...$args
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Bug4725;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Application_Model_Ada extends Clx_Model_Abstract {}
8+
9+
abstract class Clx_Model_Abstract {}
10+
11+
/**
12+
* @template T of Clx_Model_Abstract
13+
*/
14+
abstract class Clx_Model_Mapper_Abstract
15+
{
16+
}
17+
18+
19+
/**
20+
* @template T of Clx_Model_Abstract
21+
*/
22+
class Clx_Paginator_Adapter_Mapper
23+
{
24+
/**
25+
* @phpstan-param Clx_Model_Mapper_Abstract<T> $mapper
26+
*/
27+
public function __construct(Clx_Model_Mapper_Abstract $mapper)
28+
{
29+
}
30+
31+
/** @return T */
32+
public function getT()
33+
{
34+
35+
}
36+
}
37+
38+
/**
39+
* @template T of Application_Model_Ada
40+
* @extends Clx_Model_Mapper_Abstract<T>
41+
*/
42+
class ClxProductNet_Model_Mapper_Ada extends Clx_Model_Mapper_Abstract
43+
{
44+
45+
public function x() {
46+
$map = new Clx_Paginator_Adapter_Mapper($this);
47+
assertType('Bug4725\Clx_Paginator_Adapter_Mapper<T of Bug4725\Application_Model_Ada (class Bug4725\ClxProductNet_Model_Mapper_Ada, argument)>', $map);
48+
assertType('T of Bug4725\Application_Model_Ada (class Bug4725\ClxProductNet_Model_Mapper_Ada, argument)', $map->getT());
49+
}
50+
}

0 commit comments

Comments
 (0)