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\CodeQuality\Rector\Identical;
|
2019-02-26 17:19:37 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
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\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\SimplifyBoolIdenticalTrueRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyBoolIdenticalTrueRector extends AbstractRector
|
2019-02-26 17:19:37 +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-02-26 17:19:37 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Simplify bool value compare to true or false', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-02-26 17:19:37 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(bool $value, string $items)
|
|
|
|
{
|
|
|
|
$match = in_array($value, $items, TRUE) === TRUE;
|
2023-09-03 16:38:10 +00:00
|
|
|
|
2019-02-26 17:19:37 +00:00
|
|
|
$match = in_array($value, $items, TRUE) !== FALSE;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-02-26 17:19:37 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(bool $value, string $items)
|
|
|
|
{
|
|
|
|
$match = in_array($value, $items, TRUE);
|
2023-09-03 16:38:10 +00:00
|
|
|
|
2019-02-26 17:19:37 +00:00
|
|
|
$match = in_array($value, $items, TRUE);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-02-26 17:19:37 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-02-26 17:19:37 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-02-26 17:19:37 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Identical::class, NotIdentical::class];
|
2019-02-26 17:19:37 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Identical|NotIdentical $node
|
2019-02-26 17:19:37 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-02-26 17:19:37 +00:00
|
|
|
{
|
2023-09-03 16:38:10 +00:00
|
|
|
if ($this->isBooleanButNotTrueAndFalse($node->left)) {
|
2019-04-03 18:29:46 +00:00
|
|
|
return $this->processBoolTypeToNotBool($node, $node->left, $node->right);
|
|
|
|
}
|
2023-09-03 16:38:10 +00:00
|
|
|
if ($this->isBooleanButNotTrueAndFalse($node->right)) {
|
|
|
|
return $this->processBoolTypeToNotBool($node, $node->right, $node->left);
|
2021-02-21 09:32:45 +00:00
|
|
|
}
|
2023-09-03 16:38:10 +00:00
|
|
|
return null;
|
2019-04-03 18:29:46 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBoolTypeToNotBool(Node $node, Expr $leftExpr, Expr $rightExpr) : ?Expr
|
2019-04-03 18:29:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Identical) {
|
2020-02-09 11:31:31 +00:00
|
|
|
return $this->refactorIdentical($leftExpr, $rightExpr);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof NotIdentical) {
|
2020-02-09 11:31:31 +00:00
|
|
|
return $this->refactorNotIdentical($leftExpr, $rightExpr);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorIdentical(Expr $leftExpr, Expr $rightExpr) : ?Expr
|
2020-02-09 11:31:31 +00:00
|
|
|
{
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isTrue($rightExpr)) {
|
2020-02-09 11:31:31 +00:00
|
|
|
return $leftExpr;
|
2019-04-03 18:29:46 +00:00
|
|
|
}
|
2023-09-03 16:38:10 +00:00
|
|
|
// prevent double negation !!
|
|
|
|
if (!$this->valueResolver->isFalse($rightExpr)) {
|
|
|
|
return null;
|
2020-02-09 11:31:31 +00:00
|
|
|
}
|
2023-09-03 16:38:10 +00:00
|
|
|
if (!$leftExpr instanceof BooleanNot) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $leftExpr->expr;
|
2020-02-09 11:31:31 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorNotIdentical(Expr $leftExpr, Expr $rightExpr) : ?Expr
|
2020-02-09 11:31:31 +00:00
|
|
|
{
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isFalse($rightExpr)) {
|
2020-02-09 11:31:31 +00:00
|
|
|
return $leftExpr;
|
|
|
|
}
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isTrue($rightExpr)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanNot($leftExpr);
|
2019-02-26 17:19:37 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-09-03 16:38:10 +00:00
|
|
|
private function isBooleanButNotTrueAndFalse(Expr $expr) : bool
|
|
|
|
{
|
|
|
|
if ($this->valueResolver->isTrueOrFalse($expr)) {
|
|
|
|
return \false;
|
|
|
|
}
|
2023-10-13 13:49:39 +00:00
|
|
|
return $this->nodeTypeResolver->getNativeType($expr)->isBoolean()->yes();
|
2023-09-03 16:38:10 +00:00
|
|
|
}
|
2019-02-26 17:19:37 +00:00
|
|
|
}
|