Skip to content

Commit

Permalink
Support readonly as function name
Browse files Browse the repository at this point in the history
This special case was added after the PHP 8.1 release.
  • Loading branch information
nikic committed May 31, 2022
1 parent 34bea19 commit e727475
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ public function getKeywordToken(): int
{
return \T_READONLY;
}
}

protected function isKeywordContext(array $tokens, int $pos): bool
{
if (!parent::isKeywordContext($tokens, $pos)) {
return false;
}
// Support "function readonly("
return !(isset($tokens[$pos + 1]) &&
($tokens[$pos + 1][0] === '(' ||
($tokens[$pos + 1][0] === \T_WHITESPACE &&
isset($tokens[$pos + 2]) &&
$tokens[$pos + 2][0] === '(')));
}
}
14 changes: 14 additions & 0 deletions test/PhpParser/Lexer/EmulativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,20 @@ public function provideTestLexNewFeatures() {
['0o1000000000000000000000', [
[Tokens::T_DNUMBER, '0o1000000000000000000000'],
]],
['readonly class', [
[Tokens::T_READONLY, 'readonly'],
[Tokens::T_CLASS, 'class'],
]],
['function readonly(', [
[Tokens::T_FUNCTION, 'function'],
[Tokens::T_STRING, 'readonly'],
[ord('('), '('],
]],
['function readonly (', [
[Tokens::T_FUNCTION, 'function'],
[Tokens::T_STRING, 'readonly'],
[ord('('), '('],
]],
];
}

Expand Down

0 comments on commit e727475

Please sign in to comment.