Skip to content

Commit db43214

Browse files
committed
Tokenizer: fix handling of namespace operator in types for arrow functions
The `namespace` keyword as an operator is perfectly valid to be used as part of a type declaration. This usage was so far not supported in the tokenizer for the arrow function backfill. Fixed now.
1 parent 1c59b29 commit db43214

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,7 @@ protected function processAdditional()
18981898
T_STRING => T_STRING,
18991899
T_ARRAY => T_ARRAY,
19001900
T_COLON => T_COLON,
1901+
T_NAMESPACE => T_NAMESPACE,
19011902
T_NS_SEPARATOR => T_NS_SEPARATOR,
19021903
T_NULLABLE => T_NULLABLE,
19031904
T_CALLABLE => T_CALLABLE,

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ $a = fn($x) => yield 'k' => $x;
6363
/* testNullableNamespace */
6464
$a = fn(?\DateTime $x) : ?\DateTime => $x;
6565

66+
/* testNamespaceOperatorInTypes */
67+
$fn = fn(namespace\Foo $a) : ?namespace\Foo => $a;
68+
6669
/* testSelfReturnType */
6770
fn(self $a) : self => $a;
6871

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,34 @@ public function testNullableNamespace()
465465
}//end testNullableNamespace()
466466

467467

468+
/**
469+
* Test arrow functions that use the namespace operator in the return type.
470+
*
471+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
472+
*
473+
* @return void
474+
*/
475+
public function testNamespaceOperatorInTypes()
476+
{
477+
$tokens = self::$phpcsFile->getTokens();
478+
479+
$token = $this->getTargetToken('/* testNamespaceOperatorInTypes */', T_FN);
480+
$this->backfillHelper($token);
481+
482+
$this->assertSame($tokens[$token]['scope_opener'], ($token + 16), 'Scope opener is not the arrow token');
483+
$this->assertSame($tokens[$token]['scope_closer'], ($token + 19), 'Scope closer is not the semicolon token');
484+
485+
$opener = $tokens[$token]['scope_opener'];
486+
$this->assertSame($tokens[$opener]['scope_opener'], ($token + 16), 'Opener scope opener is not the arrow token');
487+
$this->assertSame($tokens[$opener]['scope_closer'], ($token + 19), 'Opener scope closer is not the semicolon token');
488+
489+
$closer = $tokens[$token]['scope_closer'];
490+
$this->assertSame($tokens[$closer]['scope_opener'], ($token + 16), 'Closer scope opener is not the arrow token');
491+
$this->assertSame($tokens[$closer]['scope_closer'], ($token + 19), 'Closer scope closer is not the semicolon token');
492+
493+
}//end testNamespaceOperatorInTypes()
494+
495+
468496
/**
469497
* Test arrow functions that use self/parent/callable/array/static return types.
470498
*

0 commit comments

Comments
 (0)