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\BooleanAnd;
|
2019-05-05 16:48:45 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
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-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector\RemoveAndTrueRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveAndTrueRector extends AbstractRector
|
2019-05-05 16:48:45 +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-05-05 16:48:45 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove and true that has no added value', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-05-05 16:48:45 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return true && 5 === 1;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-05-05 16:48:45 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 5 === 1;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-05-05 16:48:45 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-05-05 16:48:45 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-05-05 16:48:45 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [BooleanAnd::class];
|
2019-05-05 16:48:45 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param BooleanAnd $node
|
2019-05-05 16:48:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-05-05 16:48:45 +00:00
|
|
|
{
|
2019-05-07 11:39:14 +00:00
|
|
|
if ($this->isTrueOrBooleanAndTrues($node->left)) {
|
2019-05-05 16:48:45 +00:00
|
|
|
return $node->right;
|
|
|
|
}
|
2019-05-07 11:39:14 +00:00
|
|
|
if ($this->isTrueOrBooleanAndTrues($node->right)) {
|
2019-05-05 16:48:45 +00:00
|
|
|
return $node->left;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isTrueOrBooleanAndTrues(Expr $expr) : bool
|
2019-05-05 16:48:45 +00:00
|
|
|
{
|
2021-10-23 21:09:26 +00:00
|
|
|
if ($this->valueResolver->isTrue($expr)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-05-07 11:39:14 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof BooleanAnd) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-05-07 12:28:16 +00:00
|
|
|
}
|
2021-10-23 21:09:26 +00:00
|
|
|
if (!$this->isTrueOrBooleanAndTrues($expr->left)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-05-05 16:48:45 +00:00
|
|
|
}
|
2021-10-23 21:09:26 +00:00
|
|
|
return $this->isTrueOrBooleanAndTrues($expr->right);
|
2019-05-05 16:48:45 +00:00
|
|
|
}
|
|
|
|
}
|