fix: use PHP 8 concatenation precedence in the pretty printer

The precedence table gave "." the same precedence as "+" and "-", which is
the PHP 7 ordering. PHP 8.0 lowered "." below the arithmetic and shift
operators, so printing ('abc' . 1) + 2 produced 'abc' . 1 + 2, which PHP 8
reparses as 'abc' . (1 + 2). The same applied to "-", "<<" and ">>".

The printer already targets a specific PHP version and already varies
parenthesization by version, so select the concatenation precedence the same
way: for a target of PHP 8.0 or newer, place "." between the shift operators
and the pipe operator, matching the PHP8 block of grammar/php.y. Targets below
8.0 keep the PHP 7 table, which is correct for them and produces unchanged
output.

Fixes #1041
This commit is contained in:
dualfroz committed 2026-09-03 14:09:59 +02:00
1 parent 09376a3b68
commit 00e7c77eee
4 files changed
+86 -4

No files matched your search

+8
View File
@@ -172,4 +172,12 @@ class PhpVersion {
public function supportsNewDereferenceWithoutParentheses(): bool {
return $this->id >= 80400;
}
/**
* Whether the concatenation operator has lower precedence than the arithmetic and shift
* operators. Prior to PHP 8.0, "." had the same precedence as "+" and "-".
*/
public function hasLowerConcatPrecedence(): bool {
return $this->id >= 80000;
}
}