Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 536889f

Browse files
Support array and offset access on const types
1 parent 9bb3855 commit 536889f

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
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;

‎src/Printer/Printer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ final class Printer
141141
CallableTypeNode::class,
142142
UnionTypeNode::class,
143143
IntersectionTypeNode::class,
144-
ConstTypeNode::class,
145144
NullableTypeNode::class,
146145
],
147146
];
@@ -512,7 +511,6 @@ private function printOffsetAccessType(TypeNode $type): string
512511
$type instanceof CallableTypeNode
513512
|| $type instanceof UnionTypeNode
514513
|| $type instanceof IntersectionTypeNode
515-
|| $type instanceof ConstTypeNode
516514
|| $type instanceof NullableTypeNode
517515
) {
518516
return $this->wrapInParentheses($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(

‎tests/PHPStan/Printer/PrinterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,23 @@ public function enterNode(Node $node)
17231723

17241724
},
17251725
];
1726+
1727+
yield [
1728+
'/** @return Foo[abc] */',
1729+
'/** @return self::FOO[abc] */',
1730+
new class extends AbstractNodeVisitor {
1731+
1732+
public function enterNode(Node $node)
1733+
{
1734+
if ($node instanceof ReturnTagValueNode && $node->type instanceof OffsetAccessTypeNode) {
1735+
$node->type->type = new ConstTypeNode(new ConstFetchNode('self', 'FOO'));
1736+
}
1737+
1738+
return $node;
1739+
}
1740+
1741+
},
1742+
];
17261743
}
17271744

17281745
/**
@@ -1862,6 +1879,13 @@ public function dataPrintType(): iterable
18621879
]),
18631880
'Foo|Bar|(Baz|Lorem)',
18641881
];
1882+
yield [
1883+
new OffsetAccessTypeNode(
1884+
new ConstTypeNode(new ConstFetchNode('self', 'TYPES')),
1885+
new IdentifierTypeNode('int')
1886+
),
1887+
'self::TYPES[int]',
1888+
];
18651889
}
18661890

18671891
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /