2020-10-14 06:24:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-03-03 12:57:04 +01:00
|
|
|
namespace Rector\Defluent\Matcher;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
|
|
|
|
use Rector\Defluent\NodeAnalyzer\FluentChainMethodCallRootExtractor;
|
|
|
|
use Rector\Defluent\NodeAnalyzer\SameClassMethodCallAnalyzer;
|
|
|
|
use Rector\Defluent\NodeFactory\NonFluentChainMethodCallFactory;
|
|
|
|
use Rector\Defluent\Skipper\FluentMethodCallSkipper;
|
2021-01-20 18:41:35 +07:00
|
|
|
use Rector\Defluent\ValueObject\AssignAndRootExpr;
|
2020-10-14 06:24:29 +02:00
|
|
|
use Rector\Defluent\ValueObject\AssignAndRootExprAndNodesToAdd;
|
|
|
|
|
2021-03-03 12:57:04 +01:00
|
|
|
final class AssignAndRootExprAndNodesToAddMatcher
|
2020-10-14 06:24:29 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var FluentChainMethodCallNodeAnalyzer
|
|
|
|
*/
|
2021-03-03 12:57:04 +01:00
|
|
|
private $fluentChainMethodCallNodeAnalyzer;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var NonFluentChainMethodCallFactory
|
|
|
|
*/
|
2021-03-03 12:57:04 +01:00
|
|
|
private $nonFluentChainMethodCallFactory;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-28 08:47:48 +01:00
|
|
|
* @var FluentMethodCallSkipper
|
2020-10-14 06:24:29 +02:00
|
|
|
*/
|
2021-03-03 12:57:04 +01:00
|
|
|
private $fluentMethodCallSkipper;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-28 08:47:48 +01:00
|
|
|
* @var FluentChainMethodCallRootExtractor
|
2020-10-14 06:24:29 +02:00
|
|
|
*/
|
2021-02-28 08:47:48 +01:00
|
|
|
private $fluentChainMethodCallRootExtractor;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-28 08:47:48 +01:00
|
|
|
* @var SameClassMethodCallAnalyzer
|
2020-10-14 06:24:29 +02:00
|
|
|
*/
|
2021-02-28 08:47:48 +01:00
|
|
|
private $sameClassMethodCallAnalyzer;
|
2020-10-14 06:24:29 +02:00
|
|
|
|
2021-03-03 12:57:04 +01:00
|
|
|
public function __construct(
|
2020-10-14 06:24:29 +02:00
|
|
|
FluentChainMethodCallNodeAnalyzer $fluentChainMethodCallNodeAnalyzer,
|
|
|
|
FluentChainMethodCallRootExtractor $fluentChainMethodCallRootExtractor,
|
|
|
|
NonFluentChainMethodCallFactory $nonFluentChainMethodCallFactory,
|
|
|
|
SameClassMethodCallAnalyzer $sameClassMethodCallAnalyzer,
|
|
|
|
FluentMethodCallSkipper $fluentMethodCallSkipper
|
2021-03-03 12:57:04 +01:00
|
|
|
) {
|
2020-10-14 06:24:29 +02:00
|
|
|
$this->fluentChainMethodCallNodeAnalyzer = $fluentChainMethodCallNodeAnalyzer;
|
|
|
|
$this->fluentChainMethodCallRootExtractor = $fluentChainMethodCallRootExtractor;
|
|
|
|
$this->nonFluentChainMethodCallFactory = $nonFluentChainMethodCallFactory;
|
|
|
|
$this->sameClassMethodCallAnalyzer = $sameClassMethodCallAnalyzer;
|
|
|
|
$this->fluentMethodCallSkipper = $fluentMethodCallSkipper;
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:57:04 +01:00
|
|
|
public function match(MethodCall $methodCall, string $kind): ?AssignAndRootExprAndNodesToAdd
|
|
|
|
{
|
2020-10-14 06:24:29 +02:00
|
|
|
$chainMethodCalls = $this->fluentChainMethodCallNodeAnalyzer->collectAllMethodCallsInChain($methodCall);
|
|
|
|
if (! $this->sameClassMethodCallAnalyzer->haveSingleClass($chainMethodCalls)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$assignAndRootExpr = $this->fluentChainMethodCallRootExtractor->extractFromMethodCalls(
|
|
|
|
$chainMethodCalls,
|
|
|
|
$kind
|
|
|
|
);
|
|
|
|
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $assignAndRootExpr instanceof AssignAndRootExpr) {
|
2020-10-14 06:24:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->fluentMethodCallSkipper->shouldSkipMethodCalls($assignAndRootExpr, $chainMethodCalls)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nodesToAdd = $this->nonFluentChainMethodCallFactory->createFromAssignObjectAndMethodCalls(
|
|
|
|
$assignAndRootExpr,
|
|
|
|
$chainMethodCalls,
|
|
|
|
$kind
|
|
|
|
);
|
|
|
|
|
|
|
|
return new AssignAndRootExprAndNodesToAdd($assignAndRootExpr, $nodesToAdd);
|
|
|
|
}
|
|
|
|
}
|