Skip to content

Commit c97b23d

Browse files
committed
Omit new parentheses when printing for PHP 8.4
PHP 8.4 allows omitting the parentheses and PER requires it.
1 parent 6b05527 commit c97b23d

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

lib/PhpParser/PhpVersion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,8 @@ public function supportsUnicodeEscapes(): bool {
168168
public function supportsAttributes(): bool {
169169
return $this->id >= 80000;
170170
}
171+
172+
public function supportsNewDereferenceWithoutParentheses(): bool {
173+
return $this->id >= 80400;
174+
}
171175
}

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,9 @@ protected function safeAppend(string &$str, string $append): void {
11481148
* @return bool Whether parentheses are required
11491149
*/
11501150
protected function callLhsRequiresParens(Node $node): bool {
1151+
if ($node instanceof Expr\New_) {
1152+
return !$this->phpVersion->supportsNewDereferenceWithoutParentheses();
1153+
}
11511154
return !($node instanceof Node\Name
11521155
|| $node instanceof Expr\Variable
11531156
|| $node instanceof Expr\ArrayDimFetch
@@ -1179,6 +1182,9 @@ protected function dereferenceLhsRequiresParens(Node $node): bool {
11791182
* @return bool Whether parentheses are required
11801183
*/
11811184
protected function staticDereferenceLhsRequiresParens(Node $node): bool {
1185+
if ($node instanceof Expr\New_) {
1186+
return !$this->phpVersion->supportsNewDereferenceWithoutParentheses();
1187+
}
11821188
return !($node instanceof Expr\Variable
11831189
|| $node instanceof Node\Name
11841190
|| $node instanceof Expr\ArrayDimFetch
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Parentheses for new dereferences
2+
-----
3+
<?php
4+
new MyClass()::CONSTANT;
5+
new MyClass()::$staticProperty;
6+
new MyClass()::staticMethod();
7+
new MyClass()->property;
8+
new MyClass()->method();
9+
new MyClass()();
10+
new MyClass()[0];
11+
new class {}::CONSTANT;
12+
new class {}::$staticProperty;
13+
new class {}::staticMethod();
14+
new class {}->property;
15+
new class {}->method();
16+
new class {}();
17+
new class {}[0];
18+
-----
19+
!!version=8.4
20+
new MyClass()::CONSTANT;
21+
new MyClass()::$staticProperty;
22+
new MyClass()::staticMethod();
23+
new MyClass()->property;
24+
new MyClass()->method();
25+
new MyClass()();
26+
new MyClass()[0];
27+
new class
28+
{
29+
}::CONSTANT;
30+
new class
31+
{
32+
}::$staticProperty;
33+
new class
34+
{
35+
}::staticMethod();
36+
new class
37+
{
38+
}->property;
39+
new class
40+
{
41+
}->method();
42+
new class
43+
{
44+
}();
45+
new class
46+
{
47+
}[0];
48+
-----
49+
<?php
50+
new MyClass()::CONSTANT;
51+
new MyClass()::$staticProperty;
52+
new MyClass()::staticMethod();
53+
new MyClass()->property;
54+
new MyClass()->method();
55+
new MyClass()();
56+
new MyClass()[0];
57+
new class {}::CONSTANT;
58+
new class {}::$staticProperty;
59+
new class {}::staticMethod();
60+
new class {}->property;
61+
new class {}->method();
62+
new class {}();
63+
new class {}[0];
64+
-----
65+
!!version=8.3
66+
(new MyClass())::CONSTANT;
67+
(new MyClass())::$staticProperty;
68+
(new MyClass())::staticMethod();
69+
(new MyClass())->property;
70+
(new MyClass())->method();
71+
(new MyClass())();
72+
(new MyClass())[0];
73+
(new class
74+
{
75+
})::CONSTANT;
76+
(new class
77+
{
78+
})::$staticProperty;
79+
(new class
80+
{
81+
})::staticMethod();
82+
(new class
83+
{
84+
})->property;
85+
(new class
86+
{
87+
})->method();
88+
(new class
89+
{
90+
})();
91+
(new class
92+
{
93+
})[0];

0 commit comments

Comments
 (0)