Skip to content

Commit b78ed96

Browse files
authored
[DowngradePhp81] Skip check version_compare with if on DowngradeHashAlgorithmXxHashRector (part 3) (#309)
* [DowngradePhp81] Handle version_compare() in If_ on DowngradeHashAlgorithmXxHashRector * [DowngradePhp81] Handle version_compare() in If_ on DowngradeHashAlgorithmXxHashRector
1 parent 4561f51 commit b78ed96

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;
4+
5+
final class SkipCheckVersionCompareOnIf
6+
{
7+
public function run_v()
8+
{
9+
if ( version_compare( PHP_VERSION, '8.1', '>=' ) ) {
10+
return hash( 'xxh128', $value );
11+
}
12+
13+
return hash( 'md4', $value );
14+
}
15+
16+
public function run_v2()
17+
{
18+
if (version_compare( PHP_VERSION, '8.1', '>=' ) ) {
19+
$hash = hash( 'xxh128', $value );
20+
} else {
21+
$hash = hash( 'md4', $value );
22+
}
23+
24+
return $hash;
25+
}
26+
}

rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr;
10+
use PhpParser\Node\Expr\Assign;
1011
use PhpParser\Node\Expr\ConstFetch;
1112
use PhpParser\Node\Expr\FuncCall;
1213
use PhpParser\Node\Expr\Ternary;
1314
use PhpParser\Node\Scalar\String_;
15+
use PhpParser\Node\Stmt\Expression;
16+
use PhpParser\Node\Stmt\If_;
17+
use PhpParser\Node\Stmt\Return_;
1418
use PhpParser\NodeVisitor;
1519
use PHPStan\Type\IntegerRangeType;
1620
use Rector\DeadCode\ConditionResolver;
@@ -80,14 +84,22 @@ public function run()
8084
*/
8185
public function getNodeTypes(): array
8286
{
83-
return [Ternary::class, FuncCall::class];
87+
return [If_::class, Ternary::class, FuncCall::class];
8488
}
8589

8690
/**
87-
* @param Ternary|FuncCall $node
91+
* @param If_|Ternary|FuncCall $node
8892
*/
8993
public function refactor(Node $node): null|int|Node
9094
{
95+
if ($node instanceof If_) {
96+
if ($this->isVersionCompareIf($node)) {
97+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
98+
}
99+
100+
return null;
101+
}
102+
91103
if ($node instanceof Ternary) {
92104
if ($this->isVersionCompareTernary($node)) {
93105
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
@@ -118,6 +130,36 @@ public function refactor(Node $node): null|int|Node
118130
return $node;
119131
}
120132

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+
121163
private function isVersionCompareTernary(Ternary $ternary): bool
122164
{
123165
if ($ternary->if instanceof Expr && $ternary->cond instanceof FuncCall) {

0 commit comments

Comments
 (0)