Skip to content

Commit

Permalink
Fixed bug #3316 : Arrow function not tokenized correctly when using n…
Browse files Browse the repository at this point in the history
…ull in union type
  • Loading branch information
gsherwood committed Apr 23, 2021
1 parent ae4f33b commit 41a9bef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.xml
Expand Up @@ -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
</notes>
<contents>
<dir name="/">
Expand Down
5 changes: 3 additions & 2 deletions src/Tokenizers/PHP.php
Expand Up @@ -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,
];

Expand Down
3 changes: 3 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 41a9bef

Please sign in to comment.