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
60 changes: 60 additions & 0 deletions rules/Unambiguous/NodeAnalyzer/FluentMethodCallsCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rector\Unambiguous\NodeAnalyzer;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\NodeTypeResolver;

final readonly class FluentMethodCallsCollector
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver
) {
}

/**
* @return MethodCall[]
*/
public function resolve(MethodCall $firstMethodCall): array
{
// must be nested method call, so we avoid only single one
if (! $firstMethodCall->var instanceof MethodCall) {
return [];
}

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

$currentMethodCall = $firstMethodCall;
$classNameObjectType = null;
while ($currentMethodCall instanceof MethodCall) {
if ($currentMethodCall->isFirstClassCallable()) {
return [];
}

// must be exactly one argument
if (count($currentMethodCall->getArgs()) !== 1) {
return [];
}

$objectType = $this->nodeTypeResolver->getType($currentMethodCall->var);
if (! $objectType instanceof ObjectType) {
return [];
}

if ($classNameObjectType === null) {
$classNameObjectType = $objectType->getClassName();
} elseif ($classNameObjectType !== $objectType->getClassName()) {
return [];
}

$methodCalls[] = $currentMethodCall;
$currentMethodCall = $currentMethodCall->var;
}

return $methodCalls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Unambiguous\NodeAnalyzer\FluentMethodCallsCollector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,7 +31,8 @@
final class FluentSettersToStandaloneCallMethodRector extends AbstractRector
{
public function __construct(
private readonly PropertyNaming $propertyNaming
private readonly PropertyNaming $propertyNaming,
private readonly FluentMethodCallsCollector $fluentMethodCallsCollector
) {
}

Expand Down Expand Up @@ -87,54 +89,21 @@ public function refactor(Node $node): ?array
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;
$classNameObjectType = null;
while ($currentMethodCall instanceof MethodCall) {
if ($currentMethodCall->isFirstClassCallable()) {
return null;
}

// must be exactly one argument
if (count($currentMethodCall->getArgs()) !== 1) {
return null;
}

$objectType = $this->getType($currentMethodCall->var);
if (! $objectType instanceof ObjectType) {
return null;
}

if ($classNameObjectType === null) {
$classNameObjectType = $objectType->getClassName();
} elseif ($classNameObjectType !== $objectType->getClassName()) {
return null;
}

$methodCalls[] = $currentMethodCall;
$currentMethodCall = $currentMethodCall->var;
}
$methodCalls = $this->fluentMethodCallsCollector->resolve($node->expr);

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

$rootExpr = $currentMethodCall;
$lastMethodCall = end($methodCalls);
$rootExpr = $lastMethodCall->var;

if (! $rootExpr instanceof New_) {
return null;
}

if ($this->shouldSkipForVendorOrInternal($firstMethodCall)) {
if ($this->shouldSkipForVendorOrInternal($node->expr)) {
return null;
}

Expand Down