Repository navigation
Parse new with an expression and dynamic class constant fetch in php8 - #36
Merged
Merged
Conversation
PHP 8.0 lets new take any parenthesized expression as the class,
new ($prefix . 'Handler')(), and PHP 8.3 lets the constant name of a class
constant fetch be computed, Foo::{$name}. Both were syntax errors.
The class of new (expr) is an ExprBrackets node. ExprClassConstFetch gains
OpenCurlyBracketTkn and CloseCurlyBracketTkn for the computed name, kept by
the printer and dumper and added by the formatter, the same way
ExprPropertyFetch handles $object->{$name}.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two syntaxes the php8 grammar rejected, found by parsing every rector-src test fixture:
newwith an arbitrary expressionnew ($prefix . 'Handler')($config),(new (static::getModel()))->newQuery()Status::{$name},$this::{'PREFIX_' . $name}Both are common enough in real code (e.g.
new (static::getModel())in Laravel) that a downstream tool hitting one cannot process the file at all.Grammar - mirrors
zend_language_parser.y:class_name_referencegains'(' expr ')', built as the existingast.ExprBracketsclass_constantgainsclass_name :: '{' expr '}'andvariable_class_name :: '{' expr '}'AST - additive only:
ast.ExprClassConstFetchgainsOpenCurlyBracketTknandCloseCurlyBracketTkn, nil for a plain identifier name; same shape asExprPropertyFetchfor$object->{$name}Constis not an identifierThe grammar conflict count is unchanged (3 shift/reduce, 7 reduce/reduce).
php8.goregenerated with the latest goyacc - regenerating the unchanged grammar the same way reproduces the committed file byte for byte.Tests: parse shape, print-back round trip (including spacing inside the brackets), formatter and printer. Verified downstream: reco's full test suite passes against this branch.