Skip to content

Commit ba08a82

Browse files
authored
Merge pull request #782 from PHPCSStandards/feature/tokenizer-php-prevent-undefined-array-key-notice
Tokenizer/PHP: prevent an "Undefined array key" notice during live coding
2 parents ab09909 + 73e8089 commit ba08a82

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/Tokenizers/PHP.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,7 +2817,10 @@ protected function processAdditional()
28172817
}
28182818
}
28192819

2820-
if (isset($this->tokens[$x]) === true && $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
2820+
if (isset($this->tokens[$x]) === true
2821+
&& $this->tokens[$x]['code'] === T_OPEN_PARENTHESIS
2822+
&& isset($this->tokens[$x]['parenthesis_closer']) === true
2823+
) {
28212824
$ignore = Tokens::$emptyTokens;
28222825
$ignore += [
28232826
T_ARRAY => T_ARRAY,
@@ -2995,7 +2998,7 @@ protected function processAdditional()
29952998
}//end if
29962999
}//end if
29973000

2998-
// If after all that, the extra tokens are not set, this is not an arrow function.
3001+
// If after all that, the extra tokens are not set, this is not a (valid) arrow function.
29993002
if (isset($this->tokens[$i]['scope_closer']) === false) {
30003003
if (PHP_CODESNIFFER_VERBOSITY > 1) {
30013004
$line = $this->tokens[$i]['line'];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
/* testLiveCoding */
4+
// Intentional parse error. This has to be the only test in the file.
5+
$fn = static fn (string $paramA
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Tests the backfilling of the T_FN token to PHP < 7.4 for a specific parse error.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;
11+
12+
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
13+
14+
final class BackfillFnTokenParseErrorTest extends AbstractTokenizerTestCase
15+
{
16+
17+
18+
/**
19+
* Verify that un unfinished arrow function during live coding doesn't cause a "Undefined array key "parenthesis_closer"" error.
20+
*
21+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
22+
*
23+
* @return void
24+
*/
25+
public function testUnfinishedArrowFunction()
26+
{
27+
$tokens = $this->phpcsFile->getTokens();
28+
29+
$token = $this->getTargetToken('/* testLiveCoding */', [T_STRING, T_FN], 'fn');
30+
$tokenArray = $tokens[$token];
31+
32+
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING');
33+
34+
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
35+
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
36+
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
37+
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
38+
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
39+
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
40+
41+
}//end testUnfinishedArrowFunction()
42+
43+
44+
}//end class

0 commit comments

Comments
 (0)