Skip to content

Commit b5351f8

Browse files
committed
Make keyword emulation check case-insensitive
1 parent 88be612 commit b5351f8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/PhpParser/Lexer/TokenEmulator/KeywordEmulator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ abstract function getKeywordToken(): int;
99

1010
public function isEmulationNeeded(string $code): bool
1111
{
12-
return strpos($code, $this->getKeywordString()) !== false;
12+
return strpos(strtolower($code), $this->getKeywordString()) !== false;
1313
}
1414

1515
public function emulate(string $code, array $tokens): array

test/PhpParser/Lexer/EmulativeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public function testReplaceKeywords($keyword, $expectedToken) {
2424
$this->assertSame(0, $lexer->getNextToken());
2525
}
2626

27+
/**
28+
* @dataProvider provideTestReplaceKeywords
29+
*/
30+
public function testReplaceKeywordsUppercase($keyword, $expectedToken) {
31+
$lexer = $this->getLexer();
32+
$lexer->startLexing('<?php ' . strtoupper($keyword));
33+
34+
$this->assertSame($expectedToken, $lexer->getNextToken());
35+
$this->assertSame(0, $lexer->getNextToken());
36+
}
37+
2738
/**
2839
* @dataProvider provideTestReplaceKeywords
2940
*/
@@ -318,8 +329,11 @@ public function provideTestTargetVersion() {
318329
return [
319330
['8.0', 'match', [[Tokens::T_MATCH, 'match']]],
320331
['7.4', 'match', [[Tokens::T_STRING, 'match']]],
332+
// Keywords are not case-sensitive.
321333
['7.4', 'fn', [[Tokens::T_FN, 'fn']]],
334+
['7.4', 'FN', [[Tokens::T_FN, 'FN']]],
322335
['7.3', 'fn', [[Tokens::T_STRING, 'fn']]],
336+
['7.3', 'FN', [[Tokens::T_STRING, 'FN']]],
323337
// Tested here to skip testLeaveStuffAloneInStrings.
324338
['8.0', '"$foo?->bar"', [
325339
[ord('"'), '"'],

0 commit comments

Comments
 (0)