Skip to content

Commit

Permalink
Tokenizer/PHP: fix ? tokenization after attribute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Oct 6, 2021
1 parent f619f0b commit 9362d14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc
Expand Up @@ -63,3 +63,15 @@ $fn = fn(array & $one) => 1;
$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

function issue3267(string|int ...$values) {}

function setDefault(#[ImportValue(
constraints: [
[
Assert\Type::class,
['type' => 'bool'],
],
]
)] ?bool $value = null): void
{
// Do something
}
Expand Up @@ -63,3 +63,15 @@ $fn = fn(array & $one) => 1;
$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

function issue3267(string|int ...$values) {}

function setDefault(#[ImportValue(
constraints: [
[
Assert\Type::class,
['type' => 'bool'],
],
]
)] ?bool $value = null): void
{
// Do something
}
2 changes: 1 addition & 1 deletion src/Tokenizers/PHP.php
Expand Up @@ -1576,7 +1576,7 @@ protected function tokenize($string)
&& isset(Util\Tokens::$emptyTokens[$tokenType]) === false
) {
// Found the previous non-empty token.
if ($tokenType === ':' || $tokenType === ',') {
if ($tokenType === ':' || $tokenType === ',' || $tokenType === T_ATTRIBUTE_END) {
$newToken['code'] = T_NULLABLE;
$newToken['type'] = 'T_NULLABLE';

Expand Down

0 comments on commit 9362d14

Please sign in to comment.