Skip to content

Commit a4be44e

Browse files
valzargamingclaude
andcommitted
TypeParser: read the "|" that follows a "?"
"?A|B" was read as the nullable type "?A", leaving the "|B" for the caller to fail on, so every tag written that way became an error: @Property ?int|null $handler Unexpected token "|", expected variable The "?" is now carried over the union, because "?A|B" is the same set of values whichever of the two the "?" is read to belong to: "(?A)|B" and "?(A|B)" are both "A|B|null". It is read as a union whose first member is "?A", which prints back as it was written. An "&" is deliberately left as it was: "(?A)&B" and "?(A&B)" are not the same set, so "?A&B" stays an error rather than being read as either one. PHP writes that type as "(A&B)|null". The grammars in doc/grammars are updated to say the same, and the union is read at every depth a type may be written at, so "array<?int|null>", "array{a: ?int|null}" and "(?int|string)" are read too. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 1fc50c2 commit a4be44e

3 files changed

Lines changed: 130 additions & 21 deletions

File tree

‎doc/grammars/types.pp3‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
* "int\n|string" is one type while "int\nstring" is a type followed by a
3030
* description. Whichever of the two is written, a "|" or a "&" left unread is
3131
* an error rather than the end of the type, which is what the predicates say.
32+
*
33+
* A "?" written first carries over the "|" that may follow it, because "?A|B" is
34+
* the same set of values whichever of the two the "?" is read to belong to. An
35+
* "&" is not carried over, as "(?A)&B" and "?(A&B)" are not the same set.
3236
*/
3337
Type
34-
: Nullable()
38+
: Nullable() ( NullableTail() | !<T_UNION> !<T_INTERSECTION> )
3539
| Atomic() ( TypeTail() | !<T_UNION> !<T_INTERSECTION> )
3640
;
3741

3842
TypeTail
3943
: Trivia() ( Union() | Intersection() )
4044
;
4145

46+
NullableTail
47+
: Trivia() Union()
48+
;
49+
4250
Union
4351
: ( <T_UNION> Atomic() ( Trivia() &<T_UNION> )? )+ !<T_UNION>
4452
;
@@ -56,7 +64,7 @@ Nullable
5664
* may be written and the only place a line break needs no "|" after it.
5765
*/
5866
SubType
59-
: Nullable()
67+
: Nullable() SubNullableTail()
6068
| <T_VARIABLE> ConditionalForParameter()
6169
| Atomic() ( Conditional() | SubTypeTail() )
6270
;
@@ -65,6 +73,10 @@ SubTypeTail
6573
: Trivia() ( SubUnion() | SubIntersection() )?
6674
;
6775

76+
SubNullableTail
77+
: Trivia() SubUnion()?
78+
;
79+
6880
SubUnion
6981
: ( <T_UNION> Trivia() Atomic() Trivia() )+ !<T_UNION>
7082
;

