Skip to content

Commit 0138dd9

Browse files
committed
Give even invalid nodes line and index attributes
1 parent 7ff42f5 commit 0138dd9

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

‎src/Parser/PhpDocParser.php

+26-37
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,21 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
7575
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
7676
} catch (ParserException $e) {
7777
$name = '';
78+
$startLine = $tokens->currentTokenLine();
79+
$startIndex = $tokens->currentTokenIndex();
7880
if (count($children) > 0) {
7981
$lastChild = $children[count($children) - 1];
8082
if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode) {
8183
$name = $lastChild->name;
84+
$startLine = $tokens->currentTokenLine();
85+
$startIndex = $tokens->currentTokenIndex();
8286
}
8387
}
88+
8489
$tokens->forwardToTheEnd();
85-
return new Ast\PhpDoc\PhpDocNode([
86-
new Ast\PhpDoc\PhpDocTagNode($name, new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e)),
87-
]);
90+
$tag = new Ast\PhpDoc\PhpDocTagNode($name, new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e));
91+
92+
return new Ast\PhpDoc\PhpDocNode([$this->enrichWithAttributes($tokens, $tag, $startLine, $startIndex)]);
8893
}
8994

9095
return new Ast\PhpDoc\PhpDocNode(array_values($children));
@@ -96,40 +101,37 @@ private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
96101
if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG)) {
97102
$startLine = $tokens->currentTokenLine();
98103
$startIndex = $tokens->currentTokenIndex();
99-
$tag = $this->parseTag($tokens);
100-
$endLine = $tokens->currentTokenLine();
101-
$endIndex = $tokens->currentTokenIndex();
102-
103-
if ($this->useLinesAttributes) {
104-
$tag->setAttribute(Ast\Attribute::START_LINE, $startLine);
105-
$tag->setAttribute(Ast\Attribute::END_LINE, $endLine);
106-
}
107-
108-
if ($this->useIndexAttributes) {
109-
$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
110-
$tag->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
111-
}
112-
113-
return $tag;
104+
return $this->enrichWithAttributes($tokens, $this->parseTag($tokens), $startLine, $startIndex);
114105
}
115106

116107
$startLine = $tokens->currentTokenLine();
117108
$startIndex = $tokens->currentTokenIndex();
118109
$text = $this->parseText($tokens);
110+
111+
return $this->enrichWithAttributes($tokens, $text, $startLine, $startIndex);
112+
}
113+
114+
/**
115+
* @template T of Ast\Node
116+
* @param T $tag
117+
* @return T
118+
*/
119+
private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int $startLine, int $startIndex): Ast\Node
120+
{
119121
$endLine = $tokens->currentTokenLine();
120122
$endIndex = $tokens->currentTokenIndex();
121123

122124
if ($this->useLinesAttributes) {
123-
$text->setAttribute(Ast\Attribute::START_LINE, $startLine);
124-
$text->setAttribute(Ast\Attribute::END_LINE, $endLine);
125+
$tag->setAttribute(Ast\Attribute::START_LINE, $startLine);
126+
$tag->setAttribute(Ast\Attribute::END_LINE, $endLine);
125127
}
126128

127129
if ($this->useIndexAttributes) {
128-
$text->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
129-
$text->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
130+
$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
131+
$tag->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
130132
}
131133

132-
return $text;
134+
return $tag;
133135
}
134136

135137

@@ -302,20 +304,7 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
302304
$tagValue = new Ast\PhpDoc\InvalidTagValueNode($this->parseOptionalDescription($tokens), $e);
303305
}
304306

305-
$endLine = $tokens->currentTokenLine();
306-
$endIndex = $tokens->currentTokenIndex();
307-
308-
if ($this->useLinesAttributes) {
309-
$tagValue->setAttribute(Ast\Attribute::START_LINE, $startLine);
310-
$tagValue->setAttribute(Ast\Attribute::END_LINE, $endLine);
311-
}
312-
313-
if ($this->useIndexAttributes) {
314-
$tagValue->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
315-
$tagValue->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
316-
}
317-
318-
return $tagValue;
307+
return $this->enrichWithAttributes($tokens, $tagValue, $startLine, $startIndex);
319308
}
320309

321310

‎tests/PHPStan/Parser/PhpDocParserTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -5561,6 +5561,27 @@ public function dataLinesAndIndexes(): iterable
55615561
[8, 12, 52, 115],
55625562
],
55635563
];
5564+
5565+
yield [
5566+
'/** @param Foo( */',
5567+
[
5568+
[1, 1, 1, 6],
5569+
],
5570+
];
5571+
5572+
yield [
5573+
'/** @phpstan-import-type TypeAlias from AnotherClass[] */',
5574+
[
5575+
[1, 1, 8, 12],
5576+
],
5577+
];
5578+
5579+
yield [
5580+
'/** @param Foo::** $a */',
5581+
[
5582+
[1, 1, 1, 10],
5583+
],
5584+
];
55645585
}
55655586

55665587
/**

0 commit comments

Comments
 (0)