Skip to content

Commit 0874e3b

Browse files
committed
Merge branch 'feature/tokenizer-bug-php-74-arrow-functions' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents f8a1cbb + 332e094 commit 0874e3b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/Tokenizers/PHP.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1797,9 +1797,9 @@ protected function processAdditional()
17971797
}//end if
17981798

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

1811-
if ($this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
1811+
if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
18121812
$ignore = Util\Tokens::$emptyTokens;
18131813
$ignore += [
18141814
T_STRING => T_STRING,

tests/Core/Tokenizer/BackfillFnTokenTest.inc

+4
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ fn(array $a) : array => $a;
7272

7373
/* testTernary */
7474
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';
75+
76+
/* testLiveCoding */
77+
// Intentional parse error. This has to be the last test in the file.
78+
$fn = fn

tests/Core/Tokenizer/BackfillFnTokenTest.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,31 @@ public function testTernary()
553553

554554

555555
/**
556-
* Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner.
556+
* Test that the backfill presumes T_FN during live coding, but doesn't set the additional index keys.
557+
*
558+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
559+
*
560+
* @return void
561+
*/
562+
public function testLiveCoding()
563+
{
564+
$tokens = self::$phpcsFile->getTokens();
565+
566+
$token = $this->getTargetToken('/* testLiveCoding */', [T_STRING, T_FN]);
567+
$this->assertSame($tokens[$token]['code'], T_FN, 'Token not tokenized as T_FN');
568+
569+
$this->assertArrayNotHasKey('scope_condition', $tokens[$token], 'Scope condition is set');
570+
$this->assertArrayNotHasKey('scope_opener', $tokens[$token], 'Scope opener is set');
571+
$this->assertArrayNotHasKey('scope_closer', $tokens[$token], 'Scope closer is set');
572+
$this->assertArrayNotHasKey('parenthesis_owner', $tokens[$token], 'Parenthesis owner is set');
573+
$this->assertArrayNotHasKey('parenthesis_opener', $tokens[$token], 'Parenthesis opener is set');
574+
$this->assertArrayNotHasKey('parenthesis_closer', $tokens[$token], 'Parenthesis closer is set');
575+
576+
}//end testLiveCoding()
577+
578+
579+
/**
580+
* Helper function to check that all token keys are correctly set for T_FN tokens.
557581
*
558582
* @param string $token The T_FN token to check.
559583
*

0 commit comments

Comments
 (0)