From 282fdc795c353df16891f620484b3e29d02cafd2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 23 Nov 2026 13:46:25 +0100 Subject: [PATCH] Parse new with an expression and dynamic class constant fetch in php8 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}. --- .../php8/dynamic_class_constant_fetch_test.go | 98 + internal/php8/new_expression_test.go | 82 + internal/php8/php8.go | 2511 +++++++++-------- internal/php8/php8.y | 33 + pkg/ast/node.go | 6 +- pkg/visitor/dumper/dumper.go | 2 + pkg/visitor/formatter/formatter.go | 8 + pkg/visitor/formatter/formatter_test.go | 28 + pkg/visitor/printer/printer.go | 2 + pkg/visitor/printer/printer_test.go | 24 + 10 files changed, 1570 insertions(+), 1224 deletions(-) create mode 100644 internal/php8/dynamic_class_constant_fetch_test.go create mode 100644 internal/php8/new_expression_test.go diff --git a/internal/php8/dynamic_class_constant_fetch_test.go b/internal/php8/dynamic_class_constant_fetch_test.go new file mode 100644 index 0000000..651d785 --- /dev/null +++ b/internal/php8/dynamic_class_constant_fetch_test.go @@ -0,0 +1,98 @@ +package php8_test + +import ( + "bytes" + "testing" + + "github.com/rectorphp/php-parser-in-go/internal/php8" + "github.com/rectorphp/php-parser-in-go/pkg/ast" + "github.com/rectorphp/php-parser-in-go/pkg/conf" + "github.com/rectorphp/php-parser-in-go/pkg/errors" + "github.com/rectorphp/php-parser-in-go/pkg/version" + "github.com/rectorphp/php-parser-in-go/pkg/visitor/printer" +) + +// parseDynamicClassConstantFetch parses PHP 8.3 source and returns the root plus +// every parser error it collected. +func parseDynamicClassConstantFetch(source string) (ast.Vertex, []*errors.Error) { + var parserErrors []*errors.Error + config := conf.Config{ + Version: &version.Version{Major: 8, Minor: 3}, + ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) }, + } + lexer := php8.NewLexer([]byte(source), config) + parser := php8.NewParser(lexer, config) + parser.Parse() + return parser.GetRootNode(), parserErrors +} + +func classConstantFetchOf(test *testing.T, root ast.Vertex) *ast.ExprClassConstFetch { + test.Helper() + returnStmt := root.(*ast.Root).Stmts[0].(*ast.StmtReturn) + fetch, ok := returnStmt.Expr.(*ast.ExprClassConstFetch) + if !ok { + test.Fatalf("expected a class constant fetch, got %T", returnStmt.Expr) + } + return fetch +} + +func TestDynamicClassConstantFetchOnClassName(test *testing.T) { + root, parserErrors := parseDynamicClassConstantFetch(`value; +` + + root, parserErrors := parseDynamicClassConstantFetch(source) + if len(parserErrors) != 0 { + test.Fatalf("unexpected parser errors: %v", parserErrors) + } + + output := bytes.NewBufferString("") + root.Accept(printer.NewPrinter(output)) + if output.String() != source { + test.Errorf("printed output differs from source:\n%s", output.String()) + } +} diff --git a/internal/php8/new_expression_test.go b/internal/php8/new_expression_test.go new file mode 100644 index 0000000..8c39391 --- /dev/null +++ b/internal/php8/new_expression_test.go @@ -0,0 +1,82 @@ +package php8_test + +import ( + "bytes" + "testing" + + "github.com/rectorphp/php-parser-in-go/internal/php8" + "github.com/rectorphp/php-parser-in-go/pkg/ast" + "github.com/rectorphp/php-parser-in-go/pkg/conf" + "github.com/rectorphp/php-parser-in-go/pkg/errors" + "github.com/rectorphp/php-parser-in-go/pkg/version" + "github.com/rectorphp/php-parser-in-go/pkg/visitor/printer" +) + +// parseNewExpression parses PHP 8.0 source and returns the root plus every +// parser error it collected. +func parseNewExpression(source string) (ast.Vertex, []*errors.Error) { + var parserErrors []*errors.Error + config := conf.Config{ + Version: &version.Version{Major: 8, Minor: 0}, + ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) }, + } + lexer := php8.NewLexer([]byte(source), config) + parser := php8.NewParser(lexer, config) + parser.Parse() + return parser.GetRootNode(), parserErrors +} + +func TestNewWithExpressionClassName(test *testing.T) { + root, parserErrors := parseNewExpression(`newQuery();`) + if len(parserErrors) != 0 { + test.Fatalf("unexpected parser errors: %v", parserErrors) + } +} + +func TestNewWithExpressionClassNamePrintsBackUnchanged(test *testing.T) { + source := `