2021-03-03 12:57:04 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-03-03 12:57:04 +01:00
|
|
|
namespace Rector\Defluent\NodeAnalyzer;
|
|
|
|
|
2021-08-14 13:04:02 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Cast;
|
2021-03-03 12:57:04 +01:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
2021-08-14 13:04:02 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-03-03 12:57:04 +01:00
|
|
|
use Rector\Defluent\Skipper\FluentMethodCallSkipper;
|
2021-08-14 13:04:02 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-03-03 12:57:04 +01:00
|
|
|
final class MethodCallSkipAnalyzer
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Defluent\Skipper\FluentMethodCallSkipper
|
2021-03-03 12:57:04 +01:00
|
|
|
*/
|
|
|
|
private $fluentMethodCallSkipper;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer
|
2021-03-03 12:57:04 +01:00
|
|
|
*/
|
|
|
|
private $fluentChainMethodCallNodeAnalyzer;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Defluent\Skipper\FluentMethodCallSkipper $fluentMethodCallSkipper, \Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer $fluentChainMethodCallNodeAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-03 12:57:04 +01:00
|
|
|
$this->fluentMethodCallSkipper = $fluentMethodCallSkipper;
|
|
|
|
$this->fluentChainMethodCallNodeAnalyzer = $fluentChainMethodCallNodeAnalyzer;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function shouldSkipMethodCallIncludingNew(\PhpParser\Node\Expr\MethodCall $methodCall) : bool
|
2021-03-03 12:57:04 +01:00
|
|
|
{
|
|
|
|
if ($this->fluentMethodCallSkipper->shouldSkipRootMethodCall($methodCall)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-03-03 12:57:04 +01:00
|
|
|
}
|
|
|
|
$chainRootExpr = $this->fluentChainMethodCallNodeAnalyzer->resolveRootExpr($methodCall);
|
2021-05-10 22:23:08 +00:00
|
|
|
return $chainRootExpr instanceof \PhpParser\Node\Expr\New_;
|
2021-03-03 12:57:04 +01:00
|
|
|
}
|
2021-07-10 11:50:43 +00:00
|
|
|
public function shouldSkipLastCallNotReturnThis(\PhpParser\Node\Expr\MethodCall $methodCall) : bool
|
|
|
|
{
|
|
|
|
return !$this->fluentChainMethodCallNodeAnalyzer->isMethodCallReturnThis($methodCall);
|
|
|
|
}
|
2021-08-14 13:04:02 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\Cast $node
|
2021-08-14 13:04:02 +00:00
|
|
|
*/
|
|
|
|
public function shouldSkipDependsWithOtherExpr($node) : bool
|
|
|
|
{
|
|
|
|
$parentNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if (!$parentNode instanceof \PhpParser\Node) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($parentNode instanceof \PhpParser\Node\Stmt\Return_) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\Assign) {
|
|
|
|
return !$parentNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE) instanceof \PhpParser\Node\Stmt\Expression;
|
|
|
|
}
|
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\Cast) {
|
|
|
|
return $this->shouldSkipDependsWithOtherExpr($parentNode);
|
|
|
|
}
|
|
|
|
return !$parentNode instanceof \PhpParser\Node\Stmt\Expression;
|
|
|
|
}
|
2021-03-03 12:57:04 +01:00
|
|
|
}
|