Skip to content

Commit 43ad95c

Browse files
authored
feat(printer): add per-node NEWLINE_ON_FLUENT_CALL attribute to BetterStandardPrinter (#7910)
* [Feature] Add NEWLINE_ON_FLUENT_CALL constant to AttributeKey * [Refactor] Enhance newline handling for fluent calls in BetterStandardPrinter * [Feature] Add tests for newline handling on fluent calls in BetterStandardPrinter * Update AttributeKey.php
1 parent 682d85b commit 43ad95c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,6 @@ final class AttributeKey
166166
public const string HAS_CLOSURE_WITH_VARIADIC_ARGS = 'has_closure_with_variadic_args';
167167

168168
public const string IS_IN_TRY_BLOCK = 'is_in_try_block';
169+
170+
public const string NEWLINE_ON_FLUENT_CALL = 'newline_on_fluent_call';
169171
}

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ protected function pExpr_MethodCall(MethodCall $methodCall): string
375375
return parent::pExpr_MethodCall($methodCall);
376376
}
377377

378-
if (SimpleParameterProvider::provideBoolParameter(Option::NEW_LINE_ON_FLUENT_CALL) === false) {
378+
$globalFlag = SimpleParameterProvider::provideBoolParameter(Option::NEW_LINE_ON_FLUENT_CALL);
379+
$perNodeFlag = (bool) $methodCall->getAttribute(AttributeKey::NEWLINE_ON_FLUENT_CALL, false);
380+
381+
if ($globalFlag === false && $perNodeFlag === false) {
379382
return parent::pExpr_MethodCall($methodCall);
380383
}
381384

tests/PhpParser/Printer/BetterStandardPrinterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Expression;
1616
use PHPUnit\Framework\Attributes\DataProvider;
17+
use Rector\Configuration\Option;
18+
use Rector\Configuration\Parameter\SimpleParameterProvider;
1719
use Rector\NodeTypeResolver\Node\AttributeKey;
1820
use Rector\PhpParser\Printer\BetterStandardPrinter;
1921
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
@@ -80,6 +82,29 @@ public function testYield(Node $node, string $expectedPrintedNode): void
8082
$this->assertSame($expectedPrintedNode, $printedNode);
8183
}
8284

85+
public function testPerNodeNewlineOnFluentCallAttribute(): void
86+
{
87+
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, false);
88+
89+
$innerCall = new MethodCall(new Variable('foo'), 'bar');
90+
$outerCall = new MethodCall($innerCall, 'baz');
91+
$outerCall->setAttribute(AttributeKey::NEWLINE_ON_FLUENT_CALL, true);
92+
93+
$printed = $this->betterStandardPrinter->print($outerCall);
94+
$this->assertSame('$foo->bar()' . "\n ->baz()", $printed);
95+
}
96+
97+
public function testNoNewlineOnFluentCallWithoutAttribute(): void
98+
{
99+
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, false);
100+
101+
$innerCall = new MethodCall(new Variable('foo'), 'bar');
102+
$outerCall = new MethodCall($innerCall, 'baz');
103+
104+
$printed = $this->betterStandardPrinter->print($outerCall);
105+
$this->assertSame('$foo->bar()->baz()', $printed);
106+
}
107+
83108
/**
84109
* @return Iterator<array<int, (Yield_|Expression|string)>>
85110
*/

0 commit comments

Comments
 (0)