Skip to content

Commit

Permalink
Do not add extra parentheses when changing the return type of callable
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich authored and ondrejmirtes committed May 29, 2023
1 parent b0ea9f4 commit a8094fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,12 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
$mapKey = get_class($node) . '->' . $subNodeName;
$parenthesesNeeded = isset($this->parenthesesMap[$mapKey])
&& in_array(get_class($subNode), $this->parenthesesMap[$mapKey], true);

if ($subNode->getAttribute(Attribute::ORIGINAL_NODE) !== null) {
$parenthesesNeeded = $parenthesesNeeded
&& !in_array(get_class($subNode->getAttribute(Attribute::ORIGINAL_NODE)), $this->parenthesesMap[$mapKey], true);
}

$addParentheses = $parenthesesNeeded && !$originalTokens->hasParentheses($subStartPos, $subEndPos);
if ($addParentheses) {
$result .= '(';
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,26 @@ public function enterNode(Node $node)

},
];

yield [
'/** @return callable(): (null|null) */',
'/** @return callable(): (int|null) */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof UnionTypeNode) {
$node->types = [
new IdentifierTypeNode('int'),
new IdentifierTypeNode('null'),
];
}

return $node;
}

},
];
}

/**
Expand Down

0 comments on commit a8094fa

Please sign in to comment.