Default pretty printer to PHP 7.4

This commit is contained in:
Nikita Popov 2023-08-27 21:26:47 +02:00
parent efe93a171b
commit 8d58380108
5 changed files with 36 additions and 29 deletions

View File

@ -198,12 +198,12 @@ else if ($x) {
}
```
The pretty printer now accepts a `phpVersion` option, which accepts a `PhpVersion` object and defaults to PHP 7.1. The pretty printer will make formatting choices to make the code valid for that version. It currently controls the following behavior:
The pretty printer now accepts a `phpVersion` option, which accepts a `PhpVersion` object and defaults to PHP 7.4. The pretty printer will make formatting choices to make the code valid for that version. It currently controls the following behavior:
* For PHP >= 7.0 (default), short array syntax `[]` will be used by default. This does not affect nodes that specify an explicit array syntax using the `kind` attribute.
* For PHP >= 7.0 (default), parentheses around `yield` expressions will only be printed when necessary. Previously, parentheses were always printed, even if `yield` was used as a statement.
* For PHP >= 7.1 (default), the short array syntax `[]` will be used for destructuring by default (instead of `list()`). This does not affect nodes that specify and explicit syntax using the `kind` attribute.
* For PHP >= 7.3, a newline is no longer forced after heredoc/nowdoc strings, as the requirement for this has been removed with the introduction of flexible heredoc/nowdoc strings.
* For PHP >= 7.3 (default), a newline is no longer forced after heredoc/nowdoc strings, as the requirement for this has been removed with the introduction of flexible heredoc/nowdoc strings.
### Changes to precedence handling in the pretty printer

View File

@ -162,7 +162,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
* Creates a pretty printer instance using the given options.
*
* Supported options:
* * PhpVersion $phpVersion: The PHP version to target (default to PHP 7.1). This option
* * PhpVersion $phpVersion: The PHP version to target (default to PHP 7.4). This option
* controls compatibility of the generated code with older PHP
* versions in cases where a simple stylistic choice exists (e.g.
* array() vs []). It is safe to pretty-print an AST for a newer
@ -178,7 +178,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
* } $options Dictionary of formatting options
*/
public function __construct(array $options = []) {
$this->phpVersion = $options['phpVersion'] ?? PhpVersion::fromComponents(7, 1);
$this->phpVersion = $options['phpVersion'] ?? PhpVersion::fromComponents(7, 4);
$this->newline = $options['newline'] ?? "\n";
if ($this->newline !== "\n" && $this->newline != "\r\n") {

View File

@ -112,24 +112,24 @@ class PrettyPrinterTest extends CodeTestAbstract {
[new String_("\tSTR", $nowdoc), "'\tSTR'"],
[new String_("STR\x80", $heredoc), '"STR\x80"'],
// Doc string if label not contained (or not in ending position)
[new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
[new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
[new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
[new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
[new String_("STRä", $nowdoc), "<<<'STR'\nSTRä\nSTR\n"],
[new String_("STR\x80", $nowdoc), "<<<'STR'\nSTR\x80\nSTR\n"],
[new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR"],
[new String_("foo", $heredoc), "<<<STR\nfoo\nSTR"],
[new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR"],
[new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR"],
[new String_("STRä", $nowdoc), "<<<'STR'\nSTRä\nSTR"],
[new String_("STR\x80", $nowdoc), "<<<'STR'\nSTR\x80\nSTR"],
// Empty doc string variations (encapsed variant does not occur naturally)
[new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
[new String_("", $heredoc), "<<<STR\nSTR\n"],
[new InterpolatedString([new InterpolatedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
[new String_("", $nowdoc), "<<<'STR'\nSTR"],
[new String_("", $heredoc), "<<<STR\nSTR"],
[new InterpolatedString([new InterpolatedStringPart('')], $heredoc), "<<<STR\nSTR"],
// Isolated \r in doc string
[new String_("\r", $heredoc), "<<<STR\n\\r\nSTR\n"],
[new String_("\r", $heredoc), "<<<STR\n\\r\nSTR"],
[new String_("\r", $nowdoc), "'\r'"],
[new String_("\rx", $nowdoc), "<<<'STR'\n\rx\nSTR\n"],
[new String_("\rx", $nowdoc), "<<<'STR'\n\rx\nSTR"],
// Encapsed doc string variations
[new InterpolatedString([new InterpolatedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
[new InterpolatedString([new InterpolatedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
[new InterpolatedString([new Expr\Variable('y'), new InterpolatedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
[new InterpolatedString([new InterpolatedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR"],
[new InterpolatedString([new InterpolatedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR"],
[new InterpolatedString([new Expr\Variable('y'), new InterpolatedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR"],
// Encapsed doc string fallback
[new InterpolatedString([new Expr\Variable('y'), new InterpolatedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
[new InterpolatedString([new InterpolatedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
@ -257,7 +257,10 @@ CODE
}
public function testWindowsNewline() {
$prettyPrinter = new Standard(['newline' => "\r\n"]);
$prettyPrinter = new Standard([
'newline' => "\r\n",
'phpVersion' => PhpVersion::fromComponents(7, 2),
]);
$stmts = [
new Stmt\If_(new Int_(1), [
'stmts' => [

View File

@ -70,11 +70,9 @@ a{$b}
STR;
call(<<<STR
A
STR
, <<<STR
STR, <<<STR
B
STR
);
STR);
function test()
{
<<<STR
@ -88,9 +86,17 @@ STR;
<?php
foo(<<<STR
abc
STR);
STR
, <<<STR
abc
STR
);
-----
!!version=7.3
!!version=7.2
foo(<<<STR
abc
STR);
STR
, <<<STR
abc
STR
);

View File

@ -15,7 +15,6 @@ Escape sequences in double-quoted strings
\0000\0001
äöü
DOC;
-----
"\n\r\t\f\v\$\"\\";
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
@ -29,5 +28,4 @@ DOC;
\x00\x01\x02\x03\x04\x05\x06\x07\x08\t@@{ "\n" }@@\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
\x000\x001
äöü
DOC
;
DOC;