Skip to content

Commit

Permalink
type aliases: detect unsupported targets of type alias imports on par…
Browse files Browse the repository at this point in the history
…sing level
  • Loading branch information
jiripudil authored and ondrejmirtes committed Apr 3, 2021
1 parent e24edb6 commit e352d06
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,16 @@ 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')) {
$importedAs = $tokens->currentTokenValue();
$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
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */',
Expand Down

0 comments on commit e352d06

Please sign in to comment.