Skip to content

Commit bd7ce51

Browse files
authored
[DowngradePhp81] Reference MHASH_* constants by name in DowngradeHashAlgorithmXxHashRector to fix PHP 8.5 deprecation (#386)
1 parent f154b8b commit bd7ce51

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;
4+
5+
final class FixtureFullyQualifiedConstant
6+
{
7+
public function run()
8+
{
9+
return hash(\MHASH_XXH128, 'some-data-to-hash');
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;
18+
19+
final class FixtureFullyQualifiedConstant
20+
{
21+
public function run()
22+
{
23+
return hash('md5', 'some-data-to-hash');
24+
}
25+
}
26+
27+
?>

rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
2525
{
2626
/**
27-
* @var array<string, int>
27+
* Constants are referenced by name, as the MHASH_* constants are deprecated since PHP 8.5
28+
*
29+
* @var array<string, string>
2830
*/
2931
private const array HASH_ALGORITHMS_TO_DOWNGRADE = [
30-
'xxh32' => MHASH_XXH32,
31-
'xxh64' => MHASH_XXH64,
32-
'xxh3' => MHASH_XXH3,
33-
'xxh128' => MHASH_XXH128,
32+
'xxh32' => 'MHASH_XXH32',
33+
'xxh64' => 'MHASH_XXH64',
34+
'xxh3' => 'MHASH_XXH3',
35+
'xxh128' => 'MHASH_XXH128',
3436
];
3537

3638
private const string REPLACEMENT_ALGORITHM = 'md5';
@@ -167,7 +169,7 @@ private function getHashAlgorithm(array $args): ?string
167169

168170
private function mapConstantToString(string $constant): string
169171
{
170-
$mappedConstant = array_search(constant($constant), self::HASH_ALGORITHMS_TO_DOWNGRADE, true);
172+
$mappedConstant = array_search(ltrim($constant, '\\'), self::HASH_ALGORITHMS_TO_DOWNGRADE, true);
171173

172174
return $mappedConstant !== false ? $mappedConstant : $constant;
173175
}

0 commit comments

Comments
 (0)