From f304efca71cb2d2e321de519a673ae509540d9fb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 28 Dec 2025 10:07:05 +0700 Subject: [PATCH 1/4] [Php74] Fix indentation space on ClosureToArrowFunctionRector with comment inner closure --- .../Fixture/keep_docblock_on_return.php.inc | 12 ++++-------- .../Fixture/with_equal_var_doc_type.php.inc | 6 ++---- src/PhpParser/Printer/BetterStandardPrinter.php | 14 +++++++++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc index 810e66e27d1..d3463cd084a 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc @@ -28,15 +28,11 @@ class KeepDocblockOnReturn { public function run() { - - /** @psalm-suppress UndefinedFunction */ - - fn() => ff(); - - - // @psalm-suppress UndefinedFunction + /** @psalm-suppress UndefinedFunction */ + fn() => ff(); - fn() => ff(); + // @psalm-suppress UndefinedFunction + fn() => ff(); } } diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc index f7b0535b810..d21f587edd5 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc @@ -23,10 +23,8 @@ class WithEqualVarDocType { public function run() { - - /** @var string $var */ - - fn(string $var) => $var; + /** @var string $var */ + fn(string $var) => $var; } } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 97543ea602d..5dcc34b72e7 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -176,15 +176,15 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } - $indent = $this->resolveIndentSpaces(); + $indentOnlyLevel = $this->resolveIndentSpaces(true); - $text = "\n" . $indent; + $text = ""; foreach ($comments as $key => $comment) { - $commentText = $key > 0 ? $indent . $comment->getText() : $comment->getText(); + $commentText = $key > 0 ? $indentOnlyLevel . $comment->getText() : $comment->getText(); $text .= $commentText . "\n"; } - return $text . "\n" . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); + return $text . $indentOnlyLevel . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } /** @@ -502,10 +502,14 @@ private function cleanStartIndentationOnHeredocNowDoc(string $content): string return implode("\n", $trimmedLines); } - private function resolveIndentSpaces(): string + private function resolveIndentSpaces(bool $onlyLevel = false): string { $indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE); + if ($onlyLevel) { + return str_repeat($this->getIndentCharacter(), $this->indentLevel); + } + return str_repeat($this->getIndentCharacter(), $this->indentLevel) . str_repeat($this->getIndentCharacter(), $indentSize); } From a378b0dfcd261c3fd7014adeae2e9484b92fe22e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 28 Dec 2025 19:52:43 +0700 Subject: [PATCH 2/4] final touch: add handling multi comments with variable --- ...nts_with_variable_assign_on_return.php.inc | 34 +++++++++++++++++++ .../Printer/BetterStandardPrinter.php | 9 ++--- 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_multi_comments_with_variable_assign_on_return.php.inc diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_multi_comments_with_variable_assign_on_return.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_multi_comments_with_variable_assign_on_return.php.inc new file mode 100644 index 00000000000..05d9eeba973 --- /dev/null +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_multi_comments_with_variable_assign_on_return.php.inc @@ -0,0 +1,34 @@ + +----- + ff()); + } +} + +?> diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 5dcc34b72e7..0d4ea7e263f 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -176,15 +176,16 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } - $indentOnlyLevel = $this->resolveIndentSpaces(true); + $isMaxPrecedence = $precedence === self::MAX_PRECEDENCE; + $indent = $this->resolveIndentSpaces($isMaxPrecedence); - $text = ""; + $text = $isMaxPrecedence ? '' : "\n" . $indent; foreach ($comments as $key => $comment) { - $commentText = $key > 0 ? $indentOnlyLevel . $comment->getText() : $comment->getText(); + $commentText = $key > 0 ? $indent . $comment->getText() : $comment->getText(); $text .= $commentText . "\n"; } - return $text . $indentOnlyLevel . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); + return $text . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence); } /** From f90358c4fe530dbdf0858dceecd8ff9910b96f9e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 28 Dec 2025 23:00:57 +0700 Subject: [PATCH 3/4] final touch: handle as Arg --- rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php | 6 +++++- src/PhpParser/Printer/BetterStandardPrinter.php | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php index d183c2f40d6..2216cd5906a 100644 --- a/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php +++ b/rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php @@ -73,13 +73,17 @@ public function refactor(Node $node): ?Node return null; } + $attributes = $node->getAttributes(); + unset($attributes[AttributeKey::ORIGINAL_NODE]); + $arrowFunction = new ArrowFunction( [ 'params' => $node->params, 'returnType' => $node->returnType, 'byRef' => $node->byRef, 'expr' => $returnExpr, - ] + ], + $attributes ); if ($node->static) { diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 0d4ea7e263f..a3347d3553c 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -177,9 +177,15 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced } $isMaxPrecedence = $precedence === self::MAX_PRECEDENCE; + $isNewLineAndIndent = $arrowFunction->getAttribute(AttributeKey::IS_ARG_VALUE) === true; $indent = $this->resolveIndentSpaces($isMaxPrecedence); $text = $isMaxPrecedence ? '' : "\n" . $indent; + if ($isNewLineAndIndent) { + $indent = $this->resolveIndentSpaces(); + $text = "\n" . $indent; + } + foreach ($comments as $key => $comment) { $commentText = $key > 0 ? $indent . $comment->getText() : $comment->getText(); $text .= $commentText . "\n"; From 0ac37d908ca609c36e08f7f807fbc5f55962d91c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 28 Dec 2025 23:01:02 +0700 Subject: [PATCH 4/4] final touch: handle as Arg --- .../Fixture/as_arg.php.inc | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/as_arg.php.inc diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/as_arg.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/as_arg.php.inc new file mode 100644 index 00000000000..448d2e4b1f9 --- /dev/null +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/as_arg.php.inc @@ -0,0 +1,34 @@ +execute(function() { + // some comment + /** @psalm-suppress UndefinedFunction */ + return ff(); + }); + } +} + +?> +----- +execute( + // some comment + /** @psalm-suppress UndefinedFunction */ + fn() => ff()); + } +} + +?>