From 8e41312a9acbe1afc102703141803e1f640471d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 19 Nov 2019 23:54:16 +0000 Subject: [PATCH] Hotfix: PHP 7.4 fn closure tokenizer with ternary Fix #2713 Missing unit test for: ``` $fn = fn ($a) => $a ? fn () : string => 'a' : fn () : string => 'b'; ``` --- src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.inc | 2 ++ src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.php | 5 ++++- src/Tokenizers/PHP.php | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.inc b/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.inc index 07ddec2bfd..f57e07109d 100644 --- a/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.inc @@ -14,3 +14,5 @@ class Example { } $a = fn(?\DateTime $x) : ?\DateTime => $x; + +$b = fn ($b) => $b ? true : false; diff --git a/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.php b/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.php index 35310727d9..27083ce2c1 100644 --- a/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/DisallowInlineIfUnitTest.php @@ -29,7 +29,10 @@ public function getErrorList($testFile='DisallowInlineIfUnitTest.inc') { switch ($testFile) { case 'DisallowInlineIfUnitTest.inc': - return [8 => 1]; + return [ + 8 => 1, + 18 => 1, + ]; break; case 'DisallowInlineIfUnitTest.js': return [1 => 1]; diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index dc7544f266..4e950608e5 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -1083,7 +1083,7 @@ protected function tokenize($string) $newToken['code'] = T_NULLABLE; $newToken['type'] = 'T_NULLABLE'; break; - } else if (in_array($tokenType, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '=', '{', ';'], true) === true) { + } else if (in_array($tokenType, [T_DOUBLE_ARROW, 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; } @@ -1486,7 +1486,7 @@ function return types. We want to keep the parenthesis map clean, } } - if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_USE) { + if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_FN || $tokens[$i][0] === T_USE) { $isReturnType = true; } }//end if