forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonValidateRector.php
More file actions
120 lines (98 loc) · 3.37 KB
/
JsonValidateRector.php
File metadata and controls
120 lines (98 loc) · 3.37 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
<?php
declare(strict_types=1);
namespace Rector\Php83\Rector\BooleanAnd;
use PhpParser\Node;
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\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
{
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;
}
if (isset($funcCall->getArgs()[1])) {
unset($funcCall->args[1]);
}
$funcCall->name = new Name('json_validate');
return $funcCall;
}
public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_83;
}
public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall
{
if ($booleanAnd->left instanceof NotIdentical) {
$notIdentical = $booleanAnd->left;
if ($notIdentical->left instanceof FuncCall
&& $this->isName($notIdentical->left->name, 'json_decode')
&& $notIdentical->right instanceof ConstFetch
&& $this->isName($notIdentical->right->name, 'null')) {
// right side: json_last_error() === JSON_ERROR_NONE
if (! $booleanAnd->right instanceof Identical) {
return null;
}
$identical = $booleanAnd->right;
if ($identical->left instanceof FuncCall
&& $this->isName($identical->left->name, 'json_last_error')
&& $identical->right instanceof ConstFetch
&& $this->isName($identical->right->name, 'JSON_ERROR_NONE')) {
return $notIdentical->left; // return json_decode(...) call
}
}
}
return null;
}
}