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\Ternary;
|
2018-05-13 17:17:15 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\Cast\Bool_;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
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\Ternary\UnnecessaryTernaryExpressionRector\UnnecessaryTernaryExpressionRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class UnnecessaryTernaryExpressionRector extends AbstractRector
|
2018-05-13 17:17:15 +00:00
|
|
|
{
|
2018-05-17 17:05:00 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\AssignAndBinaryMap
|
2018-05-17 17:05:00 +00:00
|
|
|
*/
|
2018-11-07 17:22:27 +00:00
|
|
|
private $assignAndBinaryMap;
|
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(AssignAndBinaryMap $assignAndBinaryMap, ValueResolver $valueResolver)
|
2018-11-07 17:22:27 +00:00
|
|
|
{
|
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2018-11-07 17:22:27 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-05-13 17:17:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unnecessary ternary expressions', [new CodeSample('$foo === $bar ? true : false;', '$foo === $bar;')]);
|
2018-05-13 17:17:15 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-05-13 17:17:15 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2018-08-14 21:33:39 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2018-08-14 21:33:39 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-08-14 21:33:39 +00:00
|
|
|
{
|
2023-08-14 09:58:25 +00:00
|
|
|
if (!$node->if instanceof Expr) {
|
2018-08-14 21:33:39 +00:00
|
|
|
return null;
|
2018-05-13 17:36:01 +00:00
|
|
|
}
|
2023-08-14 09:58:25 +00:00
|
|
|
$ifExpression = $node->if;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isTrueOrFalse($ifExpression)) {
|
2019-01-14 18:21:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-08-14 09:58:25 +00:00
|
|
|
$elseExpression = $node->else;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isTrueOrFalse($elseExpression)) {
|
2018-08-14 21:33:39 +00:00
|
|
|
return null;
|
2018-05-13 17:36:01 +00:00
|
|
|
}
|
2023-08-14 09:58:25 +00:00
|
|
|
$condition = $node->cond;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$condition instanceof BinaryOp) {
|
2018-12-13 18:52:49 +00:00
|
|
|
return $this->processNonBinaryCondition($ifExpression, $elseExpression, $condition);
|
|
|
|
}
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isNull($ifExpression)) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isNull($elseExpression)) {
|
2018-08-14 21:33:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-05-17 17:04:22 +00:00
|
|
|
/** @var BinaryOp $binaryOperation */
|
2018-10-15 04:36:58 +00:00
|
|
|
$binaryOperation = $node->cond;
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isTrue($ifExpression) && $this->valueResolver->isFalse($elseExpression)) {
|
2018-05-17 17:04:22 +00:00
|
|
|
return $binaryOperation;
|
2018-05-13 18:19:51 +00:00
|
|
|
}
|
2018-11-07 17:22:27 +00:00
|
|
|
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOperation);
|
|
|
|
if ($inversedBinaryClass === null) {
|
|
|
|
return null;
|
2018-05-17 17:06:58 +00:00
|
|
|
}
|
2018-11-07 17:22:27 +00:00
|
|
|
return new $inversedBinaryClass($binaryOperation->left, $binaryOperation->right);
|
2018-05-17 17:06:58 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processNonBinaryCondition(Expr $ifExpression, Expr $elseExpression, Expr $condition) : ?Node
|
2018-12-13 18:52:49 +00:00
|
|
|
{
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($this->valueResolver->isTrue($ifExpression) && $this->valueResolver->isFalse($elseExpression)) {
|
2021-04-12 20:02:23 +00:00
|
|
|
return $this->processTrueIfExpressionWithFalseElseExpression($condition);
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isFalse($ifExpression)) {
|
2021-04-12 20:02:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isTrue($elseExpression)) {
|
2021-04-12 20:02:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->processFalseIfExpressionWithTrueElseExpression($condition);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processTrueIfExpressionWithFalseElseExpression(Expr $expr) : Expr
|
2021-04-12 20:02:23 +00:00
|
|
|
{
|
2021-10-07 17:46:41 +00:00
|
|
|
$exprType = $this->getType($expr);
|
2023-04-08 16:37:58 +00:00
|
|
|
if ($exprType->isBoolean()->yes()) {
|
2021-04-12 20:02:23 +00:00
|
|
|
return $expr;
|
2018-12-13 18:52:49 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Bool_($expr);
|
2021-04-12 20:02:23 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processFalseIfExpressionWithTrueElseExpression(Expr $expr) : Expr
|
2021-04-12 20:02:23 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof BooleanNot) {
|
2021-10-07 17:46:41 +00:00
|
|
|
$negatedExprType = $this->getType($expr->expr);
|
2023-04-08 16:37:58 +00:00
|
|
|
if ($negatedExprType->isBoolean()->yes()) {
|
2021-04-12 20:02:23 +00:00
|
|
|
return $expr->expr;
|
2018-12-13 18:52:49 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Bool_($expr->expr);
|
2021-04-12 20:02:23 +00:00
|
|
|
}
|
2021-10-07 17:46:41 +00:00
|
|
|
$exprType = $this->getType($expr);
|
2023-04-08 16:37:58 +00:00
|
|
|
if ($exprType->isBoolean()->yes()) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanNot($expr);
|
2018-12-13 18:52:49 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new BooleanNot(new Bool_($expr));
|
2018-12-13 18:52:49 +00:00
|
|
|
}
|
2018-05-13 17:17:15 +00:00
|
|
|
}
|