Skip to content

Commit

Permalink
Do not include TOKEN_CLOSE_PHPDOC in end index with one-line tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 22, 2023
1 parent 10553ab commit 900bd69
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ 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--;
}
}

$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
$tag->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public function __construct(array $tokens, int $index = 0)
}


/**
* @return list<array{string, int, int}>
*/
public function getTokens(): array
{
return $this->tokens;
}


public function currentTokenValue(): string
{
return $this->tokens[$this->index][Lexer::VALUE_OFFSET];
Expand Down
8 changes: 8 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ private function enrichWithAttributes(TokenIterator $tokens, Ast\Type\TypeNode $
}

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--;
}
}

$type->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
$type->setAttribute(Ast\Attribute::END_INDEX, $endIndex);
}
Expand Down
70 changes: 67 additions & 3 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5530,7 +5530,7 @@ public function dataLinesAndIndexes(): iterable
yield [
'/** @param Foo $a */',
[
[1, 1, 1, 7],
[1, 1, 1, 5],
],
];

Expand Down Expand Up @@ -5573,7 +5573,7 @@ public function dataLinesAndIndexes(): iterable
yield [
'/** @param Foo( */',
[
[1, 1, 1, 6],
[1, 1, 1, 4],
],
];

Expand All @@ -5587,7 +5587,28 @@ public function dataLinesAndIndexes(): iterable
yield [
'/** @param Foo::** $a */',
[
[1, 1, 1, 10],
[1, 1, 1, 8],
],
];

yield [
'/** @param Foo::** $a*/',
[
[1, 1, 1, 8],
],
];

yield [
'/** @return Foo */',
[
[1, 1, 1, 3],
],
];

yield [
'/** @return Foo*/',
[
[1, 1, 1, 3],
],
];
}
Expand Down Expand Up @@ -5617,4 +5638,47 @@ public function testLinesAndIndexes(string $phpDoc, array $childrenLines): void
}
}

/**
* @return array<mixed>
*/
public function dataTypeLinesAndIndexes(): iterable
{
yield [
'/** @return Foo */',
[1, 1, 3, 3],
];

yield [
'/** @return Foo*/',
[1, 1, 3, 3],
];
}

/**
* @dataProvider dataTypeLinesAndIndexes
* @param array{int, int, int, int} $lines
*/
public function testTypeLinesAndIndexes(string $phpDoc, array $lines): void
{
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
$constExprParser = new ConstExprParser(true, true);
$usedAttributes = [
'lines' => true,
'indexes' => true,
];
$typeParser = new TypeParser($constExprParser, true, $usedAttributes);
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes);
$phpDocNode = $phpDocParser->parse($tokens);
$this->assertInstanceOf(PhpDocTagNode::class, $phpDocNode->children[0]);
$this->assertInstanceOf(ReturnTagValueNode::class, $phpDocNode->children[0]->value);

$type = $phpDocNode->children[0]->value->type;
$this->assertInstanceOf(IdentifierTypeNode::class, $type);

$this->assertSame($lines[0], $type->getAttribute(Attribute::START_LINE));
$this->assertSame($lines[1], $type->getAttribute(Attribute::END_LINE));
$this->assertSame($lines[2], $type->getAttribute(Attribute::START_INDEX));
$this->assertSame($lines[3], $type->getAttribute(Attribute::END_INDEX));
}

}

0 comments on commit 900bd69

Please sign in to comment.