From b5351f883a62fd85e4fbaafc3a0b7035bc28fab1 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 23 Sep 2020 20:19:40 +0200 Subject: [PATCH] Make keyword emulation check case-insensitive --- .../Lexer/TokenEmulator/KeywordEmulator.php | 2 +- test/PhpParser/Lexer/EmulativeTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php b/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php index 33bf964..e7c0512 100644 --- a/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php +++ b/lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php @@ -9,7 +9,7 @@ abstract class KeywordEmulator extends TokenEmulator public function isEmulationNeeded(string $code): bool { - return strpos($code, $this->getKeywordString()) !== false; + return strpos(strtolower($code), $this->getKeywordString()) !== false; } public function emulate(string $code, array $tokens): array diff --git a/test/PhpParser/Lexer/EmulativeTest.php b/test/PhpParser/Lexer/EmulativeTest.php index d32bb1c..e141b13 100644 --- a/test/PhpParser/Lexer/EmulativeTest.php +++ b/test/PhpParser/Lexer/EmulativeTest.php @@ -24,6 +24,17 @@ class EmulativeTest extends LexerTest $this->assertSame(0, $lexer->getNextToken()); } + /** + * @dataProvider provideTestReplaceKeywords + */ + public function testReplaceKeywordsUppercase($keyword, $expectedToken) { + $lexer = $this->getLexer(); + $lexer->startLexing('assertSame($expectedToken, $lexer->getNextToken()); + $this->assertSame(0, $lexer->getNextToken()); + } + /** * @dataProvider provideTestReplaceKeywords */ @@ -318,8 +329,11 @@ class EmulativeTest extends LexerTest return [ ['8.0', 'match', [[Tokens::T_MATCH, 'match']]], ['7.4', 'match', [[Tokens::T_STRING, 'match']]], + // Keywords are not case-sensitive. ['7.4', 'fn', [[Tokens::T_FN, 'fn']]], + ['7.4', 'FN', [[Tokens::T_FN, 'FN']]], ['7.3', 'fn', [[Tokens::T_STRING, 'fn']]], + ['7.3', 'FN', [[Tokens::T_STRING, 'FN']]], // Tested here to skip testLeaveStuffAloneInStrings. ['8.0', '"$foo?->bar"', [ [ord('"'), '"'],