From e352d065af1ae9b41c12d1dfd309e90f7b1f55c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= Date: Sat, 3 Apr 2021 16:25:57 +0200 Subject: [PATCH] type aliases: detect unsupported targets of type alias imports on parsing level --- src/Parser/PhpDocParser.php | 6 ++-- tests/PHPStan/Parser/PhpDocParserTest.php | 38 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index b57679a0..704c69ac 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -403,8 +403,8 @@ private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc ); } - $importedFrom = $this->typeParser->parse($tokens); - assert($importedFrom instanceof IdentifierTypeNode); + $importedFrom = $tokens->currentTokenValue(); + $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); $importedAs = null; if ($tokens->tryConsumeTokenValue('as')) { @@ -412,7 +412,7 @@ private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); } - return new Ast\PhpDoc\TypeAliasImportTagValueNode($importedAlias, $importedFrom, $importedAs); + return new Ast\PhpDoc\TypeAliasImportTagValueNode($importedAlias, new IdentifierTypeNode($importedFrom), $importedAs); } private function parseOptionalVariableName(TokenIterator $tokens): string diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index d506dad7..c65b0328 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -2988,6 +2988,44 @@ public function provideTypeAliasImportTagsData(): \Iterator ]), ]; + yield [ + 'invalid non-identifier from', + '/** @phpstan-import-type TypeAlias from 42 */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-import-type', + new InvalidTagValueNode( + 'TypeAlias from 42', + new \PHPStan\PhpDocParser\Parser\ParserException( + '42', + Lexer::TOKEN_INTEGER, + 40, + Lexer::TOKEN_IDENTIFIER + ) + ) + ), + ]), + ]; + + yield [ + 'invalid non-simple-identifier from', + '/** @phpstan-import-type TypeAlias from AnotherClass[] */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-import-type', + new InvalidTagValueNode( + 'Unexpected token "[", expected \'*/\' at offset 52', + new \PHPStan\PhpDocParser\Parser\ParserException( + '[', + Lexer::TOKEN_OPEN_SQUARE_BRACKET, + 52, + Lexer::TOKEN_CLOSE_PHPDOC + ) + ) + ), + ]), + ]; + yield [ 'invalid missing from', '/** @phpstan-import-type TypeAlias */',