2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Plus;
|
2019-06-09 05:08:08 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\AssignOp;
|
|
|
|
use PhpParser\Node\Expr\AssignOp\Div as AssignDiv;
|
|
|
|
use PhpParser\Node\Expr\AssignOp\Minus as AssignMinus;
|
|
|
|
use PhpParser\Node\Expr\AssignOp\Mul as AssignMul;
|
|
|
|
use PhpParser\Node\Expr\AssignOp\Plus as AssignPlus;
|
2022-09-29 16:10:59 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Div;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Minus;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Mul;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Plus;
|
2023-09-19 10:39:33 +00:00
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\UnaryMinus;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-06-09 05:08:08 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/I0BGs
|
2020-05-03 19:30:01 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\RemoveDeadZeroAndOneOperationRectorTest
|
2019-06-09 05:08:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadZeroAndOneOperationRector extends AbstractRector
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove operation with 1 and 0, that have no effect on the value', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-06-09 05:08:08 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = 5 * 1;
|
|
|
|
$value = 5 + 0;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-06-09 05:08:08 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = 5;
|
|
|
|
$value = 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-06-09 05:08:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Plus::class, Minus::class, Mul::class, Div::class, AssignPlus::class, AssignMinus::class, AssignMul::class, AssignDiv::class];
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Plus|Minus|Mul|Div|AssignPlus|AssignMinus|AssignMul|AssignDiv $node
|
2019-06-09 05:08:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof AssignOp) {
|
2021-08-02 17:48:07 +00:00
|
|
|
return $this->processAssignOp($node);
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
|
|
|
// -, +
|
2021-08-02 17:48:07 +00:00
|
|
|
return $this->processBinaryOp($node);
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processAssignOp(AssignOp $assignOp) : ?Expr
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
|
|
|
// +=, -=
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($assignOp instanceof AssignPlus || $assignOp instanceof AssignMinus) {
|
2021-10-23 21:09:26 +00:00
|
|
|
if (!$this->valueResolver->isValue($assignOp->expr, 0)) {
|
2019-06-09 05:08:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-23 21:09:26 +00:00
|
|
|
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
|
|
|
|
return $assignOp->var;
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// *, /
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($assignOp instanceof AssignMul || $assignOp instanceof AssignDiv) {
|
2021-10-23 21:09:26 +00:00
|
|
|
if (!$this->valueResolver->isValue($assignOp->expr, 1)) {
|
2019-06-09 05:08:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-23 21:09:26 +00:00
|
|
|
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
|
|
|
|
return $assignOp->var;
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinaryOp(Node $node) : ?Expr
|
2019-06-09 05:08:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Plus || $node instanceof Minus) {
|
2019-06-09 05:08:08 +00:00
|
|
|
return $this->processBinaryPlusAndMinus($node);
|
|
|
|
}
|
|
|
|
// *, /
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Mul) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return $this->processBinaryMulAndDiv($node);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Div) {
|
2019-06-09 05:08:08 +00:00
|
|
|
return $this->processBinaryMulAndDiv($node);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-29 16:10:59 +00:00
|
|
|
private function areNumberType(BinaryOp $binaryOp) : bool
|
|
|
|
{
|
|
|
|
if (!$this->nodeTypeResolver->isNumberType($binaryOp->left)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $this->nodeTypeResolver->isNumberType($binaryOp->right);
|
|
|
|
}
|
2019-10-30 09:49:07 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\Plus|\PhpParser\Node\Expr\BinaryOp\Minus $binaryOp
|
2019-10-30 09:49:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinaryPlusAndMinus($binaryOp) : ?Expr
|
2019-10-30 09:49:07 +00:00
|
|
|
{
|
2022-09-29 16:10:59 +00:00
|
|
|
if (!$this->areNumberType($binaryOp)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->valueResolver->isValue($binaryOp->left, 0) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp instanceof Minus) {
|
|
|
|
return new UnaryMinus($binaryOp->right);
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2020-01-19 19:45:01 +00:00
|
|
|
return $binaryOp->right;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($binaryOp->right, 0)) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2021-02-21 09:32:45 +00:00
|
|
|
return $binaryOp->left;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\Mul|\PhpParser\Node\Expr\BinaryOp\Div $binaryOp
|
2019-10-30 09:49:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBinaryMulAndDiv($binaryOp) : ?Expr
|
2019-10-30 09:49:07 +00:00
|
|
|
{
|
2023-09-19 10:39:33 +00:00
|
|
|
if ($binaryOp->left instanceof ClassConstFetch || $binaryOp->right instanceof ClassConstFetch) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-29 16:10:59 +00:00
|
|
|
if (!$this->areNumberType($binaryOp)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp instanceof Mul && $this->valueResolver->isValue($binaryOp->left, 1) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
|
2020-01-19 19:45:01 +00:00
|
|
|
return $binaryOp->right;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($binaryOp->right, 1)) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2021-02-21 09:32:45 +00:00
|
|
|
return $binaryOp->left;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2019-06-09 05:08:08 +00:00
|
|
|
}
|