From 85656ae6764161ed8180e020b873605c5e8ab981 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 25 Aug 2025 16:51:40 +0700 Subject: [PATCH] [DowngradePhp81] Skip check php version with ternary on DowngradeHashAlgorithmXxHashRector --- .../skip_check_phpversion_ternary.php.inc | 13 +++++++++++++ .../DowngradeHashAlgorithmXxHashRector.php | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHash/Fixture/skip_check_phpversion_ternary.php.inc diff --git a/rules-tests/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHash/Fixture/skip_check_phpversion_ternary.php.inc b/rules-tests/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHash/Fixture/skip_check_phpversion_ternary.php.inc new file mode 100644 index 00000000..0d008661 --- /dev/null +++ b/rules-tests/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHash/Fixture/skip_check_phpversion_ternary.php.inc @@ -0,0 +1,13 @@ += 80100 + ? hash( 'xxh128', $value ) + : hash( 'md4', $value ); + } +} diff --git a/rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php b/rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php index ed1d9371..91953016 100644 --- a/rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php +++ b/rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php @@ -9,8 +9,10 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; +use PHPStan\Type\IntegerRangeType; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -108,7 +110,20 @@ private function shouldSkip(FuncCall $funcCall): bool return true; } - return ! $this->isName($funcCall, 'hash'); + if (! $this->isName($funcCall, 'hash')) { + return true; + } + + $scope = ScopeFetcher::fetch($funcCall); + $type = $scope->getPhpVersion() + ->getType(); + + if (! $type instanceof IntegerRangeType) { + // next todo: check version_compare() and if() usage + return false; + } + + return $type->getMin() === 80100; } /**