Skip to content

Commit

Permalink
PHP 8.3 | Tokenizer/PHP: add support for readonly anonymous classes
Browse files Browse the repository at this point in the history
PHP 8.3 introduced readonly anonymous classes, fixing an oversight in the PHP 8.2 introduction of readonly classes.

As things were, for PHP 8.1+, the tokenizer would change the token code for the `readonly` keyword from `T_READONLY` to `T_STRING` in the "context sensitive keyword" layer, thinking it to be a class name.

And for PHP < 8.1, the readonly polyfill would ignore the token as it being preceded by the `new` keyword would be seen as conflicting with the "context sensitive keyword" layer, which meant it would not be re-tokenized from `T_STRING` to `T_READONLY`.

This commit fixes both.

Includes adding tests in a number of pre-existing test classes to cover this change.
  • Loading branch information
jrfnl committed Jan 28, 2024
1 parent 529cd87 commit 5e9de65
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
18 changes: 17 additions & 1 deletion src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ protected function tokenize($string)
$preserveKeyword = true;
}

// `new readonly class` should be preserved.
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW) {
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === false
|| isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === false
) {
break;
}
}

if (is_array($tokens[$i]) === true && $tokens[$i][0] === T_CLASS) {
$preserveKeyword = true;
}
}

// `new class extends` `new class implements` should be preserved
if (($token[0] === T_EXTENDS || $token[0] === T_IMPLEMENTS)
&& $finalTokens[$lastNotEmptyToken]['code'] === T_CLASS
Expand Down Expand Up @@ -1315,7 +1330,8 @@ protected function tokenize($string)

if ($tokenIsArray === true
&& strtolower($token[1]) === 'readonly'
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
&& (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
|| $finalTokens[$lastNotEmptyToken]['code'] === T_NEW)
) {
// Get the next non-whitespace token.
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ $anonClass = new class {
function __construct() {}
};

/* testReadonlyNoParentheses */
$anonClass = new readonly class {
function __construct() {}
};

/* testNoParenthesesAndEmptyTokens */
$anonClass = new class // phpcs:ignore Standard.Cat
{
Expand All @@ -14,6 +19,11 @@ $anonClass = new class // phpcs:ignore Standard.Cat
/* testWithParentheses */
$anonClass = new class() {};

/* testReadonlyWithParentheses */
$anonClass = new readonly class() {
function __construct() {}
};

/* testWithParenthesesAndEmptyTokens */
$anonClass = new class /*comment */
() {};
6 changes: 6 additions & 0 deletions tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static function dataAnonClassNoParentheses()
'plain' => [
'testMarker' => '/* testNoParentheses */',
],
'readonly' => [
'testMarker' => '/* testReadonlyNoParentheses */',
],
'declaration contains comments and extra whitespace' => [
'testMarker' => '/* testNoParenthesesAndEmptyTokens */',
],
Expand Down Expand Up @@ -141,6 +144,9 @@ public static function dataAnonClassWithParentheses()
'plain' => [
'testMarker' => '/* testWithParentheses */',
],
'readonly' => [
'testMarker' => '/* testReadonlyWithParentheses */',
],
'declaration contains comments and extra whitespace' => [
'testMarker' => '/* testWithParenthesesAndEmptyTokens */',
],
Expand Down
13 changes: 13 additions & 0 deletions tests/Core/Tokenizer/BackfillReadonlyTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ class ReadonlyWithDisjunctiveNormalForm
public function readonly (A&B $param): void {}
}

/* testReadonlyAnonClassWithParens */
$anon = new readonly class() {};

/* testReadonlyAnonClassWithoutParens */
$anon = new Readonly class {};

/* testReadonlyAnonClassWithCommentsAndWhitespace */
$anon = new
// comment
ReadOnly
// phpcs:ignore Stnd.Cat.Sniff
class {};

/* testParseErrorLiveCoding */
// This must be the last test in the file.
readonly
11 changes: 11 additions & 0 deletions tests/Core/Tokenizer/BackfillReadonlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ public static function dataReadonly()
'property declaration, constructor property promotion, DNF type and reference' => [
'testMarker' => '/* testReadonlyConstructorPropertyPromotionWithDNFAndReference */',
],
'anon class declaration, with parentheses' => [
'testMarker' => '/* testReadonlyAnonClassWithParens */',
],
'anon class declaration, without parentheses' => [
'testMarker' => '/* testReadonlyAnonClassWithoutParens */',
'testContent' => 'Readonly',
],
'anon class declaration, with comments and whitespace' => [
'testMarker' => '/* testReadonlyAnonClassWithCommentsAndWhitespace */',
'testContent' => 'ReadOnly',
],
'live coding / parse error' => [
'testMarker' => '/* testParseErrorLiveCoding */',
],
Expand Down
6 changes: 5 additions & 1 deletion tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace /* testNamespaceNameIsString1 */ my\ /* testNamespaceNameIsString2 */
/* testVarIsKeyword */ var $var;
/* testStaticIsKeyword */ static $static;

/* testReadonlyIsKeyword */ readonly $readonly;
/* testReadonlyIsKeywordForProperty */ readonly $readonly;

/* testFinalIsKeyword */ final /* testFunctionIsKeyword */ function someFunction(
/* testCallableIsKeyword */
Expand All @@ -115,6 +115,10 @@ namespace /* testNamespaceNameIsString1 */ my\ /* testNamespaceNameIsString2 */
/* testParentIsKeyword */
parent $parent
) {
$anon = new /* testReadonlyIsKeywordForAnonClass */ readonly class() {
public function foo() {}
};

/* testReturnIsKeyword */
return $this;
}
Expand Down
11 changes: 9 additions & 2 deletions tests/Core/Tokenizer/ContextSensitiveKeywordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function testStrings($testMarker)
{
$tokens = self::$phpcsFile->getTokens();

$token = $this->getTargetToken($testMarker, (Tokens::$contextSensitiveKeywords + [T_STRING, T_NULL, T_FALSE, T_TRUE, T_PARENT, T_SELF]));
$token = $this->getTargetToken(
$testMarker,
(Tokens::$contextSensitiveKeywords + [T_ANON_CLASS, T_MATCH_DEFAULT, T_PARENT, T_SELF, T_STRING, T_NULL, T_FALSE, T_TRUE])
);

$this->assertSame(T_STRING, $tokens[$token]['code']);
$this->assertSame('T_STRING', $tokens[$token]['type']);
Expand Down Expand Up @@ -247,7 +250,7 @@ public static function dataKeywords()
'expectedTokenType' => 'T_STATIC',
],
'readonly: property declaration' => [
'testMarker' => '/* testReadonlyIsKeyword */',
'testMarker' => '/* testReadonlyIsKeywordForProperty */',
'expectedTokenType' => 'T_READONLY',
],
'final: function declaration' => [
Expand All @@ -270,6 +273,10 @@ public static function dataKeywords()
'testMarker' => '/* testParentIsKeyword */',
'expectedTokenType' => 'T_PARENT',
],
'readonly: anon class declaration' => [
'testMarker' => '/* testReadonlyIsKeywordForAnonClass */',
'expectedTokenType' => 'T_READONLY',
],
'return: statement' => [
'testMarker' => '/* testReturnIsKeyword */',
'expectedTokenType' => 'T_RETURN',
Expand Down

0 comments on commit 5e9de65

Please sign in to comment.