diff --git a/src/Printer/BetterStandardPrinter.php b/src/Printer/BetterStandardPrinter.php index 587aef7b18f..0c2cb10cf7e 100644 --- a/src/Printer/BetterStandardPrinter.php +++ b/src/Printer/BetterStandardPrinter.php @@ -24,24 +24,24 @@ final class BetterStandardPrinter extends Standard } /** - * Do not add () on Expressions + * Do not add "()" on Expressions * @see https://github.com/rectorphp/rector/pull/401#discussion_r181487199 */ - protected function pExpr_Yield(Yield_ $yieldNode) + protected function pExpr_Yield(Yield_ $yieldNode): string { if ($yieldNode->value === null) { return 'yield'; - } else { - $parentNode = $yieldNode->getAttribute(Attribute::PARENT_NODE); - $yieldContent = 'yield ' - . ($yieldNode->key !== null ? $this->p($yieldNode->key) . ' => ' : '') - . $this->p($yieldNode->value); - - if (! $parentNode instanceof Expression) { - return $yieldContent; - } - - return '(' . $yieldContent . ')'; } + + $parentNode = $yieldNode->getAttribute(Attribute::PARENT_NODE); + $shouldAddBrackets = $parentNode instanceof Expression; + + return sprintf( + '%syield %s%s%s', + $shouldAddBrackets ? '(' : '', + $yieldNode->key !== null ? $this->p($yieldNode->key) . ' => ' : '', + $this->p($yieldNode->value), + $shouldAddBrackets ? ')' : '' + ); } }