Skip to content

Commit a61e41d

Browse files
committed
Fix enrichWithAttributes
1 parent d789c95 commit a61e41d

File tree

5 files changed

+25
-36
lines changed

5 files changed

+25
-36
lines changed

src/Parser/ConstExprParser.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,14 @@ private function parseArrayItem(TokenIterator $tokens): Ast\ConstExpr\ConstExprA
245245
*/
246246
private function enrichWithAttributes(TokenIterator $tokens, Ast\ConstExpr\ConstExprNode $node, int $startLine, int $startIndex): Ast\ConstExpr\ConstExprNode
247247
{
248-
$endLine = $tokens->currentTokenLine();
249-
$endIndex = $tokens->currentTokenIndex();
250248
if ($this->useLinesAttributes) {
251249
$node->setAttribute(Ast\Attribute::START_LINE, $startLine);
252-
$node->setAttribute(Ast\Attribute::END_LINE, $endLine);
250+
$node->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
253251
}
254252

255253
if ($this->useIndexAttributes) {
256-
$tokensArray = $tokens->getTokens();
257-
$endIndex--;
258-
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
259-
$endIndex--;
260-
}
261-
262254
$node->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
263-
$node->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
255+
$node->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
264256
}
265257

266258
return $node;

src/Parser/PhpDocParser.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,14 @@ private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
161161
*/
162162
private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int $startLine, int $startIndex): Ast\Node
163163
{
164-
$endLine = $tokens->currentTokenLine();
165-
$endIndex = $tokens->currentTokenIndex();
166-
167164
if ($this->useLinesAttributes) {
168165
$tag->setAttribute(Ast\Attribute::START_LINE, $startLine);
169-
$tag->setAttribute(Ast\Attribute::END_LINE, $endLine);
166+
$tag->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
170167
}
171168

172169
if ($this->useIndexAttributes) {
173-
$tokensArray = $tokens->getTokens();
174-
$endIndex--;
175-
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
176-
$endIndex--;
177-
}
178-
179170
$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
180-
$tag->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
171+
$tag->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
181172
}
182173

183174
return $tag;

src/Parser/TokenIterator.php

+15
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ public function currentTokenIndex(): int
102102
}
103103

104104

105+
public function endIndexOfLastRelevantToken(): int
106+
{
107+
$endIndex = $this->currentTokenIndex();
108+
$endIndex--;
109+
while (in_array($this->tokens[$endIndex][Lexer::TYPE_OFFSET], $this->skippedTokenTypes, true)) {
110+
if (!isset($this->tokens[$endIndex - 1])) {
111+
break;
112+
}
113+
$endIndex--;
114+
}
115+
116+
return $endIndex;
117+
}
118+
119+
105120
public function isCurrentTokenValue(string $tokenValue): bool
106121
{
107122
return $this->tokens[$this->index][Lexer::VALUE_OFFSET] === $tokenValue;

src/Parser/TypeParser.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,14 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
7070
*/
7171
public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int $startLine, int $startIndex): Ast\Node
7272
{
73-
$endLine = $tokens->currentTokenLine();
74-
$endIndex = $tokens->currentTokenIndex();
75-
7673
if ($this->useLinesAttributes) {
7774
$type->setAttribute(Ast\Attribute::START_LINE, $startLine);
78-
$type->setAttribute(Ast\Attribute::END_LINE, $endLine);
75+
$type->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
7976
}
8077

8178
if ($this->useIndexAttributes) {
82-
$tokensArray = $tokens->getTokens();
83-
$endIndex--;
84-
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
85-
$endIndex--;
86-
}
87-
8879
$type->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
89-
$type->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
80+
$type->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
9081
}
9182

9283
return $type;

tests/PHPStan/Parser/PhpDocParserTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5970,12 +5970,12 @@ public function dataDeepNodesLinesAndIndexes(): iterable
59705970
[2, 8, 2, 23], // PhpDocTagNode
59715971
[2, 8, 3, 23], // DoctrineTagValueNode
59725972
[2, 8, 3, 23], // DoctrineAnnotation
5973-
[2, 8, 4, 22], // DoctrineArgument
5974-
[2, 8, 4, 22], // DoctrineArray
5973+
[2, 8, 4, 21], // DoctrineArgument
5974+
[2, 8, 4, 21], // DoctrineArray
59755975
[3, 3, 7, 7], // DoctrineArrayItem
59765976
[3, 3, 7, 7], // ConstExprIntegerNode
5977-
[4, 5, 11, 12], // DoctrineArrayItem
5978-
[4, 5, 11, 12], // ConstExprIntegerNode
5977+
[4, 5, 11, 11], // DoctrineArrayItem
5978+
[4, 5, 11, 11], // ConstExprIntegerNode
59795979
[6, 6, 18, 18], // DoctrineArrayItem
59805980
[6, 6, 18, 18], // ConstExprIntegerNode
59815981
],

0 commit comments

Comments
 (0)