Skip to content

Commit c3180bf

Browse files
committed
Support array and offset access on const types
1 parent 9bb3855 commit c3180bf

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/Ast/Type/OffsetAccessTypeNode.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function __toString(): string
2525
{
2626
if (
2727
$this->type instanceof CallableTypeNode
28-
|| $this->type instanceof ConstTypeNode
2928
|| $this->type instanceof NullableTypeNode
3029
) {
3130
return '(' . $this->type . ')[' . $this->offset . ']';

src/Parser/TypeParser.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,17 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
232232
);
233233
}
234234

235-
return $this->enrichWithAttributes($tokens, new Ast\Type\ConstTypeNode($constExpr), $startLine, $startIndex);
235+
$type = $this->enrichWithAttributes(
236+
$tokens,
237+
new Ast\Type\ConstTypeNode($constExpr),
238+
$startLine,
239+
$startIndex
240+
);
241+
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
242+
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
243+
}
244+
245+
return $type;
236246
} catch (LogicException $e) {
237247
throw new ParserException(
238248
$currentTokenValue,
@@ -733,14 +743,14 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
733743
);
734744
}
735745

736-
$type = new Ast\Type\ConstTypeNode($constExpr);
746+
$type = $this->enrichWithAttributes(
747+
$tokens,
748+
new Ast\Type\ConstTypeNode($constExpr),
749+
$startLine,
750+
$startIndex
751+
);
737752
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
738-
$type = $this->tryParseArrayOrOffsetAccess($tokens, $this->enrichWithAttributes(
739-
$tokens,
740-
$type,
741-
$startLine,
742-
$startIndex
743-
));
753+
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
744754
}
745755

746756
return $type;

tests/PHPStan/Parser/TypeParserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,13 @@ public function provideParseData(): array
10671067
new IdentifierTypeNode('int')
10681068
),
10691069
],
1070+
[
1071+
'self::TYPES[ int ]',
1072+
new OffsetAccessTypeNode(
1073+
new ConstTypeNode(new ConstFetchNode('self', 'TYPES')),
1074+
new IdentifierTypeNode('int')
1075+
),
1076+
],
10701077
[
10711078
"?\t\xA009", // edge-case with \h
10721079
new NullableTypeNode(

0 commit comments

Comments
 (0)