Skip to content

Commit f304efc

Browse files
committed
[Php74] Fix indentation space on ClosureToArrowFunctionRector with comment inner closure
1 parent 2efb35b commit f304efc

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

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

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

src/PhpParser/Printer/BetterStandardPrinter.php

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

179-
$indent = $this->resolveIndentSpaces();
179+
$indentOnlyLevel = $this->resolveIndentSpaces(true);
180180

181-
$text = "\n" . $indent;
181+
$text = "";
182182
foreach ($comments as $key => $comment) {
183-
$commentText = $key > 0 ? $indent . $comment->getText() : $comment->getText();
183+
$commentText = $key > 0 ? $indentOnlyLevel . $comment->getText() : $comment->getText();
184184
$text .= $commentText . "\n";
185185
}
186186

187-
return $text . "\n" . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
187+
return $text . $indentOnlyLevel . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
188188
}
189189

190190
/**
@@ -502,10 +502,14 @@ private function cleanStartIndentationOnHeredocNowDoc(string $content): string
502502
return implode("\n", $trimmedLines);
503503
}
504504

505-
private function resolveIndentSpaces(): string
505+
private function resolveIndentSpaces(bool $onlyLevel = false): string
506506
{
507507
$indentSize = SimpleParameterProvider::provideIntParameter(Option::INDENT_SIZE);
508508

509+
if ($onlyLevel) {
510+
return str_repeat($this->getIndentCharacter(), $this->indentLevel);
511+
}
512+
509513
return str_repeat($this->getIndentCharacter(), $this->indentLevel) .
510514
str_repeat($this->getIndentCharacter(), $indentSize);
511515
}

0 commit comments

Comments
 (0)