Skip to content

Commit 9362d14

Browse files
committed
Tokenizer/PHP: fix ? tokenization after attribute
In a select set of circumstance, when a `?` would follow an attribute with a nested array in it, the `?` would be tokenized as `T_INLINE_THEN`, not `T_NULLABLE`. Fixed now. Includes unit test via the `PSR12.Operators.OperatorSpacing` sniff. Note: Ternary `?` vs nullable (and ternary `:` vs colon) should really get a full set of unit tests, but I don't currently have the time to set that up. Fixes 3445
1 parent f619f0b commit 9362d14

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc

+12
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ $fn = fn(array & $one) => 1;
6363
$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());
6464

6565
function issue3267(string|int ...$values) {}
66+
67+
function setDefault(#[ImportValue(
68+
constraints: [
69+
[
70+
Assert\Type::class,
71+
['type' => 'bool'],
72+
],
73+
]
74+
)] ?bool $value = null): void
75+
{
76+
// Do something
77+
}

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ $fn = fn(array & $one) => 1;
6363
$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());
6464

6565
function issue3267(string|int ...$values) {}
66+
67+
function setDefault(#[ImportValue(
68+
constraints: [
69+
[
70+
Assert\Type::class,
71+
['type' => 'bool'],
72+
],
73+
]
74+
)] ?bool $value = null): void
75+
{
76+
// Do something
77+
}

src/Tokenizers/PHP.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ protected function tokenize($string)
15761576
&& isset(Util\Tokens::$emptyTokens[$tokenType]) === false
15771577
) {
15781578
// Found the previous non-empty token.
1579-
if ($tokenType === ':' || $tokenType === ',') {
1579+
if ($tokenType === ':' || $tokenType === ',' || $tokenType === T_ATTRIBUTE_END) {
15801580
$newToken['code'] = T_NULLABLE;
15811581
$newToken['type'] = 'T_NULLABLE';
15821582

0 commit comments

Comments
 (0)