-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDowngradeHashAlgorithmXxHashRector.php
More file actions
176 lines (147 loc) · 4.48 KB
/
Copy pathDowngradeHashAlgorithmXxHashRector.php
File metadata and controls
176 lines (147 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp81\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
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\NodeTypeResolver\Node\AttributeKey;
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;
/**
* @see \Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\DowngradeHashAlgorithmXxHashRectorTest
*/
final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
{
/**
* Constants are referenced by name, as the MHASH_* constants are deprecated since PHP 8.5
*
* @var array<string, string>
*/
private const array HASH_ALGORITHMS_TO_DOWNGRADE = [
'xxh32' => 'MHASH_XXH32',
'xxh64' => 'MHASH_XXH64',
'xxh3' => 'MHASH_XXH3',
'xxh128' => 'MHASH_XXH128',
];
private const string REPLACEMENT_ALGORITHM = 'md5';
private int $argNamedKey;
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly ValueResolver $valueResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Downgrade hash algorithm xxh32, xxh64, xxh3 or xxh128 by default to md5. You can configure the algorithm to downgrade.',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return hash('xxh128', 'some-data-to-hash');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return hash('md5', 'some-data-to-hash');
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?FuncCall
{
if ($this->shouldSkip($node)) {
return null;
}
if ($node->getAttribute(AttributeKey::PHP_VERSION_CONDITIONED)) {
return null;
}
$this->argNamedKey = 0;
$algorithm = $this->getHashAlgorithm($node->getArgs());
if ($algorithm === null || ! array_key_exists($algorithm, self::HASH_ALGORITHMS_TO_DOWNGRADE)) {
return null;
}
$args = $node->getArgs();
if (! isset($args[$this->argNamedKey])) {
return null;
}
$arg = $args[$this->argNamedKey];
$arg->value = new String_(self::REPLACEMENT_ALGORITHM);
return $node;
}
private function shouldSkip(FuncCall $funcCall): bool
{
if ($funcCall->isFirstClassCallable()) {
return true;
}
if (! $this->isName($funcCall, 'hash')) {
return true;
}
$scope = ScopeFetcher::fetch($funcCall);
$type = $scope->getPhpVersion()
->getType();
if (! $type instanceof IntegerRangeType) {
return false;
}
return $type->getMin() === 80100;
}
/**
* @param Arg[] $args
*/
private function getHashAlgorithm(array $args): ?string
{
$arg = null;
if ($this->argsAnalyzer->hasNamedArg($args)) {
foreach ($args as $key => $arg) {
if ($arg->name?->name !== 'algo') {
continue;
}
$this->argNamedKey = $key;
break;
}
} else {
$arg = $args[$this->argNamedKey];
}
$algorithmNode = $arg?->value;
return match (true) {
$algorithmNode instanceof String_ => $this->valueResolver->getValue($algorithmNode),
$algorithmNode instanceof ConstFetch => $this->mapConstantToString(
$this->valueResolver->getValue($algorithmNode)
),
default => null,
};
}
private function mapConstantToString(string $constant): string
{
$mappedConstant = array_search(ltrim($constant, '\\'), self::HASH_ALGORITHMS_TO_DOWNGRADE, true);
return $mappedConstant !== false ? $mappedConstant : $constant;
}
}