2020-03-26 21:50:07 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-03-26 21:50:07 +01:00
|
|
|
namespace Rector\DeadCode\NodeManipulator;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Greater;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Smaller;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
2021-02-19 13:01:23 +01:00
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
2020-03-26 21:50:07 +01:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
final class CountManipulator
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2020-03-26 21:50:07 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $nodeNameResolver;
|
2020-03-26 21:50:07 +01:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
2020-03-26 21:50:07 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $nodeComparator;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator)
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2021-02-19 13:01:23 +01:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isCounterHigherThanOne(\PhpParser\Node $node, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
|
|
|
// e.g. count($values) > 0
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($node instanceof \PhpParser\Node\Expr\BinaryOp\Greater) {
|
2020-03-26 21:50:07 +01:00
|
|
|
return $this->processGreater($node, $expr);
|
|
|
|
}
|
|
|
|
// e.g. count($values) >= 1
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($node instanceof \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual) {
|
2020-03-26 21:50:07 +01:00
|
|
|
return $this->processGreaterOrEqual($node, $expr);
|
|
|
|
}
|
|
|
|
// e.g. 0 < count($values)
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($node instanceof \PhpParser\Node\Expr\BinaryOp\Smaller) {
|
2020-03-26 21:50:07 +01:00
|
|
|
return $this->processSmaller($node, $expr);
|
|
|
|
}
|
|
|
|
// e.g. 1 <= count($values)
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($node instanceof \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual) {
|
2020-03-26 21:50:07 +01:00
|
|
|
return $this->processSmallerOrEqual($node, $expr);
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function processGreater(\PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNumber($greater->right, 0)) {
|
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2020-04-26 02:57:47 +02:00
|
|
|
return $this->isCountWithExpression($greater->left, $expr);
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function processGreaterOrEqual(\PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNumber($greaterOrEqual->right, 1)) {
|
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2020-04-26 02:57:47 +02:00
|
|
|
return $this->isCountWithExpression($greaterOrEqual->left, $expr);
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function processSmaller(\PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNumber($smaller->left, 0)) {
|
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
|
|
|
return $this->isCountWithExpression($smaller->right, $expr);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function processSmallerOrEqual(\PhpParser\Node\Expr\BinaryOp\SmallerOrEqual $smallerOrEqual, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNumber($smallerOrEqual->left, 1)) {
|
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2020-04-26 02:57:47 +02:00
|
|
|
return $this->isCountWithExpression($smallerOrEqual->right, $expr);
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isNumber(\PhpParser\Node $node, int $value) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$node instanceof \PhpParser\Node\Scalar\LNumber) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2020-04-26 02:57:47 +02:00
|
|
|
return $node->value === $value;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isCountWithExpression(\PhpParser\Node $node, \PhpParser\Node\Expr $expr) : bool
|
2020-03-26 21:50:07 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\FuncCall) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeNameResolver->isName($node, 'count')) {
|
|
|
|
return \false;
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
|
|
|
$countedExpr = $node->args[0]->value;
|
2021-02-19 13:01:23 +01:00
|
|
|
return $this->nodeComparator->areNodesEqual($countedExpr, $expr);
|
2020-03-26 21:50:07 +01:00
|
|
|
}
|
|
|
|
}
|