From c328457e96d0c708ee838216ea8f067f9aec3d1d Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Tue, 8 Oct 2019 08:45:48 +1100 Subject: [PATCH] Fixed bug #2641 : PSR12.Functions.NullableTypeDeclaration false positive when using new static() --- package.xml | 1 + .../NullableTypeDeclarationUnitTest.inc | 5 +++++ .../NullableTypeDeclarationUnitTest.inc.fixed | 5 +++++ src/Tokenizers/PHP.php | 18 +++++++++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package.xml b/package.xml index e79babcc57..4dacbba0ef 100644 --- a/package.xml +++ b/package.xml @@ -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() diff --git a/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc index a303ef0955..d3b580aa14 100644 --- a/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc @@ -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 +); diff --git a/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed index 765ea4364d..85fad47f0c 100644 --- a/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed @@ -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 +); diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index e544f79b90..8ccb212e90 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -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; } @@ -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; } @@ -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';