-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDowngradePhp73JsonConstRector.php
More file actions
240 lines (205 loc) · 7.19 KB
/
DowngradePhp73JsonConstRector.php
File metadata and controls
240 lines (205 loc) · 7.19 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp73\Rector\ConstFetch;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified as NameFullyQualified;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
use Rector\Enum\JsonConstant;
use Rector\NodeAnalyzer\DefineFuncCallAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://www.php.net/manual/en/function.json-encode.php#refsect1-function.json-encode-changelog
*
* @see \Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\DowngradePhp73JsonConstRectorTest
*/
final class DowngradePhp73JsonConstRector extends AbstractRector
{
/**
* @var string
*/
private const PHP73_JSON_CONSTANT_IS_KNOWN = 'php73_json_constant_is_known';
/**
* @var array<string>
*/
private const REFACTOR_FUNCS = ['json_decode', 'json_encode'];
public function __construct(
private readonly JsonConstCleaner $jsonConstCleaner,
private readonly DefineFuncCallAnalyzer $defineFuncCallAnalyzer,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove Json constant that available only in php 7.3',
[
new CodeSample(
<<<'CODE_SAMPLE'
$json = json_encode($content, JSON_THROW_ON_ERROR);
$content = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$json json_encode($content, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
$content = json_decode($json, null, 512, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ConstFetch::class, BitwiseOr::class, If_::class, Expression::class];
}
/**
* @param ConstFetch|BitwiseOr|If_|Expression $node
* @return null|Expr|array<Expression|If_>
*/
public function refactor(Node $node): null|Expr|array
{
if ($node instanceof If_) {
$this->markConstantKnownInInnerStmts($node);
return null;
}
// skip as known
if ((bool) $node->getAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN)) {
return null;
}
if ($node instanceof Expression) {
return $this->refactorExpression($node);
}
return $this->jsonConstCleaner->clean($node, [JsonConstant::THROW_ON_ERROR]);
}
private function markConstantKnownInInnerStmts(If_ $if): void
{
if (! $this->defineFuncCallAnalyzer->isDefinedWithConstants($if->cond, [JsonConstant::THROW_ON_ERROR])) {
return;
}
$this->traverseNodesWithCallable($if, static function (Node $node): null {
$node->setAttribute(self::PHP73_JSON_CONSTANT_IS_KNOWN, true);
return null;
});
}
private function resolveFuncCall(Expression $Expression): ?FuncCall
{
$expr = $Expression->expr;
if ($expr instanceof Assign) {
if ($expr->expr instanceof FuncCall) {
return $expr->expr;
}
return null;
}
if ($expr instanceof FuncCall) {
return $expr;
}
return null;
}
/**
* Add an alternative throwing error behavior after any `json_encode`
* or `json_decode` function called with the `JSON_THROW_ON_ERROR` flag set.
* This is a partial improvement of removing the `JSON_THROW_ON_ERROR` flag,
* only when the flags are directly set in the function call.
* If the flags are set from a variable, that would require a much more
* complex analysis to be 100% accurate, beyond Rector actual capabilities.
*
* @return null|array<Expression|If_>
*/
private function refactorExpression(Expression $expression): ?array
{
if ($expression->getAttribute(AttributeKey::IS_IN_TRY_BLOCK) === true) {
return null;
}
// retrieve a `FuncCall`, if any, from the statement
$funcCall = $this->resolveFuncCall($expression);
// Nothing to do if no `FuncCall` found
if (! $funcCall instanceof FuncCall) {
return null;
}
// Nothing to do if not a refactored function
if (! in_array($this->getName($funcCall), self::REFACTOR_FUNCS, true)) {
return null;
}
// Nothing to do if the flag `JSON_THROW_ON_ERROR` is not set in args
if (! $this->hasConstFetchInArgs($funcCall->args, 'JSON_THROW_ON_ERROR')) {
return null;
}
$nodes = [$expression];
$nodes[] = new If_(
new NotIdentical(
new FuncCall(new Name('json_last_error')),
new ConstFetch(new Name('JSON_ERROR_NONE'))
),
[
'stmts' => [
new Expression(new Throw_(
new New_(
new NameFullyQualified('Exception'),
[new Arg(new FuncCall(new Name('json_last_error_msg')))]
)
)),
],
]
);
return $nodes;
}
/**
* Search if a given constant is set within a list of `Arg`
* @param array<Arg|VariadicPlaceholder> $args
*/
private function hasConstFetchInArgs(array $args, string $constName): bool
{
foreach ($args as $arg) {
// Only `Arg` instances are handled.
if (! $arg instanceof Arg) {
return false;
}
$value = $arg->value;
if ($value instanceof ConstFetch && $this->getName($value) === $constName) {
return true;
}
if ($value instanceof BitwiseOr) {
return $this->hasConstFetchInBitwiseOr($value, $constName);
}
}
return false;
}
/**
* Search if a given constant is set within a `BitwiseOr`
*/
private function hasConstFetchInBitwiseOr(BitwiseOr $bitwiseOr, string $constantName): bool
{
foreach ([$bitwiseOr->left, $bitwiseOr->right] as $subNode) {
if ($subNode instanceof BitwiseOr && $this->hasConstFetchInBitwiseOr($subNode, $constantName)) {
return true;
}
if ($subNode instanceof ConstFetch && $this->isName($subNode, $constantName)) {
return true;
}
}
return false;
}
}