rector/rules/DeadCode/NodeManipulator/CountManipulator.php

98 lines
3.6 KiB
PHP
Raw Normal View History

2020-03-26 21:50:07 +01:00
<?php
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;
use Rector\Core\PhpParser\Comparing\NodeComparator;
2020-03-26 21:50:07 +01:00
use Rector\NodeNameResolver\NodeNameResolver;
final class CountManipulator
{
/**
* @var \Rector\NodeNameResolver\NodeNameResolver
2020-03-26 21:50:07 +01:00
*/
private $nodeNameResolver;
2020-03-26 21:50:07 +01:00
/**
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
2020-03-26 21:50:07 +01:00
*/
private $nodeComparator;
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator)
2020-03-26 21:50:07 +01:00
{
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeComparator = $nodeComparator;
2020-03-26 21:50:07 +01: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
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
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)
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)
if ($node instanceof \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual) {
2020-03-26 21:50:07 +01:00
return $this->processSmallerOrEqual($node, $expr);
}
return \false;
2020-03-26 21:50:07 +01:00
}
private function processGreater(\PhpParser\Node\Expr\BinaryOp\Greater $greater, \PhpParser\Node\Expr $expr) : bool
2020-03-26 21:50:07 +01:00
{
if (!$this->isNumber($greater->right, 0)) {
return \false;
2020-03-26 21:50:07 +01:00
}
return $this->isCountWithExpression($greater->left, $expr);
2020-03-26 21:50:07 +01:00
}
private function processGreaterOrEqual(\PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $greaterOrEqual, \PhpParser\Node\Expr $expr) : bool
2020-03-26 21:50:07 +01:00
{
if (!$this->isNumber($greaterOrEqual->right, 1)) {
return \false;
2020-03-26 21:50:07 +01:00
}
return $this->isCountWithExpression($greaterOrEqual->left, $expr);
2020-03-26 21:50:07 +01:00
}
private function processSmaller(\PhpParser\Node\Expr\BinaryOp\Smaller $smaller, \PhpParser\Node\Expr $expr) : bool
2020-03-26 21:50:07 +01:00
{
if (!$this->isNumber($smaller->left, 0)) {
return \false;
2020-03-26 21:50:07 +01:00
}
return $this->isCountWithExpression($smaller->right, $expr);
}
private function processSmallerOrEqual(\PhpParser\Node\Expr\BinaryOp\SmallerOrEqual $smallerOrEqual, \PhpParser\Node\Expr $expr) : bool
2020-03-26 21:50:07 +01:00
{
if (!$this->isNumber($smallerOrEqual->left, 1)) {
return \false;
2020-03-26 21:50:07 +01:00
}
return $this->isCountWithExpression($smallerOrEqual->right, $expr);
2020-03-26 21:50:07 +01:00
}
private function isNumber(\PhpParser\Node $node, int $value) : bool
2020-03-26 21:50:07 +01:00
{
if (!$node instanceof \PhpParser\Node\Scalar\LNumber) {
return \false;
2020-03-26 21:50:07 +01:00
}
return $node->value === $value;
2020-03-26 21:50:07 +01:00
}
private function isCountWithExpression(\PhpParser\Node $node, \PhpParser\Node\Expr $expr) : bool
2020-03-26 21:50:07 +01:00
{
if (!$node instanceof \PhpParser\Node\Expr\FuncCall) {
return \false;
2020-03-26 21:50:07 +01:00
}
if (!$this->nodeNameResolver->isName($node, 'count')) {
return \false;
2020-03-26 21:50:07 +01:00
}
$countedExpr = $node->args[0]->value;
return $this->nodeComparator->areNodesEqual($countedExpr, $expr);
2020-03-26 21:50:07 +01:00
}
}