-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathJsonValidateRector.php
More file actions
182 lines (151 loc) · 5.34 KB
/
JsonValidateRector.php
File metadata and controls
182 lines (151 loc) · 5.34 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
<?php
declare(strict_types=1);
namespace Rector\Php83\Rector\BooleanAnd;
use PhpParser\Node\Identifier;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\NodeManipulator\BinaryOpManipulator;
use Rector\Php71\ValueObject\TwoNodeMatch;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\JsonValidateRectorTest
*/
final class JsonValidateRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
private const int JSON_MAX_DEPTH = 0x7FFFFFFF;
public function __construct(
private readonly BinaryOpManipulator $binaryOpManipulator,
private readonly ValueResolver $valueResolver,
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::JSON_VALIDATE;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE with json_validate()',
[
new CodeSample(
<<<'CODE_SAMPLE'
if (json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE) {
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if (json_validate($json)) {
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BooleanAnd::class];
}
/**
* @param BooleanAnd $node
*/
public function refactor(Node $node): ?Node
{
$funcCall = $this->matchJsonValidateArg($node);
if (! $funcCall instanceof FuncCall) {
return null;
}
if ($funcCall->isFirstClassCallable()) {
return null;
}
$args = $funcCall->getArgs();
if (count($args) < 1) {
return null;
}
if (! $this->validateArgs($funcCall)) {
return null;
}
// Remove associative argument (position 1 or named) - json_validate does not have this param
foreach ($args as $index => $arg) {
if ($arg instanceof Arg && (
($arg->name instanceof Identifier && $arg->name->toString() === 'associative') ||
(!$arg->name instanceof Identifier && $index === 1)
)) {
unset($funcCall->args[$index]);
break;
}
}
$funcCall->name = new Name('json_validate');
return $funcCall;
}
public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_83;
}
public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall
{
// match: json_decode(...) !== null OR null !== json_decode(...)
if (! ($booleanAnd->left instanceof NotIdentical)) {
return null;
}
$decodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
$booleanAnd->left,
fn (Node $node): bool => $node instanceof FuncCall && $this->isName($node->name, 'json_decode'),
fn (Node $node): bool => $node instanceof ConstFetch && $this->isName($node->name, 'null')
);
if (! $decodeMatch instanceof TwoNodeMatch) {
return null;
}
// match: json_last_error() === JSON_ERROR_NONE OR JSON_ERROR_NONE === json_last_error()
if (! ($booleanAnd->right instanceof Identical)) {
return null;
}
$errorMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
$booleanAnd->right,
fn (Node $node): bool => $node instanceof FuncCall && $this->isName($node->name, 'json_last_error'),
fn (Node $node): bool => $node instanceof ConstFetch && $this->isName($node->name, 'JSON_ERROR_NONE')
);
if (! $errorMatch instanceof TwoNodeMatch) {
return null;
}
// always return the json_decode(...) call
$expr = $decodeMatch->getFirstExpr();
if (! $expr instanceof FuncCall) {
return null;
}
return $expr;
}
private function validateArgs(FuncCall $funcCall): bool
{
$depth = $funcCall->getArg('depth', 2);
$flags = $funcCall->getArg('flags', 3);
if ($flags instanceof Arg) {
$flagsValue = $this->valueResolver->getValue($flags);
if ($flagsValue !== JSON_INVALID_UTF8_IGNORE) {
return false;
}
}
if ($depth instanceof Arg) {
$depthValue = $this->valueResolver->getValue($depth);
if ($depthValue <= 0 || $depthValue > self::JSON_MAX_DEPTH) {
return false;
}
}
return true;
}
}