Skip to content

Commit bf642b2

Browse files
committed
Added arrow function support to findEndOfStatement (ref #2523)
1 parent c1d30ce commit bf642b2

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

src/Files/File.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,10 @@ public function findEndOfStatement($start, $ignore=null)
23542354
&& ($i === $this->tokens[$i]['scope_opener']
23552355
|| $i === $this->tokens[$i]['scope_condition'])
23562356
) {
2357-
if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
2357+
if ($i === $start
2358+
&& (isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true
2359+
|| $this->tokens[$i]['code'] === T_FN)
2360+
) {
23582361
return $this->tokens[$i]['scope_closer'];
23592362
}
23602363

@@ -2372,7 +2375,7 @@ public function findEndOfStatement($start, $ignore=null)
23722375
if ($end !== false) {
23732376
$i = $end;
23742377
}
2375-
}
2378+
}//end if
23762379

23772380
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
23782381
$lastNotEmpty = $i;

tests/Core/File/FindEndOfStatementTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ $a = new Datetime;
2929

3030
/* testUseGroup */
3131
use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
32+
33+
$a = [
34+
/* testArrowFunctionArrayValue */
35+
'a' => fn() => return 1,
36+
'b' => fn() => return 1,
37+
];

tests/Core/File/FindEndOfStatementTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,20 @@ public function testUseGroup()
172172
}//end testUseGroup()
173173

174174

175+
/**
176+
* Test a use group.
177+
*
178+
* @return void
179+
*/
180+
public function testArrowFunctionArrayValue()
181+
{
182+
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionArrayValue */') + 7);
183+
$found = self::$phpcsFile->findEndOfStatement($start);
184+
185+
$tokens = self::$phpcsFile->getTokens();
186+
$this->assertSame($tokens[($start + 9)], $tokens[$found]);
187+
188+
}//end testArrowFunctionArrayValue()
189+
190+
175191
}//end class

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ fn&($x) => $x;
4646

4747
/* testGrouped */
4848
(fn($x) => $x) + $y;
49+
50+
/* testArrayValue */
51+
$a = [
52+
'a' => fn() => return 1,
53+
];

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,34 @@ public function testGrouped()
375375
}//end testGrouped()
376376

377377

378+
/**
379+
* Test arrow functions that are used as array values.
380+
*
381+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
382+
*
383+
* @return void
384+
*/
385+
public function testArrayValue()
386+
{
387+
$tokens = self::$phpcsFile->getTokens();
388+
389+
$token = $this->getTargetToken('/* testArrayValue */', T_FN);
390+
$this->backfillHelper($token);
391+
392+
$this->assertSame($tokens[$token]['scope_opener'], ($token + 4), 'Scope opener is not the arrow token');
393+
$this->assertSame($tokens[$token]['scope_closer'], ($token + 9), 'Scope closer is not the comma token');
394+
395+
$opener = $tokens[$token]['scope_opener'];
396+
$this->assertSame($tokens[$opener]['scope_opener'], ($token + 4), 'Opener scope opener is not the arrow token');
397+
$this->assertSame($tokens[$opener]['scope_closer'], ($token + 9), 'Opener scope closer is not the comma token');
398+
399+
$closer = $tokens[$token]['scope_opener'];
400+
$this->assertSame($tokens[$closer]['scope_opener'], ($token + 4), 'Closer scope opener is not the arrow token');
401+
$this->assertSame($tokens[$closer]['scope_closer'], ($token + 9), 'Closer scope closer is not the comma token');
402+
403+
}//end testArrayValue()
404+
405+
378406
/**
379407
* Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner.
380408
*

0 commit comments

Comments
 (0)