‎src/Parser/TypeParser.php‎

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,58 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
3434
{
3535
$startLine = $tokens->currentTokenLine();
3636
$startIndex = $tokens->currentTokenIndex();
37-
if ($tokens->isCurrentTokenType(Lexer::TOKEN_NULLABLE)) {
37+
$nullable = $tokens->isCurrentTokenType(Lexer::TOKEN_NULLABLE);
38+
if ($nullable) {
3839
$type = $this->parseNullable($tokens);
3940

4041
} else {
4142
$type = $this->parseAtomic($tokens);
43+
}
4244

43-
$tokens->pushSavePoint();
44-
$tokens->skipNewLineTokensAndConsumeComments();
45+
$tokens->pushSavePoint();
46+
$tokens->skipNewLineTokensAndConsumeComments();
4547

46-
try {
47-
$enrichedType = $this->enrichTypeOnUnionOrIntersection($tokens, $type);
48+
try {
49+
$enrichedType = $this->enrichTypeOnUnionOrIntersection($tokens, $type, $nullable);
4850

49-
} catch (ParserException $parserException) {
50-
$enrichedType = null;
51-
}
51+
} catch (ParserException $parserException) {
52+
$enrichedType = null;
53+
}
5254

53-
if ($enrichedType !== null) {
54-
$type = $enrichedType;
55-
$tokens->dropSavePoint();
55+
if ($enrichedType !== null) {
56+
$type = $enrichedType;
57+
$tokens->dropSavePoint();
5658

57-
} else {
58-
$tokens->rollback();
59-
$type = $this->enrichTypeOnUnionOrIntersection($tokens, $type) ?? $type;
60-
}
59+
} else {
60+
$tokens->rollback();
61+
$type = $this->enrichTypeOnUnionOrIntersection($tokens, $type, $nullable) ?? $type;
6162
}
6263

6364
return $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
6465
}
6566

66-
/** @phpstan-impure */
67-
private function enrichTypeOnUnionOrIntersection(TokenIterator $tokens, Ast\Type\TypeNode $type): ?Ast\Type\TypeNode
67+
/**
68+
* A "?" written before a type carries over the "|" that may follow it, because
69+
* "?A|B" is the same set of values whichever of the two the "?" is read to
70+
* belong to: "(?A)|B" and "?(A|B)" are both "A|B|null".
71+
*
72+
* An "&" is left alone, as "(?A)&B" and "?(A&B)" are not the same set, and
73+
* PHP itself writes the latter as "(A&B)|null" rather than as "?A&B".
74+
*
75+
* @phpstan-impure
76+
*/
77+
private function enrichTypeOnUnionOrIntersection(
78+
TokenIterator $tokens,
79+
Ast\Type\TypeNode $type,
80+
bool $unionOnly = false
81+
): ?Ast\Type\TypeNode
6882
{
6983
if ($tokens->isCurrentTokenType(Lexer::TOKEN_UNION)) {
7084
return $this->parseUnion($tokens, $type);
7185

7286
}
7387

74-
if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTERSECTION)) {
88+
if (!$unionOnly && $tokens->isCurrentTokenType(Lexer::TOKEN_INTERSECTION)) {
7589
return $this->parseIntersection($tokens, $type);
7690
}
7791

@@ -112,7 +126,11 @@ private function subParse(TokenIterator $tokens): Ast\Type\TypeNode
112126

113127
if ($tokens->isCurrentTokenType(Lexer::TOKEN_NULLABLE)) {
114128
$type = $this->parseNullable($tokens);
129+
$tokens->skipNewLineTokensAndConsumeComments();
115130

131+
if ($tokens->isCurrentTokenType(Lexer::TOKEN_UNION)) {
132+
$type = $this->subParseUnion($tokens, $type);
133+
}
116134
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {
117135
$type = $this->parseConditionalForParameter($tokens, $tokens->currentTokenValue());
118136

@@ -395,11 +413,19 @@ private function parseConditionalForParameter(TokenIterator $tokens, string $par
395413
/** @phpstan-impure */
396414
private function parseNullable(TokenIterator $tokens): Ast\Type\TypeNode
397415
{
416+
$startLine = $tokens->currentTokenLine();
417+
$startIndex = $tokens->currentTokenIndex();
418+
398419
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);
399420

400421
$type = $this->parseAtomic($tokens);
401422

402-
return new Ast\Type\NullableTypeNode($type);
423+
return $this->enrichWithAttributes(
424+
$tokens,
425+
new Ast\Type\NullableTypeNode($type),
426+
$startLine,
427+
$startIndex,
428+
);
403429
}
404430

405431
/** @phpstan-impure */

‎tests/PHPStan/Parser/TypeParserTest.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,77 @@ public function provideParseData(): array
546546
new IdentifierTypeNode('int'),
547547
),
548548
],
549+
[
550+
'?int|null',
551+
new UnionTypeNode([
552+
new NullableTypeNode(new IdentifierTypeNode('int')),
553+
new IdentifierTypeNode('null'),
554+
]),
555+
],
556+
[
557+
'?Foo|Bar',
558+
new UnionTypeNode([
559+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
560+
new IdentifierTypeNode('Bar'),
561+
]),
562+
],
563+
[
564+
'?Foo|Bar|Baz',
565+
new UnionTypeNode([
566+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
567+
new IdentifierTypeNode('Bar'),
568+
new IdentifierTypeNode('Baz'),
569+
]),
570+
],
571+
[
572+
"?Foo\n|Bar",
573+
new UnionTypeNode([
574+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
575+
new IdentifierTypeNode('Bar'),
576+
]),
577+
],
578+
[
579+
'(?Foo|Bar)',
580+
new UnionTypeNode([
581+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
582+
new IdentifierTypeNode('Bar'),
583+
]),
584+
],
585+
[
586+
'array<?Foo|null>',
587+
new GenericTypeNode(
588+
new IdentifierTypeNode('array'),
589+
[
590+
new UnionTypeNode([
591+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
592+
new IdentifierTypeNode('null'),
593+
]),
594+
],
595+
[
596+
GenericTypeNode::VARIANCE_INVARIANT,
597+
],
598+
),
599+
],
600+
[
601+
'array{a: ?Foo|null}',
602+
ArrayShapeNode::createSealed([
603+
new ArrayShapeItemNode(
604+
new IdentifierTypeNode('a'),
605+
false,
606+
new UnionTypeNode([
607+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
608+
new IdentifierTypeNode('null'),
609+
]),
610+
),
611+
]),
612+
],
613+
[
614+
// "(?Foo)&Bar" and "?(Foo&Bar)" are different types, so the "&"
615+
// is left for the caller to reject rather than read into either.
616+
'?Foo&Bar',
617+
new NullableTypeNode(new IdentifierTypeNode('Foo')),
618+
Lexer::TOKEN_INTERSECTION,
619+
],
549620
[
550621
'?Foo<Bar>',
551622
new NullableTypeNode(

0 commit comments

Comments
 (0)