-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tokenizer/PHP: prevent an "Undefined array key" notice during live co…
…ding During live coding or for code containing a parse error, it was possible for the tokenizer to run into an `Undefined array key "parenthesis_closer"` error when trying to verify arrow functions. As this error happens during the tokenization, this resulted in PHPCS silently not scanning the file - without even showing the error notice. Fixed now. Includes tests.
- Loading branch information
Showing
3 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
/* testLiveCoding */ | ||
// Intentional parse error. This has to be the only test in the file. | ||
$fn = static fn (string $paramA |
44 changes: 44 additions & 0 deletions
44
tests/Core/Tokenizers/PHP/BackfillFnTokenParseErrorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Tests the backfilling of the T_FN token to PHP < 7.4 for a specific parse error. | ||
* | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
* @copyright 2024 PHPCSStandards and contributors | ||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP; | ||
|
||
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase; | ||
|
||
final class BackfillFnTokenParseErrorTest extends AbstractTokenizerTestCase | ||
{ | ||
|
||
|
||
/** | ||
* Verify that un unfinished arrow function during live coding doesn't cause a "Undefined array key "parenthesis_closer"" error. | ||
* | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional | ||
* | ||
* @return void | ||
*/ | ||
public function testUnfinishedArrowFunction() | ||
{ | ||
$tokens = $this->phpcsFile->getTokens(); | ||
|
||
$token = $this->getTargetToken('/* testLiveCoding */', [T_STRING, T_FN], 'fn'); | ||
$tokenArray = $tokens[$token]; | ||
|
||
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING'); | ||
|
||
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set'); | ||
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set'); | ||
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set'); | ||
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set'); | ||
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set'); | ||
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set'); | ||
|
||
}//end testUnfinishedArrowFunction() | ||
|
||
|
||
}//end class |