From 90cf56c329811799df2bf212539b26919790c32a Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 29 May 2023 13:32:44 +0200 Subject: [PATCH] Format-preserving printer: fix bug when replacing already parenthesized type --- src/Printer/Printer.php | 3 ++- tests/PHPStan/Printer/PrinterTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Printer/Printer.php b/src/Printer/Printer.php index bc07d10c..bace7281 100644 --- a/src/Printer/Printer.php +++ b/src/Printer/Printer.php @@ -534,7 +534,8 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes, } $parenthesesNeeded = isset($this->parenthesesListMap[$mapKey]) - && in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true); + && in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true) + && !in_array(get_class($originalNode), $this->parenthesesListMap[$mapKey], true); $addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($itemStartPos, $itemEndPos); if ($addParentheses) { $result .= '('; diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index 3d36709b..776873dd 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -1368,6 +1368,23 @@ public function enterNode(Node $node) }, ]; + + yield [ + '/** @var string&(integer|float) */', + '/** @var string&(int|float) */', + new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof IdentifierTypeNode && $node->name === 'integer') { + $node->name = 'int'; + } + + return $node; + } + + }, + ]; } /**