diff --git a/package.xml b/package.xml index c408f94353..ca6aa28da2 100644 --- a/package.xml +++ b/package.xml @@ -31,6 +31,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Thanks to Juliette Reinders Folmer for the tests - Fixed bug #3296 : PSR2.ControlStructures.SwitchDeclaration takes phpcs:ignore as content of case body - Fixed bug #3303 : findStartOfStatement() doesn't work with T_OPEN_TAG_WITH_ECHO + - Fixed bug #3316 : Arrow function not tokenized correctly when using null in union type diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index 3d8cee1657..debef98d13 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -2276,16 +2276,17 @@ protected function processAdditional() if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) { $ignore = Util\Tokens::$emptyTokens; $ignore += [ - T_STRING => T_STRING, T_ARRAY => T_ARRAY, + T_CALLABLE => T_CALLABLE, T_COLON => T_COLON, T_NAMESPACE => T_NAMESPACE, T_NS_SEPARATOR => T_NS_SEPARATOR, + T_NULL => T_NULL, T_NULLABLE => T_NULLABLE, - T_CALLABLE => T_CALLABLE, T_PARENT => T_PARENT, T_SELF => T_SELF, T_STATIC => T_STATIC, + T_STRING => T_STRING, T_TYPE_UNION => T_TYPE_UNION, ]; diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.inc b/tests/Core/Tokenizer/BackfillFnTokenTest.inc index 72cb244200..56e8c29c3d 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.inc +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.inc @@ -93,6 +93,9 @@ $arrowWithUnionReturn = fn($param) : int|float => $param | 10; /* testTernary */ $fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b'; +/* testTernaryWithTypes */ +$fn = fn(int|null $a) : array|null => $a ? null : []; + function matchInArrow($x) { /* testWithMatchValue */ $fn = fn($x) => match(true) { diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.php b/tests/Core/Tokenizer/BackfillFnTokenTest.php index 6404026c34..76b39819d2 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.php +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.php @@ -445,6 +445,24 @@ public function testTernary() }//end testTernary() + /** + * Test typed arrow functions used in ternary operators. + * + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional + * + * @return void + */ + public function testTernaryWithTypes() + { + $tokens = self::$phpcsFile->getTokens(); + + $token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN); + $this->backfillHelper($token); + $this->scopePositionTestHelper($token, 15, 27); + + }//end testTernaryWithTypes() + + /** * Test arrow function returning a match control structure. *