Skip to content

Commit

Permalink
Fixed bug #2641 : PSR12.Functions.NullableTypeDeclaration false posit…
Browse files Browse the repository at this point in the history
…ive when using new static()
  • Loading branch information
gsherwood committed Oct 7, 2019
1 parent 451a256 commit c328457
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2623 : PSR12.ControlStructures.ControlStructureSpacing not ignoring indentation inside multi-line string arguments
- Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
- Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as file docblocks
- Fixed bug #2641 : PSR12.Functions.NullableTypeDeclaration false positive when using new static()
</notes>
<contents>
<dir name="/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ class TestTokenizingOfNullableVsInlineThen {
$test = Something::one(static::CONSTANT) ?: '';
}
}

$foo = new static(
is_null($a) ? foo($a) : $a,
is_null($b) ? $b : $c
);
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ class TestTokenizingOfNullableVsInlineThen {
$test = Something::one(static::CONSTANT) ?: '';
}
}

$foo = new static(
is_null($a) ? foo($a) : $a,
is_null($b) ? $b : $c
);
18 changes: 17 additions & 1 deletion src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,10 @@ protected function tokenize($string)
$tokenType = $tokens[$i];
}

if ($tokenType === T_STATIC && $lastSeenNonEmpty === T_DOUBLE_COLON) {
if ($tokenType === T_STATIC
&& ($lastSeenNonEmpty === T_DOUBLE_COLON
|| $lastSeenNonEmpty === '(')
) {
$lastSeenNonEmpty = $tokenType;
continue;
}
Expand All @@ -998,6 +1001,11 @@ protected function tokenize($string)
if ($tokenType === ':' || $tokenType === ',') {
$newToken['code'] = T_NULLABLE;
$newToken['type'] = 'T_NULLABLE';

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;
}

break;
}

Expand All @@ -1007,10 +1015,18 @@ protected function tokenize($string)
if ($tokenType === T_FUNCTION
|| isset(Util\Tokens::$methodPrefixes[$tokenType]) === true
) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;
}

$newToken['code'] = T_NULLABLE;
$newToken['type'] = 'T_NULLABLE';
break;
} else if (in_array($tokenType, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '=', '{', ';'], true) === true) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from ? to T_INLINE_THEN".PHP_EOL;
}

$newToken['code'] = T_INLINE_THEN;
$newToken['type'] = 'T_INLINE_THEN';

Expand Down

0 comments on commit c328457

Please sign in to comment.