Skip to content

Commit a26e0b5

Browse files
phpstan-botstaabm
authored andcommitted
Fix circular @phpstan-import-type between classes making aliases unresolvable
- When two classes have mutual @phpstan-import-type tags (FooType imports Bar from BarType, BarType imports Foo from FooType), the circular detection in ClassReflection::getTypeAliases() was too aggressive - The fix returns local type aliases (defined via @phpstan-type) when circular import resolution is detected, since local aliases don't depend on imports - Added regression test in tests/PHPStan/Rules/PhpDoc/data/bug-11463.php Closes phpstan/phpstan#11463
1 parent 1991098 commit a26e0b5

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Reflection/ClassReflection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,13 @@ public function getTypeAliases(): array
13761376

13771377
// prevent circular imports
13781378
if (array_key_exists($this->getName(), self::$resolvingTypeAliasImports)) {
1379+
// Return only local type aliases to break the cycle.
1380+
// Imported aliases are not available yet, but local ones
1381+
// don't depend on other classes and can be returned safely.
1382+
$localAliases = array_map(static fn (TypeAliasTag $typeAliasTag): TypeAlias => $typeAliasTag->getTypeAlias(), $typeAliasTags);
1383+
if ($localAliases !== []) {
1384+
return $localAliases;
1385+
}
13791386
throw new CircularTypeAliasDefinitionException();
13801387
}
13811388

tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,9 @@ public function testBug9708(): void
487487
$this->analyse([__DIR__ . '/data/bug-9708.php'], []);
488488
}
489489

490+
public function testBug11463(): void
491+
{
492+
$this->analyse([__DIR__ . '/data/bug-11463.php'], []);
493+
}
494+
490495
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug11463;
4+
5+
/**
6+
* @phpstan-type Foo 'foo'
7+
*
8+
* @phpstan-import-type Bar from BarType
9+
*/
10+
class FooType
11+
{
12+
/**
13+
* @param Bar $bar
14+
*/
15+
public function foo(string $bar): void {}
16+
}
17+
18+
/**
19+
* @phpstan-import-type Foo from FooType
20+
*
21+
* @phpstan-type Bar 'bar'
22+
*/
23+
class BarType {
24+
/**
25+
* @param Foo $foo
26+
*/
27+
public function bar($foo): string { return $foo; }
28+
}

0 commit comments

Comments
 (0)