Skip to content

Commit c46da7c

Browse files
authored
[DowngradePhp81] Skip check php version with ternary on DowngradeHashAlgorithmXxHashRector (#307)
1 parent 7741746 commit c46da7c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;
4+
5+
final class SkipCheckPHPVersionTernary
6+
{
7+
public function run($value)
8+
{
9+
return PHP_VERSION_ID >= 80100
10+
? hash( 'xxh128', $value )
11+
: hash( 'md4', $value );
12+
}
13+
}

rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use PhpParser\Node\Expr\ConstFetch;
1010
use PhpParser\Node\Expr\FuncCall;
1111
use PhpParser\Node\Scalar\String_;
12+
use PHPStan\Type\IntegerRangeType;
1213
use Rector\NodeAnalyzer\ArgsAnalyzer;
1314
use Rector\PhpParser\Node\Value\ValueResolver;
15+
use Rector\PHPStan\ScopeFetcher;
1416
use Rector\Rector\AbstractRector;
1517
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1618
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -108,7 +110,20 @@ private function shouldSkip(FuncCall $funcCall): bool
108110
return true;
109111
}
110112

111-
return ! $this->isName($funcCall, 'hash');
113+
if (! $this->isName($funcCall, 'hash')) {
114+
return true;
115+
}
116+
117+
$scope = ScopeFetcher::fetch($funcCall);
118+
$type = $scope->getPhpVersion()
119+
->getType();
120+
121+
if (! $type instanceof IntegerRangeType) {
122+
// next todo: check version_compare() and if() usage
123+
return false;
124+
}
125+
126+
return $type->getMin() === 80100;
112127
}
113128

114129
/**

0 commit comments

Comments
 (0)