Skip to content

Commit d98976c

Browse files
authored
tidy up just changed arrow function comments, move above full arrow function, as standard (#7739)
1 parent 88d58c1 commit d98976c

File tree

3 files changed

+22
-44
lines changed

3 files changed

+22
-44
lines changed

rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class KeepDocblockOnReturn
2828
{
2929
public function run()
3030
{
31-
fn() =>
31+
3232
/** @psalm-suppress UndefinedFunction */
33-
ff();
3433

35-
fn() =>
34+
fn() => ff();
35+
36+
3637
// @psalm-suppress UndefinedFunction
37-
ff();
38+
39+
fn() => ff();
3840
}
3941
}
4042

rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class WithEqualVarDocType
2323
{
2424
public function run()
2525
{
26-
fn(string $var) =>
26+
2727
/** @var string $var */
28-
$var;
28+
29+
fn(string $var) => $var;
2930
}
3031
}
3132

32-
?>
33+
?>

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use PhpParser\Node\Expr\MethodCall;
2222
use PhpParser\Node\Expr\Ternary;
2323
use PhpParser\Node\Expr\Yield_;
24-
use PhpParser\Node\InterpolatedStringPart;
2524
use PhpParser\Node\Scalar\InterpolatedString;
2625
use PhpParser\Node\Scalar\String_;
2726
use PhpParser\Node\Stmt\Declare_;
@@ -113,14 +112,6 @@ public function pFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace
113112
return $this->pStmts($fileWithoutNamespace->stmts);
114113
}
115114

116-
/**
117-
* @api magic method in parent
118-
*/
119-
public function pInterpolatedStringPart(InterpolatedStringPart $interpolatedStringPart): string
120-
{
121-
return $interpolatedStringPart->value;
122-
}
123-
124115
protected function p(
125116
Node $node,
126117
int $precedence = self::MAX_PRECEDENCE,
@@ -129,14 +120,12 @@ protected function p(
129120
): string {
130121
// handle already AlwaysRememberedExpr
131122
// @see https://github.com/rectorphp/rector/issues/8815#issuecomment-2503453191
132-
while ($node instanceof AlwaysRememberedExpr) {
133-
$node = $node->getExpr();
123+
if ($node instanceof AlwaysRememberedExpr) {
124+
return $this->p($node->getExpr(), $precedence, $lhsPrecedence, $parentFormatPreserved);
134125
}
135126

136-
// handle overlapped origNode is Match_
137-
// and its subnodes still have AlwaysRememberedExpr
127+
// handle overlapped origNode is Match_ and its subnodes still have AlwaysRememberedExpr
138128
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
139-
140129
if ($originalNode instanceof Match_) {
141130
$subNodeNames = $node->getSubNodeNames();
142131
foreach ($subNodeNames as $subNodeName) {
@@ -146,17 +135,20 @@ protected function p(
146135
}
147136
}
148137

149-
$this->wrapBinaryOp($node);
138+
$this->wrapBinaryOpWithBrackets($node);
150139

151140
$content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);
152141

142+
// remove once its fixed in php-parser, https://github.com/nikic/PHP-Parser/pull/1126
153143
if ($node instanceof CallLike) {
154144
$this->cleanVariadicPlaceHolderTrailingComma($node);
155145
}
156146

157-
return $node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES) === true
158-
? ('(' . $content . ')')
159-
: $content;
147+
if ($node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES)) {
148+
return '(' . $content . ')';
149+
}
150+
151+
return $content;
160152
}
161153

162154
protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $precedence, int $lhsPrecedence): string
@@ -169,7 +161,6 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced
169161

170162
/** @var Comment[] $comments */
171163
$comments = $expr->getAttribute(AttributeKey::COMMENTS) ?? [];
172-
173164
if ($comments === []) {
174165
return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
175166
}
@@ -182,23 +173,7 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced
182173
$text .= $commentText . "\n";
183174
}
184175

185-
return $this->pPrefixOp(
186-
ArrowFunction::class,
187-
$this->pAttrGroups($arrowFunction->attrGroups, true)
188-
. $this->pStatic($arrowFunction->static)
189-
. 'fn' . ($arrowFunction->byRef ? '&' : '')
190-
. '(' . $this->pMaybeMultiline(
191-
$arrowFunction->params,
192-
$this->phpVersion->supportsTrailingCommaInParamList()
193-
) . ')'
194-
. ($arrowFunction->returnType instanceof Node ? ': ' . $this->p($arrowFunction->returnType) : '')
195-
. ' =>'
196-
. $text
197-
. $indent,
198-
$arrowFunction->expr,
199-
$precedence,
200-
$lhsPrecedence
201-
);
176+
return $text . "\n" . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
202177
}
203178

204179
/**
@@ -463,7 +438,7 @@ private function cleanVariadicPlaceHolderTrailingComma(CallLike $callLike): void
463438
}
464439
}
465440

466-
private function wrapBinaryOp(Node $node): void
441+
private function wrapBinaryOpWithBrackets(Node $node): void
467442
{
468443
if ($this->exprAnalyzer->isExprWithExprPropertyWrappable($node)) {
469444
$node->expr->setAttribute(AttributeKey::ORIGINAL_NODE, null);

0 commit comments

Comments
 (0)