Skip to content

Commit 1308c52

Browse files
committed
Fix crash when template types map and variances map are not of equal length
1 parent c6fad64 commit 1308c52

3 files changed

Lines changed: 101 additions & 13 deletions

File tree

‎src/Reflection/ClassReflection.php‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,26 @@ public function getName(): string
241241

242242
public function getDisplayName(bool $withTemplateTypes = true): string
243243
{
244-
$name = $this->displayName;
245-
246244
if (
247245
$withTemplateTypes === false
248246
|| $this->resolvedTemplateTypeMap === null
249247
|| count($this->resolvedTemplateTypeMap->getTypes()) === 0
250248
) {
251-
return $name;
249+
return $this->displayName;
250+
}
251+
252+
$templateTypes = [];
253+
$variances = $this->getCallSiteVarianceMap()->getVariances();
254+
foreach ($this->getActiveTemplateTypeMap()->getTypes() as $name => $templateType) {
255+
$variance = $variances[$name] ?? null;
256+
if ($variance === null) {
257+
continue;
258+
}
259+
260+
$templateTypes[] = TypeProjectionHelper::describe($templateType, $variance, VerbosityLevel::typeOnly());
252261
}
253262

254-
return $name . '<' . implode(',', array_map(
255-
static fn (Type $type, TemplateTypeVariance $variance): string => TypeProjectionHelper::describe($type, $variance, VerbosityLevel::typeOnly()),
256-
$this->getActiveTemplateTypeMap()->getTypes(),
257-
$this->getCallSiteVarianceMap()->getVariances(),
258-
)) . '>';
263+
return $this->displayName . '<' . implode(',', $templateTypes) . '>';
259264
}
260265

261266
public function getCacheKey(): string
@@ -268,11 +273,18 @@ public function getCacheKey(): string
268273
$cacheKey = $this->displayName;
269274

270275
if ($this->resolvedTemplateTypeMap !== null) {
271-
$cacheKey .= '<' . implode(',', array_map(
272-
static fn (Type $type, TemplateTypeVariance $variance): string => TypeProjectionHelper::describe($type, $variance, VerbosityLevel::cache()),
273-
$this->getActiveTemplateTypeMap()->getTypes(),
274-
$this->getCallSiteVarianceMap()->getVariances(),
275-
)) . '>';
276+
$templateTypes = [];
277+
$variances = $this->getCallSiteVarianceMap()->getVariances();
278+
foreach ($this->getActiveTemplateTypeMap()->getTypes() as $name => $templateType) {
279+
$variance = $variances[$name] ?? null;
280+
if ($variance === null) {
281+
continue;
282+
}
283+
284+
$templateTypes[] = TypeProjectionHelper::describe($templateType, $variance, VerbosityLevel::cache());
285+
}
286+
287+
$cacheKey .= '<' . implode(',', $templateTypes) . '>';
276288
}
277289

278290
if ($this->extraCacheKey !== null) {

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,16 @@ public function testBug9994(): void
12421242
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(1|2|3|null): mixed, false given.', $errors[1]->getMessage());
12431243
}
12441244

1245+
public function testBug10049(): void
1246+
{
1247+
if (PHP_VERSION_ID < 80100) {
1248+
$this->markTestSkipped('Test requires PHP 8.1.');
1249+
}
1250+
1251+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10049-recursive.php');
1252+
$this->assertNoErrors($errors);
1253+
}
1254+
12451255
/**
12461256
* @param string[]|null $allAnalysedFiles
12471257
* @return Error[]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10049Recursive;
4+
5+
/**
6+
* @template SELF of SimpleEntity<SELF>
7+
*/
8+
abstract class SimpleEntity
9+
{
10+
/**
11+
* @param SimpleTable<SELF> $table
12+
*/
13+
public function __construct(protected readonly SimpleTable $table)
14+
{
15+
}
16+
}
17+
18+
/**
19+
* @template-covariant E of SimpleEntity
20+
*/
21+
class SimpleTable
22+
{
23+
/**
24+
* @template ENTITY of SimpleEntity
25+
*
26+
* @param class-string<ENTITY> $className
27+
*
28+
* @return SimpleTable<ENTITY>
29+
*/
30+
public static function table(string $className, string $name): SimpleTable
31+
{
32+
return new SimpleTable($className, $name);
33+
}
34+
35+
/**
36+
* @param class-string<E> $className
37+
*/
38+
private function __construct(readonly string $className, readonly string $table)
39+
{
40+
}
41+
}
42+
43+
/**
44+
* @template-extends SimpleEntity<TestEntity>
45+
*/
46+
class TestEntity extends SimpleEntity
47+
{
48+
public function __construct()
49+
{
50+
$table = SimpleTable::table(TestEntity::class, 'testentity');
51+
parent::__construct($table);
52+
}
53+
}
54+
55+
56+
/**
57+
* @template-extends SimpleEntity<AnotherEntity>
58+
*/
59+
class AnotherEntity extends SimpleEntity
60+
{
61+
public function __construct()
62+
{
63+
$table = SimpleTable::table(AnotherEntity::class, 'anotherentity');
64+
parent::__construct($table);
65+
}
66+
}

0 commit comments

Comments
 (0)