Skip to content

Commit

Permalink
Tokenizer: fix handling of namespace operator in types for arrow func…
Browse files Browse the repository at this point in the history
…tions

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.
  • Loading branch information
jrfnl committed Sep 2, 2020
1 parent 1c59b29 commit db43214
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,7 @@ protected function processAdditional()
T_STRING => T_STRING,
T_ARRAY => T_ARRAY,
T_COLON => T_COLON,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_NULLABLE => T_NULLABLE,
T_CALLABLE => T_CALLABLE,
Expand Down
3 changes: 3 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ $a = fn($x) => yield 'k' => $x;
/* testNullableNamespace */
$a = fn(?\DateTime $x) : ?\DateTime => $x;

/* testNamespaceOperatorInTypes */
$fn = fn(namespace\Foo $a) : ?namespace\Foo => $a;

/* testSelfReturnType */
fn(self $a) : self => $a;

Expand Down
28 changes: 28 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,34 @@ public function testNullableNamespace()
}//end testNullableNamespace()


/**
* Test arrow functions that use the namespace operator in the return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNamespaceOperatorInTypes()
{
$tokens = self::$phpcsFile->getTokens();

$token = $this->getTargetToken('/* testNamespaceOperatorInTypes */', T_FN);
$this->backfillHelper($token);

$this->assertSame($tokens[$token]['scope_opener'], ($token + 16), 'Scope opener is not the arrow token');
$this->assertSame($tokens[$token]['scope_closer'], ($token + 19), 'Scope closer is not the semicolon token');

$opener = $tokens[$token]['scope_opener'];
$this->assertSame($tokens[$opener]['scope_opener'], ($token + 16), 'Opener scope opener is not the arrow token');
$this->assertSame($tokens[$opener]['scope_closer'], ($token + 19), 'Opener scope closer is not the semicolon token');

$closer = $tokens[$token]['scope_closer'];
$this->assertSame($tokens[$closer]['scope_opener'], ($token + 16), 'Closer scope opener is not the arrow token');
$this->assertSame($tokens[$closer]['scope_closer'], ($token + 19), 'Closer scope closer is not the semicolon token');

}//end testNamespaceOperatorInTypes()


/**
* Test arrow functions that use self/parent/callable/array/static return types.
*
Expand Down

0 comments on commit db43214

Please sign in to comment.