Skip to content

Commit

Permalink
Fix token indexes in TypeAliasImportTagValueNode
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 29, 2023
1 parent 5f901f0 commit b0c366d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,8 @@ private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int

if ($this->useIndexAttributes) {
$tokensArray = $tokens->getTokens();
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_CLOSE_PHPDOC) {
$endIndex--;
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
$endIndex--;
}
} elseif ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_PHPDOC_EOL) {
$endIndex--;
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
$endIndex--;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5614,7 +5614,7 @@ public function dataLinesAndIndexes(): iterable
yield [
'/** @phpstan-import-type TypeAlias from AnotherClass[] */',
[
[1, 1, 8, 12],
[1, 1, 8, 11],
],
];

Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,23 @@ public function enterNode(Node $node)

},
];

yield [
'/** @phpstan-import-type Foo from \Bar as Lorem */',
'/** @phpstan-import-type Foo from Bar as Lorem */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof TypeAliasImportTagValueNode) {
$node->importedFrom = new IdentifierTypeNode('Bar');
}

return $node;
}

},
];
}

/**
Expand Down

0 comments on commit b0c366d

Please sign in to comment.