2020-04-13 23:44:44 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-04-14 10:49:58 +02:00
|
|
|
namespace Rector\NodeNestingScope;
|
2020-04-13 23:44:44 +02:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-02-13 14:25:47 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2020-10-12 16:34:28 +02:00
|
|
|
use PhpParser\Node\FunctionLike;
|
2021-02-13 14:25:47 +01:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2021-02-13 23:26:00 +01:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-02-19 13:01:23 +01:00
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
2020-04-13 23:44:44 +02:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-04-01 03:55:44 +02:00
|
|
|
use Rector\NodeNestingScope\ValueObject\ControlStructure;
|
2020-04-13 23:44:44 +02:00
|
|
|
final class ScopeNestingComparator
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var Expr[]
|
2020-04-13 23:44:44 +02:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $doubleIfBranchExprs = [];
|
2021-01-19 20:11:10 +01:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
2021-01-19 20:11:10 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $betterNodeFinder;
|
2021-02-13 14:25:47 +01:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
2021-02-13 14:25:47 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $nodeComparator;
|
2021-07-15 03:40:51 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\NodeNestingScope\ParentFinder
|
|
|
|
*/
|
|
|
|
private $parentFinder;
|
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator, \Rector\NodeNestingScope\ParentFinder $parentFinder)
|
2020-04-13 23:44:44 +02:00
|
|
|
{
|
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-02-19 13:01:23 +01:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2021-07-15 03:40:51 +00:00
|
|
|
$this->parentFinder = $parentFinder;
|
2020-04-13 23:44:44 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function areReturnScopeNested(\PhpParser\Node\Stmt\Return_ $return, \PhpParser\Node $secondNodeScopeNode) : bool
|
2021-02-13 23:26:00 +01:00
|
|
|
{
|
2021-07-15 03:40:51 +00:00
|
|
|
$firstNodeScopeNode = $this->parentFinder->findByTypes($return, \Rector\NodeNestingScope\ValueObject\ControlStructure::RETURN_ISOLATING_SCOPE_NODE_TYPES);
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->areNodesEqual($firstNodeScopeNode, $secondNodeScopeNode);
|
2021-02-13 23:26:00 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function areScopeNestingEqual(\PhpParser\Node $firstNode, \PhpParser\Node $secondNode) : bool
|
2020-04-13 23:44:44 +02:00
|
|
|
{
|
|
|
|
$firstNodeScopeNode = $this->findParentControlStructure($firstNode);
|
|
|
|
$secondNodeScopeNode = $this->findParentControlStructure($secondNode);
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->areNodesEqual($firstNodeScopeNode, $secondNodeScopeNode);
|
2020-04-13 23:44:44 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isNodeConditionallyScoped(\PhpParser\Node\Expr $expr) : bool
|
2020-10-12 16:34:28 +02:00
|
|
|
{
|
2021-07-15 03:40:51 +00:00
|
|
|
$foundParent = $this->parentFinder->findByTypes($expr, \Rector\NodeNestingScope\ValueObject\ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES + [\PhpParser\Node\FunctionLike::class]);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$foundParent instanceof \PhpParser\Node) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
|
|
|
// is in both if/else branches
|
|
|
|
if ($this->isInBothIfElseBranch($foundParent, $expr)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$foundParent instanceof \PhpParser\Node\Stmt\Else_) {
|
|
|
|
return !$foundParent instanceof \PhpParser\Node\FunctionLike;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($expr, $this->doubleIfBranchExprs)) {
|
2021-05-10 22:23:08 +00:00
|
|
|
return !$foundParent instanceof \PhpParser\Node\FunctionLike;
|
2021-02-21 16:32:45 +07:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isInBothIfElseBranch(\PhpParser\Node $foundParentNode, \PhpParser\Node\Expr $seekedExpr) : bool
|
2021-02-13 14:25:47 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($foundParentNode instanceof \PhpParser\Node\Stmt\Else_) {
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->isNodeEqual($seekedExpr, $this->doubleIfBranchExprs);
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$foundParentNode instanceof \PhpParser\Node\Stmt\If_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$foundIfNode = $this->betterNodeFinder->find($foundParentNode->stmts, function ($node) use($seekedExpr) : bool {
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->areNodesEqual($node, $seekedExpr);
|
2021-02-13 14:25:47 +01:00
|
|
|
});
|
|
|
|
if ($foundParentNode->else === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-10-12 16:34:28 +02:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$foundElseNode = $this->betterNodeFinder->find($foundParentNode->else, function ($node) use($seekedExpr) : bool {
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->areNodesEqual($node, $seekedExpr);
|
2021-02-13 14:25:47 +01:00
|
|
|
});
|
|
|
|
if ($foundIfNode && $foundElseNode) {
|
|
|
|
$this->doubleIfBranchExprs[] = $seekedExpr;
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-02-13 14:25:47 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-10-12 16:34:28 +02:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function findParentControlStructure(\PhpParser\Node $node) : ?\PhpParser\Node
|
2020-04-13 23:44:44 +02:00
|
|
|
{
|
2021-07-15 03:40:51 +00:00
|
|
|
return $this->parentFinder->findByTypes($node, \Rector\NodeNestingScope\ValueObject\ControlStructure::BREAKING_SCOPE_NODE_TYPES);
|
2020-04-13 23:44:44 +02:00
|
|
|
}
|
|
|
|
}
|