diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index 1126c3d8..23f9915d 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -720,7 +720,8 @@ class Standard extends PrettyPrinter { protected function pExpr_Yield(Expr\Yield_ $node, int $precedence, int $lhsPrecedence): string { if ($node->value === null) { - return 'yield'; + $opPrecedence = $this->precedenceMap[Expr\Yield_::class][0]; + return $opPrecedence >= $lhsPrecedence ? '(yield)' : 'yield'; } else { if (!$this->phpVersion->supportsYieldWithoutParentheses()) { return '(yield ' . $this->pKey($node->key) . $this->p($node->value) . ')'; diff --git a/test/code/prettyPrinter/expr/yield.test b/test/code/prettyPrinter/expr/yield.test index 8d7f4d00..006fc79d 100644 --- a/test/code/prettyPrinter/expr/yield.test +++ b/test/code/prettyPrinter/expr/yield.test @@ -22,6 +22,9 @@ function gen() match ($x) { yield $a, (yield $b) => $c, }; + yield -$a; + (yield) - $a; + yield * $a; } ----- function gen() @@ -44,6 +47,9 @@ function gen() match ($x) { yield $a, (yield $b) => $c, }; + yield -$a; + (yield) - $a; + (yield) * $a; } ----- $c, }; + yield -$a; + (yield) - $a; + yield * $a; } ----- !!version=5.6 @@ -91,4 +100,7 @@ function gen() match ($x) { (yield $a), (yield $b) => $c, }; + (yield -$a); + (yield) - $a; + (yield) * $a; }