Skip to content

Commit be04892

Browse files
authored
[BetterPhpDocParser] Don't wrap first-position @method param union in extra parens (#8003)
1 parent 3ce9bbb commit be04892

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
4+
5+
/**
6+
* @method static \Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass disable(string|array ...$classes)
7+
*/
8+
class ImportMethodWithFirstParamUnion
9+
{
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;
17+
18+
use Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\NormalParamClass;
19+
20+
/**
21+
* @method static NormalParamClass disable(string|array ...$classes)
22+
*/
23+
class ImportMethodWithFirstParamUnion
24+
{
25+
}
26+
27+
?>

src/BetterPhpDocParser/PhpDocNodeVisitor/UnionTypeNodePhpDocNodeVisitor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public function enterNode(Node $node): ?Node
5858
private function isWrappedInCurlyBrackets(BetterTokenIterator $betterTokenProvider, StartAndEnd $startAndEnd): bool
5959
{
6060
$previousPosition = $startAndEnd->getStart() - 1;
61+
$nextPosition = $startAndEnd->getEnd() + 1;
6162

62-
if ($betterTokenProvider->isTokenTypeOnPosition(Lexer::TOKEN_OPEN_PARENTHESES, $previousPosition)) {
63-
return true;
64-
}
65-
66-
// there is no + 1, as end is right at the next token
67-
return $betterTokenProvider->isTokenTypeOnPosition(Lexer::TOKEN_CLOSE_PARENTHESES, $startAndEnd->getEnd());
63+
// A union is wrapped only when BOTH parens flank it. Either alone may be unrelated —
64+
// e.g. an `@method`'s parameter-list `(` before a first-position union, or the matching
65+
// `)` after a last-position union — neither belongs to the union type itself.
66+
return $betterTokenProvider->isTokenTypeOnPosition(Lexer::TOKEN_OPEN_PARENTHESES, $previousPosition)
67+
&& $betterTokenProvider->isTokenTypeOnPosition(Lexer::TOKEN_CLOSE_PARENTHESES, $nextPosition);
6868
}
6969

7070
private function resolveStartAndEnd(UnionTypeNode $unionTypeNode): ?StartAndEnd

0 commit comments

Comments
 (0)