From bf642b2a244acdb26681d0f4f3f275d5c8125468 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 11 Nov 2019 08:11:30 +1100 Subject: [PATCH] Added arrow function support to findEndOfStatement (ref #2523) --- src/Files/File.php | 7 +++-- tests/Core/File/FindEndOfStatementTest.inc | 6 +++++ tests/Core/File/FindEndOfStatementTest.php | 16 +++++++++++ tests/Core/Tokenizer/BackfillFnTokenTest.inc | 5 ++++ tests/Core/Tokenizer/BackfillFnTokenTest.php | 28 ++++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/Files/File.php b/src/Files/File.php index 505eea13e5..bf50998048 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -2354,7 +2354,10 @@ public function findEndOfStatement($start, $ignore=null) && ($i === $this->tokens[$i]['scope_opener'] || $i === $this->tokens[$i]['scope_condition']) ) { - if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) { + if ($i === $start + && (isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true + || $this->tokens[$i]['code'] === T_FN) + ) { return $this->tokens[$i]['scope_closer']; } @@ -2372,7 +2375,7 @@ public function findEndOfStatement($start, $ignore=null) if ($end !== false) { $i = $end; } - } + }//end if if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) { $lastNotEmpty = $i; diff --git a/tests/Core/File/FindEndOfStatementTest.inc b/tests/Core/File/FindEndOfStatementTest.inc index f8763c6a69..8ff59d4211 100644 --- a/tests/Core/File/FindEndOfStatementTest.inc +++ b/tests/Core/File/FindEndOfStatementTest.inc @@ -29,3 +29,9 @@ $a = new Datetime; /* testUseGroup */ use Vendor\Package\{ClassA as A, ClassB, ClassC as C}; + +$a = [ + /* testArrowFunctionArrayValue */ + 'a' => fn() => return 1, + 'b' => fn() => return 1, +]; diff --git a/tests/Core/File/FindEndOfStatementTest.php b/tests/Core/File/FindEndOfStatementTest.php index e4096c17aa..5ed3fb3874 100644 --- a/tests/Core/File/FindEndOfStatementTest.php +++ b/tests/Core/File/FindEndOfStatementTest.php @@ -172,4 +172,20 @@ public function testUseGroup() }//end testUseGroup() + /** + * Test a use group. + * + * @return void + */ + public function testArrowFunctionArrayValue() + { + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionArrayValue */') + 7); + $found = self::$phpcsFile->findEndOfStatement($start); + + $tokens = self::$phpcsFile->getTokens(); + $this->assertSame($tokens[($start + 9)], $tokens[$found]); + + }//end testArrowFunctionArrayValue() + + }//end class diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.inc b/tests/Core/Tokenizer/BackfillFnTokenTest.inc index 9b1e660fc7..599ff3773e 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.inc +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.inc @@ -46,3 +46,8 @@ fn&($x) => $x; /* testGrouped */ (fn($x) => $x) + $y; + +/* testArrayValue */ +$a = [ + 'a' => fn() => return 1, +]; diff --git a/tests/Core/Tokenizer/BackfillFnTokenTest.php b/tests/Core/Tokenizer/BackfillFnTokenTest.php index ca18dbbafe..dfa439c197 100644 --- a/tests/Core/Tokenizer/BackfillFnTokenTest.php +++ b/tests/Core/Tokenizer/BackfillFnTokenTest.php @@ -375,6 +375,34 @@ public function testGrouped() }//end testGrouped() + /** + * Test arrow functions that are used as array values. + * + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional + * + * @return void + */ + public function testArrayValue() + { + $tokens = self::$phpcsFile->getTokens(); + + $token = $this->getTargetToken('/* testArrayValue */', T_FN); + $this->backfillHelper($token); + + $this->assertSame($tokens[$token]['scope_opener'], ($token + 4), 'Scope opener is not the arrow token'); + $this->assertSame($tokens[$token]['scope_closer'], ($token + 9), 'Scope closer is not the comma token'); + + $opener = $tokens[$token]['scope_opener']; + $this->assertSame($tokens[$opener]['scope_opener'], ($token + 4), 'Opener scope opener is not the arrow token'); + $this->assertSame($tokens[$opener]['scope_closer'], ($token + 9), 'Opener scope closer is not the comma token'); + + $closer = $tokens[$token]['scope_opener']; + $this->assertSame($tokens[$closer]['scope_opener'], ($token + 4), 'Closer scope opener is not the arrow token'); + $this->assertSame($tokens[$closer]['scope_closer'], ($token + 9), 'Closer scope closer is not the comma token'); + + }//end testArrayValue() + + /** * Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner. *