Skip to content

Commit 08ccb8d

Browse files
committed
More precise type indexes
1 parent 57f6787 commit 08ccb8d

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

src/Parser/TypeParser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ private function enrichWithAttributes(TokenIterator $tokens, Ast\Type\TypeNode $
7373

7474
if ($this->useIndexAttributes) {
7575
$tokensArray = $tokens->getTokens();
76-
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_CLOSE_PHPDOC) {
76+
$endIndex--;
77+
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
7778
$endIndex--;
78-
if ($tokensArray[$endIndex][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
79-
$endIndex--;
80-
}
8179
}
8280

8381
$type->setAttribute(Ast\Attribute::START_INDEX, $startIndex);

tests/PHPStan/Parser/PhpDocParserTest.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5641,7 +5641,7 @@ public function testLinesAndIndexes(string $phpDoc, array $childrenLines): void
56415641
/**
56425642
* @return array<mixed>
56435643
*/
5644-
public function dataTypeLinesAndIndexes(): iterable
5644+
public function dataReturnTypeLinesAndIndexes(): iterable
56455645
{
56465646
yield [
56475647
'/** @return Foo */',
@@ -5652,13 +5652,43 @@ public function dataTypeLinesAndIndexes(): iterable
56525652
'/** @return Foo*/',
56535653
[1, 1, 3, 3],
56545654
];
5655+
5656+
yield [
5657+
'/**
5658+
* @param Foo $foo
5659+
* @return Foo
5660+
*/',
5661+
[3, 3, 10, 10],
5662+
];
5663+
5664+
yield [
5665+
'/**
5666+
* @return Foo
5667+
* @param Foo $foo
5668+
*/',
5669+
[2, 2, 4, 4],
5670+
];
5671+
5672+
yield [
5673+
'/**
5674+
* @param Foo $foo
5675+
* @return Foo */',
5676+
[3, 3, 10, 10],
5677+
];
5678+
5679+
yield [
5680+
'/**
5681+
* @param Foo $foo
5682+
* @return Foo*/',
5683+
[3, 3, 10, 10],
5684+
];
56555685
}
56565686

56575687
/**
5658-
* @dataProvider dataTypeLinesAndIndexes
5688+
* @dataProvider dataReturnTypeLinesAndIndexes
56595689
* @param array{int, int, int, int} $lines
56605690
*/
5661-
public function testTypeLinesAndIndexes(string $phpDoc, array $lines): void
5691+
public function testReturnTypeLinesAndIndexes(string $phpDoc, array $lines): void
56625692
{
56635693
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
56645694
$constExprParser = new ConstExprParser(true, true);
@@ -5669,10 +5699,8 @@ public function testTypeLinesAndIndexes(string $phpDoc, array $lines): void
56695699
$typeParser = new TypeParser($constExprParser, true, $usedAttributes);
56705700
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes);
56715701
$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;
5702+
$returnTag = $phpDocNode->getReturnTagValues()[0];
5703+
$type = $returnTag->type;
56765704
$this->assertInstanceOf(IdentifierTypeNode::class, $type);
56775705

56785706
$this->assertSame($lines[0], $type->getAttribute(Attribute::START_LINE));

tests/PHPStan/Parser/TypeParserTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ static function (TypeNode $typeNode): TypeNode {
19231923
1,
19241924
1,
19251925
0,
1926-
13,
1926+
12,
19271927
],
19281928
[
19291929
static function (UnionTypeNode $typeNode): TypeNode {
@@ -1932,7 +1932,7 @@ static function (UnionTypeNode $typeNode): TypeNode {
19321932
1,
19331933
1,
19341934
0,
1935-
2,
1935+
0,
19361936
],
19371937
[
19381938
static function (UnionTypeNode $typeNode): TypeNode {
@@ -1941,7 +1941,40 @@ static function (UnionTypeNode $typeNode): TypeNode {
19411941
1,
19421942
1,
19431943
4,
1944-
13,
1944+
12,
1945+
],
1946+
],
1947+
];
1948+
1949+
yield [
1950+
'int | object{foo: int}[] ',
1951+
[
1952+
[
1953+
static function (TypeNode $typeNode): TypeNode {
1954+
return $typeNode;
1955+
},
1956+
1,
1957+
1,
1958+
0,
1959+
12,
1960+
],
1961+
[
1962+
static function (UnionTypeNode $typeNode): TypeNode {
1963+
return $typeNode->types[0];
1964+
},
1965+
1,
1966+
1,
1967+
0,
1968+
0,
1969+
],
1970+
[
1971+
static function (UnionTypeNode $typeNode): TypeNode {
1972+
return $typeNode->types[1];
1973+
},
1974+
1,
1975+
1,
1976+
4,
1977+
12,
19451978
],
19461979
],
19471980
];
@@ -1959,7 +1992,7 @@ static function (TypeNode $typeNode): TypeNode {
19591992
1,
19601993
4,
19611994
0,
1962-
15,
1995+
14,
19631996
],
19641997
],
19651998
];

0 commit comments

Comments
 (0)