Skip to content

Commit

Permalink
Format-preserving printer: fix bug when replacing already parenthesiz…
Browse files Browse the repository at this point in the history
…ed type
  • Loading branch information
ondrejmirtes committed May 29, 2023
1 parent a56b0df commit 90cf56c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '(';
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

},
];
}

/**
Expand Down

0 comments on commit 90cf56c

Please sign in to comment.