Skip to content

Commit 6c04009

Browse files
committed
Fix ArrayTypeNode indexes
1 parent d985f89 commit 6c04009

File tree

2 files changed

+177
-14
lines changed

2 files changed

+177
-14
lines changed

src/Parser/TypeParser.php

+22-14
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ private function tryParseCallable(TokenIterator $tokens, Ast\Type\IdentifierType
585585
/** @phpstan-impure */
586586
private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode
587587
{
588-
$startLine = $tokens->currentTokenLine();
589-
$startIndex = $tokens->currentTokenIndex();
588+
$startLine = $type->getAttribute(Ast\Attribute::START_LINE);
589+
$startIndex = $type->getAttribute(Ast\Attribute::START_INDEX);
590590
try {
591591
while ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
592592
$tokens->pushSavePoint();
@@ -598,21 +598,29 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ
598598
$offset = $this->parse($tokens);
599599
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_SQUARE_BRACKET);
600600
$tokens->dropSavePoint();
601-
$type = $this->enrichWithAttributes(
602-
$tokens,
603-
new Ast\Type\OffsetAccessTypeNode($type, $offset),
604-
$startLine,
605-
$startIndex
606-
);
601+
$type = new Ast\Type\OffsetAccessTypeNode($type, $offset);
602+
603+
if ($startLine !== null && $startIndex !== null) {
604+
$type = $this->enrichWithAttributes(
605+
$tokens,
606+
$type,
607+
$startLine,
608+
$startIndex
609+
);
610+
}
607611
} else {
608612
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_SQUARE_BRACKET);
609613
$tokens->dropSavePoint();
610-
$type = $this->enrichWithAttributes(
611-
$tokens,
612-
new Ast\Type\ArrayTypeNode($type),
613-
$startLine,
614-
$startIndex
615-
);
614+
$type = new Ast\Type\ArrayTypeNode($type);
615+
616+
if ($startLine !== null && $startIndex !== null) {
617+
$type = $this->enrichWithAttributes(
618+
$tokens,
619+
$type,
620+
$startLine,
621+
$startIndex
622+
);
623+
}
616624
}
617625
}
618626

tests/PHPStan/Parser/TypeParserTest.php

+155
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,161 @@ static function (TypeNode $typeNode): TypeNode {
22022202
],
22032203
],
22042204
];
2205+
2206+
yield [
2207+
'int[][][]',
2208+
[
2209+
[
2210+
static function (TypeNode $typeNode): TypeNode {
2211+
return $typeNode;
2212+
},
2213+
'int[][][]',
2214+
1,
2215+
1,
2216+
0,
2217+
6,
2218+
],
2219+
[
2220+
static function (ArrayTypeNode $typeNode): TypeNode {
2221+
return $typeNode->type;
2222+
},
2223+
'int[][]',
2224+
1,
2225+
1,
2226+
0,
2227+
4,
2228+
],
2229+
[
2230+
static function (ArrayTypeNode $typeNode): TypeNode {
2231+
if (!$typeNode->type instanceof ArrayTypeNode) {
2232+
throw new Exception();
2233+
}
2234+
2235+
return $typeNode->type->type;
2236+
},
2237+
'int[]',
2238+
1,
2239+
1,
2240+
0,
2241+
2,
2242+
],
2243+
[
2244+
static function (ArrayTypeNode $typeNode): TypeNode {
2245+
if (!$typeNode->type instanceof ArrayTypeNode) {
2246+
throw new Exception();
2247+
}
2248+
if (!$typeNode->type->type instanceof ArrayTypeNode) {
2249+
throw new Exception();
2250+
}
2251+
2252+
return $typeNode->type->type->type;
2253+
},
2254+
'int',
2255+
1,
2256+
1,
2257+
0,
2258+
0,
2259+
],
2260+
],
2261+
];
2262+
2263+
yield [
2264+
'int[foo][bar][baz]',
2265+
[
2266+
[
2267+
static function (TypeNode $typeNode): TypeNode {
2268+
return $typeNode;
2269+
},
2270+
'int[foo][bar][baz]',
2271+
1,
2272+
1,
2273+
0,
2274+
9,
2275+
],
2276+
[
2277+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2278+
return $typeNode->type;
2279+
},
2280+
'int[foo][bar]',
2281+
1,
2282+
1,
2283+
0,
2284+
6,
2285+
],
2286+
[
2287+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2288+
return $typeNode->offset;
2289+
},
2290+
'baz',
2291+
1,
2292+
1,
2293+
8,
2294+
8,
2295+
],
2296+
[
2297+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2298+
if (!$typeNode->type instanceof OffsetAccessTypeNode) {
2299+
throw new Exception();
2300+
}
2301+
2302+
return $typeNode->type->type;
2303+
},
2304+
'int[foo]',
2305+
1,
2306+
1,
2307+
0,
2308+
3,
2309+
],
2310+
[
2311+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2312+
if (!$typeNode->type instanceof OffsetAccessTypeNode) {
2313+
throw new Exception();
2314+
}
2315+
2316+
return $typeNode->type->offset;
2317+
},
2318+
'bar',
2319+
1,
2320+
1,
2321+
5,
2322+
5,
2323+
],
2324+
[
2325+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2326+
if (!$typeNode->type instanceof OffsetAccessTypeNode) {
2327+
throw new Exception();
2328+
}
2329+
if (!$typeNode->type->type instanceof OffsetAccessTypeNode) {
2330+
throw new Exception();
2331+
}
2332+
2333+
return $typeNode->type->type->type;
2334+
},
2335+
'int',
2336+
1,
2337+
1,
2338+
0,
2339+
0,
2340+
],
2341+
[
2342+
static function (OffsetAccessTypeNode $typeNode): TypeNode {
2343+
if (!$typeNode->type instanceof OffsetAccessTypeNode) {
2344+
throw new Exception();
2345+
}
2346+
if (!$typeNode->type->type instanceof OffsetAccessTypeNode) {
2347+
throw new Exception();
2348+
}
2349+
2350+
return $typeNode->type->type->offset;
2351+
},
2352+
'foo',
2353+
1,
2354+
1,
2355+
2,
2356+
2,
2357+
],
2358+
],
2359+
];
22052360
}
22062361

22072362
/**

0 commit comments

Comments
 (0)