From d19800fe23c4b645e188b7d9b0663ce47e1dc1cb Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Tue, 1 Jul 2025 09:29:46 +0200 Subject: [PATCH] [TypeInfo] Fix imported-only alias resolving --- .../TypeInfo/Tests/Fixtures/DummyWithTypeAliases.php | 11 +++++++++++ .../Tests/TypeContext/TypeContextFactoryTest.php | 5 +++++ .../TypeInfo/TypeContext/TypeContextFactory.php | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithTypeAliases.php b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithTypeAliases.php index 7f73190df1549..85f5784dd2cfe 100644 --- a/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithTypeAliases.php +++ b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithTypeAliases.php @@ -57,6 +57,17 @@ final class DummyWithTypeAliases public mixed $psalmOtherAliasedExternalAlias; } +/** + * @phpstan-import-type CustomInt from DummyWithPhpDoc + */ +final class DummyWithImportedOnlyTypeAliases +{ + /** + * @var CustomInt + */ + public mixed $externalAlias; +} + /** * @phpstan-type Foo = array{0: Bar} * @phpstan-type Bar = array{0: Foo} diff --git a/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php b/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php index cf0f1bb91179f..532ba3984250e 100644 --- a/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php +++ b/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php @@ -15,6 +15,7 @@ use Symfony\Component\TypeInfo\Exception\LogicException; use Symfony\Component\TypeInfo\Tests\Fixtures\AbstractDummy; use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy; +use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithImportedOnlyTypeAliases; use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithInvalidTypeAlias; use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithInvalidTypeAliasImport; use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithRecursiveTypeAliases; @@ -179,6 +180,10 @@ public function testCollectTypeAliases() 'PsalmCustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]), 'PsalmAliasedCustomInt' => Type::int(), ], $this->typeContextFactory->createFromReflection(new \ReflectionProperty(DummyWithTypeAliases::class, 'localAlias'))->typeAliases); + + $this->assertEquals([ + 'CustomInt' => Type::int(), + ], $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithImportedOnlyTypeAliases::class))->typeAliases); } public function testDoNotCollectTypeAliasesWhenToStringTypeResolver() diff --git a/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php b/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php index 8e1cc3d4314e7..a7564557e555c 100644 --- a/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php +++ b/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php @@ -241,7 +241,7 @@ private function collectTypeAliases(\ReflectionClass $reflection, TypeContext $t private function resolveTypeAliases(array $toResolve, array $resolved, TypeContext $typeContext): array { if (!$toResolve) { - return []; + return $resolved; } $typeContext = new TypeContext(