-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
UnnecessaryTernaryExpressionRector.php
119 lines (117 loc) · 4.11 KB
/
UnnecessaryTernaryExpressionRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\Ternary;
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;
use Rector\PhpParser\Node\AssignAndBinaryMap;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector\UnnecessaryTernaryExpressionRectorTest
*/
final class UnnecessaryTernaryExpressionRector extends AbstractRector
{
/**
* @readonly
*/
private AssignAndBinaryMap $assignAndBinaryMap;
/**
* @readonly
*/
private ValueResolver $valueResolver;
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, ValueResolver $valueResolver)
{
$this->assignAndBinaryMap = $assignAndBinaryMap;
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove unnecessary ternary expressions', [new CodeSample('$foo === $bar ? true : false;', '$foo === $bar;')]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Ternary::class];
}
/**
* @param Ternary $node
*/
public function refactor(Node $node) : ?Node
{
if (!$node->if instanceof Expr) {
return null;
}
$ifExpression = $node->if;
if (!$this->valueResolver->isTrueOrFalse($ifExpression)) {
return null;
}
$elseExpression = $node->else;
if (!$this->valueResolver->isTrueOrFalse($elseExpression)) {
return null;
}
$condition = $node->cond;
if (!$condition instanceof BinaryOp) {
return $this->processNonBinaryCondition($ifExpression, $elseExpression, $condition);
}
if ($this->valueResolver->isNull($ifExpression)) {
return null;
}
if ($this->valueResolver->isNull($elseExpression)) {
return null;
}
/** @var BinaryOp $binaryOperation */
$binaryOperation = $node->cond;
if ($this->valueResolver->isTrue($ifExpression) && $this->valueResolver->isFalse($elseExpression)) {
return $binaryOperation;
}
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOperation);
if ($inversedBinaryClass === null) {
return null;
}
return new $inversedBinaryClass($binaryOperation->left, $binaryOperation->right);
}
private function processNonBinaryCondition(Expr $ifExpression, Expr $elseExpression, Expr $condition) : ?Node
{
if ($this->valueResolver->isTrue($ifExpression) && $this->valueResolver->isFalse($elseExpression)) {
return $this->processTrueIfExpressionWithFalseElseExpression($condition);
}
if (!$this->valueResolver->isFalse($ifExpression)) {
return null;
}
if (!$this->valueResolver->isTrue($elseExpression)) {
return null;
}
return $this->processFalseIfExpressionWithTrueElseExpression($condition);
}
private function processTrueIfExpressionWithFalseElseExpression(Expr $expr) : Expr
{
$exprType = $this->getType($expr);
if ($exprType->isBoolean()->yes()) {
return $expr;
}
return new Bool_($expr);
}
private function processFalseIfExpressionWithTrueElseExpression(Expr $expr) : Expr
{
if ($expr instanceof BooleanNot) {
$negatedExprType = $this->getType($expr->expr);
if ($negatedExprType->isBoolean()->yes()) {
return $expr->expr;
}
return new Bool_($expr->expr);
}
$exprType = $this->getType($expr);
if ($exprType->isBoolean()->yes()) {
return new BooleanNot($expr);
}
return new BooleanNot(new Bool_($expr));
}
}