Skip to content

Commit e58efea

Browse files
committed
Fixed bug #3316 : Arrow function not tokenized correctly when using null in union type
1 parent 3540a5d commit e58efea

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/Tokenizers/PHP.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,17 +2100,18 @@ protected function processAdditional()
21002100
if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
21012101
$ignore = Tokens::$emptyTokens;
21022102
$ignore += [
2103-
T_STRING => T_STRING,
2104-
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
2105-
T_NAME_RELATIVE => T_NAME_RELATIVE,
2106-
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
21072103
T_ARRAY => T_ARRAY,
2104+
T_CALLABLE => T_CALLABLE,
21082105
T_COLON => T_COLON,
2106+
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
2107+
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
2108+
T_NAME_RELATIVE => T_NAME_RELATIVE,
2109+
T_NULL => T_NULL,
21092110
T_NULLABLE => T_NULLABLE,
2110-
T_CALLABLE => T_CALLABLE,
21112111
T_PARENT => T_PARENT,
21122112
T_SELF => T_SELF,
21132113
T_STATIC => T_STATIC,
2114+
T_STRING => T_STRING,
21142115
T_TYPE_UNION => T_TYPE_UNION,
21152116
];
21162117

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ $arrowWithUnionReturn = fn($param) : int|float => $param | 10;
9999
/* testTernary */
100100
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';
101101

102+
/* testTernaryWithTypes */
103+
$fn = fn(int|null $a) : array|null => $a ? null : [];
104+
102105
function matchInArrow($x) {
103106
/* testWithMatchValue */
104107
$fn = fn($x) => match(true) {

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,24 @@ public function testTernary()
501501
}//end testTernary()
502502

503503

504+
/**
505+
* Test typed arrow functions used in ternary operators.
506+
*
507+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
508+
*
509+
* @return void
510+
*/
511+
public function testTernaryWithTypes()
512+
{
513+
$tokens = self::$phpcsFile->getTokens();
514+
515+
$token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN);
516+
$this->backfillHelper($token);
517+
$this->scopePositionTestHelper($token, 15, 27);
518+
519+
}//end testTernaryWithTypes()
520+
521+
504522
/**
505523
* Test arrow function returning a match control structure.
506524
*

0 commit comments

Comments
 (0)