|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Arg; |
9 | 9 | use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\Assign; |
10 | 11 | use PhpParser\Node\Expr\ConstFetch; |
11 | 12 | use PhpParser\Node\Expr\FuncCall; |
12 | 13 | use PhpParser\Node\Expr\Ternary; |
13 | 14 | use PhpParser\Node\Scalar\String_; |
| 15 | +use PhpParser\Node\Stmt\Expression; |
| 16 | +use PhpParser\Node\Stmt\If_; |
| 17 | +use PhpParser\Node\Stmt\Return_; |
14 | 18 | use PhpParser\NodeVisitor; |
15 | 19 | use PHPStan\Type\IntegerRangeType; |
16 | 20 | use Rector\DeadCode\ConditionResolver; |
@@ -80,14 +84,22 @@ public function run() |
80 | 84 | */ |
81 | 85 | public function getNodeTypes(): array |
82 | 86 | { |
83 | | - return [Ternary::class, FuncCall::class]; |
| 87 | + return [If_::class, Ternary::class, FuncCall::class]; |
84 | 88 | } |
85 | 89 |
|
86 | 90 | /** |
87 | | - * @param Ternary|FuncCall $node |
| 91 | + * @param If_|Ternary|FuncCall $node |
88 | 92 | */ |
89 | 93 | public function refactor(Node $node): null|int|Node |
90 | 94 | { |
| 95 | + if ($node instanceof If_) { |
| 96 | + if ($this->isVersionCompareIf($node)) { |
| 97 | + return NodeVisitor::DONT_TRAVERSE_CHILDREN; |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
91 | 103 | if ($node instanceof Ternary) { |
92 | 104 | if ($this->isVersionCompareTernary($node)) { |
93 | 105 | return NodeVisitor::DONT_TRAVERSE_CHILDREN; |
@@ -118,6 +130,36 @@ public function refactor(Node $node): null|int|Node |
118 | 130 | return $node; |
119 | 131 | } |
120 | 132 |
|
| 133 | + private function isVersionCompareIf(If_ $if): bool |
| 134 | + { |
| 135 | + if ($if->cond instanceof FuncCall) { |
| 136 | + // per use case reported only |
| 137 | + if (count($if->stmts) !== 1) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + $versionCompare = $this->conditionResolver->resolveFromExpr($if->cond); |
| 142 | + |
| 143 | + if (! $versionCompare instanceof VersionCompareCondition || $versionCompare->getSecondVersion() !== 80100) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + if ($versionCompare->getCompareSign() !== '>=') { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + if ($if->stmts[0] instanceof Expression && $if->stmts[0]->expr instanceof Assign && $if->stmts[0]->expr->expr instanceof FuncCall) { |
| 152 | + return $this->isName($if->stmts[0]->expr->expr, 'hash'); |
| 153 | + } |
| 154 | + |
| 155 | + if ($if->stmts[0] instanceof Return_ && $if->stmts[0]->expr instanceof FuncCall) { |
| 156 | + return $this->isName($if->stmts[0]->expr, 'hash'); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
121 | 163 | private function isVersionCompareTernary(Ternary $ternary): bool |
122 | 164 | { |
123 | 165 | if ($ternary->if instanceof Expr && $ternary->cond instanceof FuncCall) { |
|
0 commit comments