mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
adc6b8148a
f7eeebaa7b
[DeadCode] Fix non-null type check on RemoveUnusedNonEmptyArrayBeforeForeachRector (#430)
123 lines
4.4 KiB
PHP
123 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr;
|
|
use PhpParser\Node\Expr\Array_;
|
|
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
use PhpParser\Node\Expr\Empty_;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PhpParser\Node\NullableType;
|
|
use PhpParser\Node\Param;
|
|
use PhpParser\Node\Stmt\If_;
|
|
use PHPStan\Type\ArrayType;
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
final class UselessIfCondBeforeForeachDetector
|
|
{
|
|
/**
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
*/
|
|
private $nodeTypeResolver;
|
|
/**
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
|
*/
|
|
private $nodeComparator;
|
|
/**
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
|
*/
|
|
private $betterNodeFinder;
|
|
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder)
|
|
{
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
$this->nodeComparator = $nodeComparator;
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
}
|
|
/**
|
|
* Matches:
|
|
* !empty($values)
|
|
*/
|
|
public function isMatchingNotEmpty(\PhpParser\Node\Stmt\If_ $if, \PhpParser\Node\Expr $foreachExpr) : bool
|
|
{
|
|
$cond = $if->cond;
|
|
if (!$cond instanceof \PhpParser\Node\Expr\BooleanNot) {
|
|
return \false;
|
|
}
|
|
if (!$cond->expr instanceof \PhpParser\Node\Expr\Empty_) {
|
|
return \false;
|
|
}
|
|
/** @var Empty_ $empty */
|
|
$empty = $cond->expr;
|
|
if (!$this->nodeComparator->areNodesEqual($empty->expr, $foreachExpr)) {
|
|
return \false;
|
|
}
|
|
// is array though?
|
|
$arrayType = $this->nodeTypeResolver->resolve($empty->expr);
|
|
if (!$arrayType instanceof \PHPStan\Type\ArrayType) {
|
|
return \false;
|
|
}
|
|
$previousParam = $this->fromPreviousParam($foreachExpr);
|
|
if (!$previousParam instanceof \PhpParser\Node\Param) {
|
|
return \true;
|
|
}
|
|
return !$previousParam->type instanceof \PhpParser\Node\NullableType;
|
|
}
|
|
/**
|
|
* Matches:
|
|
* $values !== []
|
|
* $values != []
|
|
* [] !== $values
|
|
* [] != $values
|
|
*/
|
|
public function isMatchingNotIdenticalEmptyArray(\PhpParser\Node\Stmt\If_ $if, \PhpParser\Node\Expr $foreachExpr) : bool
|
|
{
|
|
if (!$if->cond instanceof \PhpParser\Node\Expr\BinaryOp\NotIdentical && !$if->cond instanceof \PhpParser\Node\Expr\BinaryOp\NotEqual) {
|
|
return \false;
|
|
}
|
|
/** @var NotIdentical|NotEqual $notIdentical */
|
|
$notIdentical = $if->cond;
|
|
return $this->isMatchingNotBinaryOp($notIdentical, $foreachExpr);
|
|
}
|
|
private function fromPreviousParam(\PhpParser\Node\Expr $expr) : ?\PhpParser\Node
|
|
{
|
|
return $this->betterNodeFinder->findFirstPreviousOfNode($expr, function (\PhpParser\Node $node) use($expr) : bool {
|
|
if (!$node instanceof \PhpParser\Node\Param) {
|
|
return \false;
|
|
}
|
|
if (!$node->var instanceof \PhpParser\Node\Expr\Variable) {
|
|
return \false;
|
|
}
|
|
return $this->nodeComparator->areNodesEqual($node->var, $expr);
|
|
});
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Expr\BinaryOp\NotIdentical|\PhpParser\Node\Expr\BinaryOp\NotEqual $binaryOp
|
|
*/
|
|
private function isMatchingNotBinaryOp($binaryOp, \PhpParser\Node\Expr $foreachExpr) : bool
|
|
{
|
|
if ($this->isEmptyArrayAndForeachedVariable($binaryOp->left, $binaryOp->right, $foreachExpr)) {
|
|
return \true;
|
|
}
|
|
return $this->isEmptyArrayAndForeachedVariable($binaryOp->right, $binaryOp->left, $foreachExpr);
|
|
}
|
|
private function isEmptyArrayAndForeachedVariable(\PhpParser\Node\Expr $leftExpr, \PhpParser\Node\Expr $rightExpr, \PhpParser\Node\Expr $foreachExpr) : bool
|
|
{
|
|
if (!$this->isEmptyArray($leftExpr)) {
|
|
return \false;
|
|
}
|
|
return $this->nodeComparator->areNodesEqual($foreachExpr, $rightExpr);
|
|
}
|
|
private function isEmptyArray(\PhpParser\Node\Expr $expr) : bool
|
|
{
|
|
if (!$expr instanceof \PhpParser\Node\Expr\Array_) {
|
|
return \false;
|
|
}
|
|
return $expr->items === [];
|
|
}
|
|
}
|