Repository navigation
Expand file tree
/
Copy pathDowngradeHashAlgorithmXxHashRector.php
More file actions
208 lines (175 loc) · 5.64 KB
/
Copy pathDowngradeHashAlgorithmXxHashRector.php
File metadata and controls
208 lines (175 loc) · 5.64 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp81\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\If_;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\Value\ValueResolver;
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,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}
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 [Ternary::class, If_::class, FuncCall::class];
}
/**
* @param Ternary|If_|FuncCall $node
*/
public function refactor(Node $node): ?FuncCall
{
if ($node instanceof Ternary || $node instanceof If_) {
$this->markGuardedHashCalls($node);
return null;
}
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;
}
return ! $this->isName($funcCall, 'hash');
}
/**
* Mark hash() calls guarded by a PHP_VERSION_ID check as version conditioned,
* so they are skipped like version_compare() guarded calls.
*/
private function markGuardedHashCalls(Ternary|If_ $node): void
{
if (! $this->hasPhpVersionIdCond($node->cond)) {
return;
}
/** @var FuncCall[] $funcCalls */
$funcCalls = $this->betterNodeFinder->findInstancesOf($node, [FuncCall::class]);
foreach ($funcCalls as $funcCall) {
if (! $this->isName($funcCall, 'hash')) {
continue;
}
$funcCall->setAttribute(AttributeKey::PHP_VERSION_CONDITIONED, true);
}
}
private function hasPhpVersionIdCond(Expr $expr): bool
{
if (! $expr instanceof BinaryOp) {
return false;
}
return $this->isPhpVersionIdConstFetch($expr->left) || $this->isPhpVersionIdConstFetch($expr->right);
}
private function isPhpVersionIdConstFetch(Expr $expr): bool
{
return $expr instanceof ConstFetch && $this->isName($expr, 'PHP_VERSION_ID');
}
/**
* @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;
}
}