Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;

use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SomeSetterClass;

final class SomeClass
{
public function setup()
{
return (new SomeSetterClass())
->setName('John')
->setSurname('Doe');
}
}

?>
-----
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;

use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SomeSetterClass;

final class SomeClass
{
public function setup()
{
$someSetterClass = new SomeSetterClass();
$someSetterClass->setSurname('Doe');
$someSetterClass->setName('John');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;

use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SomeSetterClass;

final class SkipSoleSetter
{
public function setup()
{
return (new SomeSetterClass())
->setName('John');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FluentSettersToStandaloneCallMethodRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source;

final class SomeSetterClass
{
private ?string $name = null;

private ?string $surname = null;

public function setName(string $name): self
{
$this->name = $name;
return $this;
}

public function setSurname(?string $surname): self
{
$this->surname = $surname;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector;

return RectorConfig::configure()
->withRules([FluentSettersToStandaloneCallMethodRector::class]);
7 changes: 6 additions & 1 deletion rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Naming\Naming;

use Nette\Utils\Strings;
use PhpParser\Node\Name;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
Expand Down Expand Up @@ -116,8 +117,12 @@ public function getExpectedNameFromType(Type $type): ?ExpectedName
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
}

public function fqnToVariableName(ThisType | ObjectType | string $objectType): string
public function fqnToVariableName(ThisType | ObjectType | Name | string $objectType): string
{
if ($objectType instanceof Name) {
$objectType = $objectType->toString();
}

if ($objectType instanceof ThisType) {
$objectType = $objectType->getStaticObjectType();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace Rector\Unambiguous\Rector\Expression;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @experimental since 2025-11
*
* @see \Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\FluentSettersToStandaloneCallMethodRectorTest
*/
final class FluentSettersToStandaloneCallMethodRector extends AbstractRector
{
public function __construct(
private readonly PropertyNaming $propertyNaming
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change fluent setter chain calls, to standalone line of setters',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return (new SomeFluentClass())
->setName('John')
->setAge(30);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$someFluentClass = new SomeFluentClass();
$someFluentClass->setName('John');
$someFluentClass->setAge(30);

return $someFluentClass;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class, Return_::class];
}

/**
* @param Expression|Return_ $node
*/
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof MethodCall) {
return null;
}

$firstMethodCall = $node->expr;

// must be nested method call, so we avoid only single one
if (! $firstMethodCall->var instanceof MethodCall) {
return null;
}

/** @var MethodCall[] $methodCalls */
$methodCalls = [];

$currentMethodCall = $firstMethodCall;
while ($currentMethodCall instanceof MethodCall) {
$methodCalls[] = $currentMethodCall;
$currentMethodCall = $currentMethodCall->var;
}

// at least 2 method calls
if (count($methodCalls) < 1) {
return null;
}

$rootExpr = $currentMethodCall;

$variableName = $this->resolveVariableName($rootExpr);
$someVariable = new Variable($variableName);

$firstAssign = new Assign($someVariable, $rootExpr);
$stmts = [new Expression($firstAssign)];

foreach ($methodCalls as $methodCall) {
$methodCall->var = $someVariable;
// inlines indent and removes () around first expr
$methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$stmts[] = new Expression($methodCall);
}

$node->expr = $someVariable;

return $stmts;
}

private function resolveVariableName(Expr $expr): string
{
if (! $expr instanceof New_) {
return 'someVariable';
}

if ($expr->class instanceof Name) {
return $this->propertyNaming->fqnToVariableName($expr->class);
}

return 'someVariable';
}
}
1 change: 0 additions & 1 deletion stubs/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@

class QueryBuilder
{

}