Skip to content

Commit

Permalink
Merge branch 'feature/tokenizer-bug-php-74-arrow-functions' of https:…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jan 30, 2020
2 parents f8a1cbb + 332e094 commit 0874e3b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1797,9 +1797,9 @@ protected function processAdditional()
}//end if

continue;
} else if ($this->tokens[$i]['code'] === T_FN) {
} else if ($this->tokens[$i]['code'] === T_FN && isset($this->tokens[($i + 1)]) === true) {
// Possible arrow function.
for ($x = ($i + 1); $i < $numTokens; $x++) {
for ($x = ($i + 1); $x < $numTokens; $x++) {
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false
&& $this->tokens[$x]['code'] !== T_BITWISE_AND
) {
Expand All @@ -1808,7 +1808,7 @@ protected function processAdditional()
}
}

if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
$ignore = Util\Tokens::$emptyTokens;
$ignore += [
T_STRING => T_STRING,
Expand Down
4 changes: 4 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ fn(array $a) : array => $a;

/* testTernary */
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';

/* testLiveCoding */
// Intentional parse error. This has to be the last test in the file.
$fn = fn
26 changes: 25 additions & 1 deletion tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,31 @@ public function testTernary()


/**
* Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner.
* Test that the backfill presumes T_FN during live coding, but doesn't set the additional index keys.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testLiveCoding()
{
$tokens = self::$phpcsFile->getTokens();

$token = $this->getTargetToken('/* testLiveCoding */', [T_STRING, T_FN]);
$this->assertSame($tokens[$token]['code'], T_FN, 'Token not tokenized as T_FN');

$this->assertArrayNotHasKey('scope_condition', $tokens[$token], 'Scope condition is set');
$this->assertArrayNotHasKey('scope_opener', $tokens[$token], 'Scope opener is set');
$this->assertArrayNotHasKey('scope_closer', $tokens[$token], 'Scope closer is set');
$this->assertArrayNotHasKey('parenthesis_owner', $tokens[$token], 'Parenthesis owner is set');
$this->assertArrayNotHasKey('parenthesis_opener', $tokens[$token], 'Parenthesis opener is set');
$this->assertArrayNotHasKey('parenthesis_closer', $tokens[$token], 'Parenthesis closer is set');

}//end testLiveCoding()


/**
* Helper function to check that all token keys are correctly set for T_FN tokens.
*
* @param string $token The T_FN token to check.
*
Expand Down

0 comments on commit 0874e3b

Please sign in to comment.