nikic / PHP-Parser

A PHP parser written in PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PrettyPrinter does not support `new (expr)()`

arima-ryunosuke opened this issue · comments

  • version: v4.15.2
  • php-version: PHP 8.0.20
  • platform: windows and linux

It seems that PrettyPrinter\Standard outputs incorrect syntax.

<?php
require_once __DIR__ . '/vendor/autoload.php';

use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;

$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
$ast = $parser->parse('<?php new ("stdClass")();');

// output: new "stdClass"();
echo (new Standard())->prettyPrint($ast);

new ("stdClass")(); is a syntax error in php 7.4, but it is valid syntax in php 8.0.

https://www.php.net/manual/en/migration80.new-features.php

new and instanceof can now be used with arbitrary expressions, using new (expression)(...$args) and $obj instanceof (expression).

This case seems to be caused by the following code. When I commented it out to try it out, the parentheses were given.

https://github.com/nikic/PHP-Parser/blob/v4.15.2/lib/PhpParser/PrettyPrinterAbstract.php#L1068

Any good ideas for fixes or workarounds?

Fixed on master.