Skip to content

PHP 8.4 | Tokenizer/PHP: allow for anon class dereferencing #1160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 7, 2025

Description

As of PHP 8.4, new expressions can be dereferenced without needing parentheses wrappers, as long as they have the parentheses for the constructor arguments.

echo new MyClass()->property;
echo new $myClass(['value'])[0];

Additionally, new expressions involving anonymous classes can also be dereferenced directly and these do not have the constructor argument parentheses requirement.

echo new class { public $property = 'property'; }->property;
echo new class (['value']) extends ArrayObject {}[0];

For the most part, this has no impact on the PHPCS tokenizer layer, however, dereferencing the result of a new anonymous class expression using array access runs into trouble with the short array retokenization. I.e. the square brackets in the below example were tokenized as T_OPEN|CLOSE_SHORT_ARRAY, while they should be tokenized as T_OPEN|CLOSE_SQUARE_BRACKET.

echo new class {}[0];

Fixed now. Includes tests safeguarding the fix.

Refs:

Suggested changelog entry

Fixed: the square brackets for array access on a new anonymous class expression without wrapping parentheses, as allowed since PHP 8.4, were tokenized as short array tokens instead of square bracket tokens.

Related issues/external references

Related to #734

As of PHP 8.4, new expressions can be dereferenced without needing parentheses wrappers, as long as they have the parentheses for the constructor arguments.
```php
echo new MyClass()->property;
echo new $myClass(['value'])[0];
```

Additionally, new expressions involving anonymous classes can also be dereferenced directly and these do not have the constructor argument parentheses requirement.
```php
echo new class { public $property = 'property'; }->property;
echo new class (['value']) extends ArrayObject {}[0];
```

For the most part, this has no impact on the PHPCS tokenizer layer, however, dereferencing the result of a new anonymous class expression using array access runs into trouble with the short array retokenization.
I.e. the square brackets in the below example were tokenized as `T_OPEN|CLOSE_SHORT_ARRAY`, while they should be tokenized as `T_OPEN|CLOSE_SQUARE_BRACKET`.
```php
echo new class {}[0];
```

Fixed now. Includes tests safeguarding the fix.

Refs:
* https://wiki.php.net/rfc/new_without_parentheses
* php/php-src 13029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant