diff --git a/src/Psalm/Internal/Type/TypeParser.php b/src/Psalm/Internal/Type/TypeParser.php index ca3298baa..78578b550 100644 --- a/src/Psalm/Internal/Type/TypeParser.php +++ b/src/Psalm/Internal/Type/TypeParser.php @@ -78,7 +78,7 @@ class TypeParser } else { $only_token[0] = TypeTokenizer::fixScalarTerms($only_token[0], $php_version); - $atomic = Atomic::create($only_token[0], $php_version, $template_type_map); + $atomic = Atomic::create($only_token[0], $php_version, $template_type_map, $type_aliases); $atomic->offset_start = 0; $atomic->offset_end = strlen($only_token[0]); diff --git a/tests/TypeAnnotationTest.php b/tests/TypeAnnotationTest.php index 96886ec69..41f5d7299 100644 --- a/tests/TypeAnnotationTest.php +++ b/tests/TypeAnnotationTest.php @@ -189,6 +189,70 @@ class TypeAnnotationTest extends TestCase } }' ], + 'classTypeAliasImportWithAlias' => [ + ' "Nokia"]; + } + } + + /** + * @psalm-import-type PhoneType from Phone as TPhone + */ + class User { + /** @psalm-return TPhone */ + function toArray(): array { + return array_merge([], (new Phone)->toArray()); + } + }' + ], + 'classTypeAliasDirectUsage' => [ + ' "Nokia"]; + } + } + + /** + * @psalm-import-type PhoneType from Phone + */ + class User { + /** @psalm-return PhoneType */ + function toArray(): array { + return array_merge([], (new Phone)->toArray()); + } + }' + ], + 'classTypeAliasFromExternalNamespace' => [ + ' "Nokia"]; + } + } + } + + namespace Bar { + /** + * @psalm-import-type PhoneType from \Foo\Phone + */ + class User { + /** @psalm-return PhoneType */ + function toArray(): array { + return (new \Foo\Phone)->toArray(); + } + } + }' + ], ]; } @@ -254,6 +318,38 @@ class TypeAnnotationTest extends TestCase }', 'error_message' => 'InvalidReturnStatement', ], + 'classTypeInvalidAlias' => [ + ' "Matt"]; + } + } + + /** + * @psalm-import-type PhoneType from Phone + */ + class User { + /** @psalm-return UserType */ + function toArray(): array { + return (new Phone)->toArray(); + } + }', + 'error_message' => 'UndefinedDocblockClass', + ], + 'classTypeAliasFromInvalidClass' => [ + ' 'UndefinedDocblockClass', + ], ]; } }