Skip to content

Commit 900bd69

Browse files
committed
Do not include TOKEN_CLOSE_PHPDOC in end index with one-line tags
1 parent 10553ab commit 900bd69

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

Diff for: src/Parser/PhpDocParser.php

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int
127127
}
128128

129129
if ($this->useIndexAttributes) {
130+
$tokensArray = $tokens->getTokens();
131+
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_CLOSE_PHPDOC) {
132+
$endIndex--;
133+
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
134+
$endIndex--;
135+
}
136+
}
137+
130138
$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
131139
$tag->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
132140
}

Diff for: src/Parser/TokenIterator.php

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public function __construct(array $tokens, int $index = 0)
3737
}
3838

3939

40+
/**
41+
* @return list<array{string, int, int}>
42+
*/
43+
public function getTokens(): array
44+
{
45+
return $this->tokens;
46+
}
47+
48+
4049
public function currentTokenValue(): string
4150
{
4251
return $this->tokens[$this->index][Lexer::VALUE_OFFSET];

Diff for: src/Parser/TypeParser.php

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ private function enrichWithAttributes(TokenIterator $tokens, Ast\Type\TypeNode $
7272
}
7373

7474
if ($this->useIndexAttributes) {
75+
$tokensArray = $tokens->getTokens();
76+
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_CLOSE_PHPDOC) {
77+
$endIndex--;
78+
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
79+
$endIndex--;
80+
}
81+
}
82+
7583
$type->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
7684
$type->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
7785
}

Diff for: tests/PHPStan/Parser/PhpDocParserTest.php

+67-3
Original file line numberDiff line numberDiff line change
@@ -5530,7 +5530,7 @@ public function dataLinesAndIndexes(): iterable
55305530
yield [
55315531
'/** @param Foo $a */',
55325532
[
5533-
[1, 1, 1, 7],
5533+
[1, 1, 1, 5],
55345534
],
55355535
];
55365536

@@ -5573,7 +5573,7 @@ public function dataLinesAndIndexes(): iterable
55735573
yield [
55745574
'/** @param Foo( */',
55755575
[
5576-
[1, 1, 1, 6],
5576+
[1, 1, 1, 4],
55775577
],
55785578
];
55795579

@@ -5587,7 +5587,28 @@ public function dataLinesAndIndexes(): iterable
55875587
yield [
55885588
'/** @param Foo::** $a */',
55895589
[
5590-
[1, 1, 1, 10],
5590+
[1, 1, 1, 8],
5591+
],
5592+
];
5593+
5594+
yield [
5595+
'/** @param Foo::** $a*/',
5596+
[
5597+
[1, 1, 1, 8],
5598+
],
5599+
];
5600+
5601+
yield [
5602+
'/** @return Foo */',
5603+
[
5604+
[1, 1, 1, 3],
5605+
],
5606+
];
5607+
5608+
yield [
5609+
'/** @return Foo*/',
5610+
[
5611+
[1, 1, 1, 3],
55915612
],
55925613
];
55935614
}
@@ -5617,4 +5638,47 @@ public function testLinesAndIndexes(string $phpDoc, array $childrenLines): void
56175638
}
56185639
}
56195640

5641+
/**
5642+
* @return array<mixed>
5643+
*/
5644+
public function dataTypeLinesAndIndexes(): iterable
5645+
{
5646+
yield [
5647+
'/** @return Foo */',
5648+
[1, 1, 3, 3],
5649+
];
5650+
5651+
yield [
5652+
'/** @return Foo*/',
5653+
[1, 1, 3, 3],
5654+
];
5655+
}
5656+
5657+
/**
5658+
* @dataProvider dataTypeLinesAndIndexes
5659+
* @param array{int, int, int, int} $lines
5660+
*/
5661+
public function testTypeLinesAndIndexes(string $phpDoc, array $lines): void
5662+
{
5663+
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
5664+
$constExprParser = new ConstExprParser(true, true);
5665+
$usedAttributes = [
5666+
'lines' => true,
5667+
'indexes' => true,
5668+
];
5669+
$typeParser = new TypeParser($constExprParser, true, $usedAttributes);
5670+
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes);
5671+
$phpDocNode = $phpDocParser->parse($tokens);
5672+
$this->assertInstanceOf(PhpDocTagNode::class, $phpDocNode->children[0]);
5673+
$this->assertInstanceOf(ReturnTagValueNode::class, $phpDocNode->children[0]->value);
5674+
5675+
$type = $phpDocNode->children[0]->value->type;
5676+
$this->assertInstanceOf(IdentifierTypeNode::class, $type);
5677+
5678+
$this->assertSame($lines[0], $type->getAttribute(Attribute::START_LINE));
5679+
$this->assertSame($lines[1], $type->getAttribute(Attribute::END_LINE));
5680+
$this->assertSame($lines[2], $type->getAttribute(Attribute::START_INDEX));
5681+
$this->assertSame($lines[3], $type->getAttribute(Attribute::END_INDEX));
5682+
}
5683+
56205684
}

0 commit comments

Comments
 (0)