Skip to content

Commit 90cf56c

Browse files
committed
Format-preserving printer: fix bug when replacing already parenthesized type
1 parent a56b0df commit 90cf56c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/Printer/Printer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
534534
}
535535

536536
$parenthesesNeeded = isset($this->parenthesesListMap[$mapKey])
537-
&& in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true);
537+
&& in_array(get_class($newNode), $this->parenthesesListMap[$mapKey], true)
538+
&& !in_array(get_class($originalNode), $this->parenthesesListMap[$mapKey], true);
538539
$addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($itemStartPos, $itemEndPos);
539540
if ($addParentheses) {
540541
$result .= '(';

Diff for: tests/PHPStan/Printer/PrinterTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,23 @@ public function enterNode(Node $node)
13681368

13691369
},
13701370
];
1371+
1372+
yield [
1373+
'/** @var string&(integer|float) */',
1374+
'/** @var string&(int|float) */',
1375+
new class extends AbstractNodeVisitor {
1376+
1377+
public function enterNode(Node $node)
1378+
{
1379+
if ($node instanceof IdentifierTypeNode && $node->name === 'integer') {
1380+
$node->name = 'int';
1381+
}
1382+
1383+
return $node;
1384+
}
1385+
1386+
},
1387+
];
13711388
}
13721389

13731390
/**

0 commit comments

Comments
 (0)