Skip to content

Commit afd6b04

Browse files
authored
[Php74] Fix indentation space on ClosureToArrowFunctionRector with comment inner closure (#7779)
* [Php74] Fix indentation space on ClosureToArrowFunctionRector with comment inner closure * final touch: add handling multi comments with variable * final touch: handle as Arg * final touch: handle as Arg
1 parent 2efb35b commit afd6b04

File tree

6 files changed

+94
-17
lines changed

6 files changed

+94
-17
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
4+
5+
class AsArg
6+
{
7+
public function run()
8+
{
9+
$this->execute(function() {
10+
// some comment
11+
/** @psalm-suppress UndefinedFunction */
12+
return ff();
13+
});
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
22+
23+
class AsArg
24+
{
25+
public function run()
26+
{
27+
$this->execute(
28+
// some comment
29+
/** @psalm-suppress UndefinedFunction */
30+
fn() => ff());
31+
}
32+
}
33+
34+
?>

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ class KeepDocblockOnReturn
2828
{
2929
public function run()
3030
{
31-
32-
/** @psalm-suppress UndefinedFunction */
33-
34-
fn() => ff();
35-
36-
37-
// @psalm-suppress UndefinedFunction
31+
/** @psalm-suppress UndefinedFunction */
32+
fn() => ff();
3833

39-
fn() => ff();
34+
// @psalm-suppress UndefinedFunction
35+
fn() => ff();
4036
}
4137
}
4238

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
4+
5+
class KeepMultiCommentsWithVariableAssignOnReturn
6+
{
7+
public function run()
8+
{
9+
$variable = function() {
10+
// some comment
11+
/** @psalm-suppress UndefinedFunction */
12+
return ff();
13+
};
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
22+
23+
class KeepMultiCommentsWithVariableAssignOnReturn
24+
{
25+
public function run()
26+
{
27+
$variable =
28+
// some comment
29+
/** @psalm-suppress UndefinedFunction */
30+
(fn() => ff());
31+
}
32+
}
33+
34+
?>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ class WithEqualVarDocType
2323
{
2424
public function run()
2525
{
26-
27-
/** @var string $var */
28-
29-
fn(string $var) => $var;
26+
/** @var string $var */
27+
fn(string $var) => $var;
3028
}
3129
}
3230

rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ public function refactor(Node $node): ?Node
7373
return null;
7474
}
7575

76+
$attributes = $node->getAttributes();
77+
unset($attributes[AttributeKey::ORIGINAL_NODE]);
78+
7679
$arrowFunction = new ArrowFunction(
7780
[
7881
'params' => $node->params,
7982
'returnType' => $node->returnType,
8083
'byRef' => $node->byRef,
8184
'expr' => $returnExpr,
82-
]
85+
],
86+
$attributes
8387
);
8488

8589
if ($node->static) {

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,22 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced
176176
return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
177177
}
178178

179-
$indent = $this->resolveIndentSpaces();
179+
$isMaxPrecedence = $precedence === self::MAX_PRECEDENCE;
180+
$isNewLineAndIndent = $arrowFunction->getAttribute(AttributeKey::IS_ARG_VALUE) === true;
181+
$indent = $this->resolveIndentSpaces($isMaxPrecedence);
182+
183+
$text = $isMaxPrecedence ? '' : "\n" . $indent;
184+
if ($isNewLineAndIndent) {
185+
$indent = $this->resolveIndentSpaces();
186+
$text = "\n" . $indent;
187+
}
180188

181-
$text = "\n" . $indent;
182189
foreach ($comments as $key => $comment) {
183190
$commentText = $key > 0 ? $indent . $comment->getText() : $comment->getText();
184191
$text .= $commentText . "\n";
185192
}
186193

187-
return $text . "\n" . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
194+
return $text . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
188195
}
189196

190197
/**
@@ -502,10 +509,14 @@ private function cleanStartIndentationOnHeredocNowDoc(string $content): string
502509
return implode("\n", $trimmedLines);
503510
}
504511

505-
private function resolveIndentSpaces(): string
512+
private function resolveIndentSpaces(bool $onlyLevel = false): string
506513
{
507514
$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);
508515

516+
if ($onlyLevel) {
517+
return str_repeat($this->getIndentCharacter(), $this->indentLevel);
518+
}
519+
509520
return str_repeat($this->getIndentCharacter(), $this->indentLevel) .
510521
str_repeat($this->getIndentCharacter(), $indentSize);
511522
}

0 commit comments

Comments
 (0)