Skip to content

Commit

Permalink
Added arrow function support to findEndOfStatement (ref #2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Nov 10, 2019
1 parent c1d30ce commit bf642b2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/Core/File/FindEndOfStatementTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
16 changes: 16 additions & 0 deletions tests/Core/File/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ fn&($x) => $x;

/* testGrouped */
(fn($x) => $x) + $y;

/* testArrayValue */
$a = [
'a' => fn() => return 1,
];
28 changes: 28 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit bf642b2

Please sign in to comment.