Repository navigation
Expand file tree
/
Copy pathInlineIfToExplicitIfRector.php
More file actions
160 lines (134 loc) · 4.07 KB
/
Copy pathInlineIfToExplicitIfRector.php
File metadata and controls
160 lines (134 loc) · 4.07 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\NodeManipulator\BinaryOpManipulator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector\InlineIfToExplicitIfRectorTest
*/
final class InlineIfToExplicitIfRector extends AbstractRector
{
public function __construct(
private readonly BinaryOpManipulator $binaryOpManipulator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change inline if to explicit if', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$userId = null;
is_null($userId) && $userId = 5;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$userId = null;
if (is_null($userId)) {
$userId = 5;
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
*/
public function refactor(Node $node): ?Node
{
if ($node->expr instanceof BooleanAnd) {
return $this->processExplicitIf($node);
}
if ($node->expr instanceof BooleanOr) {
return $this->processExplicitIf($node);
}
return null;
}
private function processExplicitIf(Expression $expression): ?Node
{
/** @var BooleanAnd|BooleanOr $booleanExpr */
$booleanExpr = $expression->expr;
$exprLeft = $booleanExpr->left instanceof BooleanNot
? $booleanExpr->left->expr
: $booleanExpr->left;
if ($exprLeft instanceof FuncCall && $this->isName($exprLeft, 'defined')) {
return null;
}
if ($this->isSameLocalMethodCallChain($booleanExpr)) {
return null;
}
$leftStaticType = $this->getType($booleanExpr->left);
if (! $leftStaticType->isBoolean()->yes()) {
return null;
}
/** @var Expr $expr */
$expr = $booleanExpr instanceof BooleanAnd
? $booleanExpr->left
: $this->binaryOpManipulator->inverseNode($booleanExpr->left);
$if = new If_($expr);
$if->stmts[] = new Expression($booleanExpr->right);
$this->mirrorComments($if, $expression);
return $if;
}
private function isSameLocalMethodCallChain(BooleanAnd|BooleanOr $booleanExpr): bool
{
$leaves = $this->collectOperands($booleanExpr);
$firstMethodCall = null;
foreach ($leaves as $leaf) {
if (! $leaf instanceof MethodCall) {
return false;
}
if (! $leaf->var instanceof Variable || ! $this->isName($leaf->var, 'this')) {
return false;
}
if (! $firstMethodCall instanceof MethodCall) {
$firstMethodCall = $leaf;
continue;
}
if (! $this->nodeNameResolver->areNamesEqual($firstMethodCall->name, $leaf->name)) {
return false;
}
}
return $firstMethodCall instanceof MethodCall;
}
/**
* @return Expr[]
*/
private function collectOperands(Expr $expr): array
{
if ($expr instanceof BooleanAnd || $expr instanceof BooleanOr) {
return array_merge($this->collectOperands($expr->left), $this->collectOperands($expr->right));
}
return [$expr];
}
}