Skip to content

Commit a8094fa

Browse files
kukulichondrejmirtes
authored andcommitted
Do not add extra parentheses when changing the return type of callable
1 parent b0ea9f4 commit a8094fa

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: src/Printer/Printer.php

+6
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,12 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
778778
$mapKey = get_class($node) . '->' . $subNodeName;
779779
$parenthesesNeeded = isset($this->parenthesesMap[$mapKey])
780780
&& in_array(get_class($subNode), $this->parenthesesMap[$mapKey], true);
781+
782+
if ($subNode->getAttribute(Attribute::ORIGINAL_NODE) !== null) {
783+
$parenthesesNeeded = $parenthesesNeeded
784+
&& !in_array(get_class($subNode->getAttribute(Attribute::ORIGINAL_NODE)), $this->parenthesesMap[$mapKey], true);
785+
}
786+
781787
$addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($subStartPos, $subEndPos);
782788
if ($addParentheses) {
783789
$result .= '(';

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

+20
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,26 @@ public function enterNode(Node $node)
14061406

14071407
},
14081408
];
1409+
1410+
yield [
1411+
'/** @return callable(): (null|null) */',
1412+
'/** @return callable(): (int|null) */',
1413+
new class extends AbstractNodeVisitor {
1414+
1415+
public function enterNode(Node $node)
1416+
{
1417+
if ($node instanceof UnionTypeNode) {
1418+
$node->types = [
1419+
new IdentifierTypeNode('int'),
1420+
new IdentifierTypeNode('null'),
1421+
];
1422+
}
1423+
1424+
return $node;
1425+
}
1426+
1427+
},
1428+
];
14091429
}
14101430

14111431
/**

0 commit comments

Comments
 (0)