diff --git a/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php b/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php index d9cc6333..e1b4ec0c 100644 --- a/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php +++ b/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php @@ -23,6 +23,7 @@ use CuyZ\Valinor\Definition\Repository\ClassDefinitionRepository; use CuyZ\Valinor\Type\ClassType; use CuyZ\Valinor\Type\GenericType; +use CuyZ\Valinor\Type\ObjectType; use CuyZ\Valinor\Type\Parser\Exception\InvalidType; use CuyZ\Valinor\Type\Parser\Factory\Specifications\AliasSpecification; use CuyZ\Valinor\Type\Parser\Factory\Specifications\ClassContextSpecification; @@ -183,7 +184,7 @@ private function typeResolver(ClassType $type, ReflectionClass $target): Reflect /** * @return array */ - private function localTypeAliases(ClassType $type): array + private function localTypeAliases(ObjectType $type): array { $reflection = Reflection::class($type->className()); $rawTypes = DocParser::localTypeAliases($reflection); @@ -224,7 +225,7 @@ private function importedTypeAliases(ClassType $type): array throw new InvalidTypeAliasImportClass($type, $class); } - if (! $classType instanceof ClassType) { + if (! $classType instanceof ObjectType) { throw new InvalidTypeAliasImportClassType($type, $classType); } @@ -242,7 +243,7 @@ private function importedTypeAliases(ClassType $type): array return $importedTypes; } - private function typeParser(ClassType $type): TypeParser + private function typeParser(ObjectType $type): TypeParser { $specs = [ new ClassContextSpecification($type->className()), diff --git a/src/Type/Types/UnresolvableType.php b/src/Type/Types/UnresolvableType.php index 08664c96..db4e6811 100644 --- a/src/Type/Types/UnresolvableType.php +++ b/src/Type/Types/UnresolvableType.php @@ -4,7 +4,7 @@ namespace CuyZ\Valinor\Type\Types; -use CuyZ\Valinor\Type\ClassType; +use CuyZ\Valinor\Type\ObjectType; use CuyZ\Valinor\Type\Parser\Exception\InvalidType; use CuyZ\Valinor\Type\Type; use CuyZ\Valinor\Utility\Reflection\Reflection; @@ -81,7 +81,7 @@ public static function forDocBlockTypeNotMatchingNative(ReflectionProperty|Refle return new self($typeFromDocBlock->toString(), $message); } - public static function forLocalAlias(string $raw, string $name, ClassType $type, InvalidType $exception): self + public static function forLocalAlias(string $raw, string $name, ObjectType $type, InvalidType $exception): self { return new self( $raw, diff --git a/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php b/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php index b661e40f..e7a24379 100644 --- a/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php +++ b/tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php @@ -51,10 +51,12 @@ public function test_type_aliases_are_imported_correctly(): void ->map($class, [ 'firstImportedType' => 42, 'secondImportedType' => 1337, + 'thirdImportedType' => 404, ]); self::assertSame(42, $result->firstImportedType); self::assertSame(1337, $result->secondImportedType); + self::assertSame(404, $result->thirdImportedType); } catch (MappingError $error) { $this->mappingFail($error); } @@ -112,12 +114,18 @@ class AnotherPhpStanLocalAlias public int $aliasWithEqualsSign; } +/** + * @phpstan-type AliasOfInteger = int + */ +interface InterfaceWithPhpStanLocalAlias {} + /** * Comment: * Some comment before import * * @phpstan-import-type AliasWithEqualsSign from PhpStanLocalAliases * @phpstan-import-type AliasWithoutEqualsSign from AnotherPhpStanLocalAlias + * @phpstan-import-type AliasOfInteger from InterfaceWithPhpStanLocalAlias */ class PhpStanAliasImport { @@ -126,6 +134,9 @@ class PhpStanAliasImport /** @var AliasWithoutEqualsSign */ public int $secondImportedType; + + /** @var AliasOfInteger */ + public int $thirdImportedType; } /** @@ -178,12 +189,18 @@ class AnotherPsalmLocalAliases public int $aliasWithEqualsSign; } +/** + * @phpstan-type AliasOfInteger = int + */ +interface InterfaceWithPsalmLocalAlias {} + /** * Comment: * Some comment before import * * @psalm-import-type AliasWithEqualsSign from PsalmLocalAliases * @psalm-import-type AliasWithoutEqualsSign from AnotherPsalmLocalAliases + * @psalm-import-type AliasOfInteger from InterfaceWithPsalmLocalAlias */ class PsalmAliasImport { @@ -192,4 +209,7 @@ class PsalmAliasImport /** @var AliasWithoutEqualsSign */ public int $secondImportedType; + + /** @var AliasOfInteger */ + public int $thirdImportedType; }