-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeHashAlgorithmXxHashRector.php
More file actions
199 lines (166 loc) · 5.46 KB
/
DowngradeHashAlgorithmXxHashRector.php
File metadata and controls
199 lines (166 loc) · 5.46 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
<?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\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeVisitor;
use PHPStan\Type\IntegerRangeType;
use Rector\DeadCode\ConditionResolver;
use Rector\DeadCode\ValueObject\VersionCompareCondition;
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;
/**
* @see \Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\DowngradeHashAlgorithmXxHashRectorTest
*/
final class DowngradeHashAlgorithmXxHashRector extends AbstractRector
{
private const HASH_ALGORITHMS_TO_DOWNGRADE = [
'xxh32' => MHASH_XXH32,
'xxh64' => MHASH_XXH64,
'xxh3' => MHASH_XXH3,
'xxh128' => MHASH_XXH128,
];
private const REPLACEMENT_ALGORITHM = 'md5';
private int $argNamedKey;
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly ValueResolver $valueResolver,
private readonly ConditionResolver $conditionResolver
) {
}
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, FuncCall::class];
}
/**
* @param Ternary|FuncCall $node
*/
public function refactor(Node $node): null|int|Node
{
if ($node instanceof Ternary) {
if ($this->isVersionCompareTernary($node)) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return null;
}
if ($this->shouldSkip($node)) {
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 isVersionCompareTernary(Ternary $ternary): bool
{
if ($ternary->if instanceof Expr && $ternary->cond instanceof FuncCall) {
$versionCompare = $this->conditionResolver->resolveFromExpr($ternary->cond);
if ($versionCompare instanceof VersionCompareCondition && $versionCompare->getSecondVersion() === 80100) {
if ($versionCompare->getCompareSign() === '>=') {
return $ternary->if instanceof FuncCall && $this->isName($ternary->if, 'hash');
}
if ($versionCompare->getCompareSign() === '<') {
return $ternary->else instanceof FuncCall && $this->isName($ternary->else, 'hash');
}
}
}
return false;
}
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) {
// next todo: check version_compare() and if() usage
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(constant($constant), self::HASH_ALGORITHMS_TO_DOWNGRADE, true);
return $mappedConstant !== false ? $mappedConstant : $constant;
}
}