Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@ final class AttributeKey
public const string HAS_CLOSURE_WITH_VARIADIC_ARGS = 'has_closure_with_variadic_args';

public const string IS_IN_TRY_BLOCK = 'is_in_try_block';

public const string NEWLINE_ON_FLUENT_CALL = 'newlineOnFluentCall';
}
5 changes: 4 additions & 1 deletion src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ protected function pExpr_MethodCall(MethodCall $methodCall): string
return parent::pExpr_MethodCall($methodCall);
}

if (SimpleParameterProvider::provideBoolParameter(Option::NEW_LINE_ON_FLUENT_CALL) === false) {
$globalFlag = SimpleParameterProvider::provideBoolParameter(Option::NEW_LINE_ON_FLUENT_CALL);
$perNodeFlag = (bool) $methodCall->getAttribute(AttributeKey::NEWLINE_ON_FLUENT_CALL, false);

if ($globalFlag === false && $perNodeFlag === false) {
return parent::pExpr_MethodCall($methodCall);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/PhpParser/Printer/BetterStandardPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
Expand Down Expand Up @@ -80,6 +82,29 @@ public function testYield(Node $node, string $expectedPrintedNode): void
$this->assertSame($expectedPrintedNode, $printedNode);
}

public function testPerNodeNewlineOnFluentCallAttribute(): void
{
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, false);

$innerCall = new MethodCall(new Variable('foo'), 'bar');
$outerCall = new MethodCall($innerCall, 'baz');
$outerCall->setAttribute(AttributeKey::NEWLINE_ON_FLUENT_CALL, true);

$printed = $this->betterStandardPrinter->print($outerCall);
$this->assertSame('$foo->bar()' . "\n ->baz()", $printed);
}

public function testNoNewlineOnFluentCallWithoutAttribute(): void
{
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, false);

$innerCall = new MethodCall(new Variable('foo'), 'bar');
$outerCall = new MethodCall($innerCall, 'baz');

$printed = $this->betterStandardPrinter->print($outerCall);
$this->assertSame('$foo->bar()->baz()', $printed);
}

/**
* @return Iterator<array<int, (Yield_|Expression|string)>>
*/
Expand Down