From db43214122992786b97d329c6394bd3d860b1ff2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 31 Aug 2020 00:56:56 +0200 Subject: [PATCH] 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. --- src/Tokenizers/PHP.php | 1 + tests/Core/Tokenizer/BackfillFnTokenTest.inc | 3 +++ tests/Core/Tokenizer/BackfillFnTokenTest.php | 28 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index 085d74990a..898536d728 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -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, diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.inc b/tests/Core/Tokenizer/BackfillFnTokenTest.inc index 083fe6979c..46c165a2d2 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.inc +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.inc @@ -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; diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.php b/tests/Core/Tokenizer/BackfillFnTokenTest.php index 55d378333f..9ef8dad442 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.php +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.php @@ -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. *