Skip to content

PHP8.4: Support new without parenthesis #415

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: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,8 @@ private function parsePostfixExpressionRest($expression, $allowUpdateExpression
$expression instanceof SubscriptExpression ||
$expression instanceof ScopedPropertyAccessExpression ||
$expression instanceof StringLiteral ||
$expression instanceof ArrayCreationExpression
$expression instanceof ArrayCreationExpression ||
$expression instanceof ObjectCreationExpression
)) {
return $expression;
}
Expand Down Expand Up @@ -3289,6 +3290,11 @@ private function parseObjectCreationExpression($parentNode) {
$objectCreationExpression->classMembers = $this->parseClassMembers($objectCreationExpression);
}

// PHP8.4 new with no parenthesis
if ($this->getCurrentToken()->kind === TokenKind::ArrowToken) {
return $this->parsePostfixExpressionRest($objectCreationExpression);
Copy link
Contributor Author

@dantleech dantleech Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line with the rest of this libaray this should probably have a run time php_version() > 80400?

(incidentally would be good to refactor so that it didn't depend on the runtime PHP version)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for being able to configure at what PHP level the parser works

}

return $objectCreationExpression;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser84/new-without-parenthesis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

new Foobar()->bar();
3 changes: 3 additions & 0 deletions tests/cases/parser84/new-without-parenthesis.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

new Foobar()->bar();
3 changes: 3 additions & 0 deletions tests/cases/parser84/new-without-parenthesis.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

new Foobar()->bar();