Skip to content

Commit 22de000

Browse files
ArshidArshid
authored andcommitted
[php 8.3] Add json_validate rule
1 parent 77c8a6e commit 22de000

3 files changed

Lines changed: 21 additions & 83 deletions

File tree

rules-tests/Php83/Rector/BooleanAnd/JsonValidateRector/Fixture/json_validate_base.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture;
44

5-
if (json_decode($json) !== null && json_last_error() === JSON_ERROR_NONE){
5+
if (json_decode($json, true, 512) !== null && json_last_error() === JSON_ERROR_NONE){
66
echo 1;
77
}
88
?>
@@ -11,7 +11,7 @@ if (json_decode($json) !== null && json_last_error() === JSON_ERROR_NONE){
1111

1212
namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture;
1313

14-
if (json_validate($json)){
14+
if (json_validate($json, true, 512)){
1515
echo 1;
1616
}
1717
?>

rules/Php83/Rector/BooleanAnd/JsonValidateRector.php

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@
1111
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1212
use PhpParser\Node\Expr\ConstFetch;
1313
use PhpParser\Node\Expr\FuncCall;
14-
use PhpParser\Node\Identifier;
1514
use PhpParser\Node\Name;
1615
use PHPStan\Analyser\Scope;
17-
use PHPStan\Reflection\Native\NativeFunctionReflection;
18-
use Rector\NodeAnalyzer\ArgsAnalyzer;
1916
use Rector\NodeManipulator\BinaryOpManipulator;
2017
use Rector\NodeTypeResolver\Node\AttributeKey;
21-
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
2218
use Rector\Php71\ValueObject\TwoNodeMatch;
2319
use Rector\PhpParser\Node\Value\ValueResolver;
2420
use Rector\Rector\AbstractRector;
25-
use Rector\Reflection\ReflectionResolver;
2621
use Rector\ValueObject\PhpVersionFeature;
2722
use Rector\ValueObject\PolyfillPackage;
2823
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -41,8 +36,6 @@ final class JsonValidateRector extends AbstractRector implements MinPhpVersionIn
4136

4237
public function __construct(
4338
private readonly BinaryOpManipulator $binaryOpManipulator,
44-
private readonly ReflectionResolver $reflectionResolver,
45-
private readonly ArgsAnalyzer $argsAnalyzer,
4639
private ValueResolver $valueResolver,
4740
) {
4841
}
@@ -102,15 +95,12 @@ public function refactor(Node $node): ?Node
10295
}
10396

10497
$args = $funcCall->getArgs();
105-
$positions = $this->argsAnalyzer->hasNamedArg($args)
106-
? $this->resolveNamedPositions($args)
107-
: $this->resolveOriginalPositions($funcCall, $scope);
10898

109-
if ($positions === []) {
99+
if ($args === []) {
110100
return null;
111101
}
112102

113-
if (! $this->validateArgs($args, $positions)) {
103+
if (! $this->validateArgs($funcCall)) {
114104
return null;
115105
}
116106
$funcCall->name = new Name('json_validate');
@@ -164,81 +154,25 @@ public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall
164154
return $funcCall;
165155
}
166156

167-
/**
168-
* @param Arg[] $args
169-
* @param int[]|string[] $positions
170-
*/
171-
protected function validateArgs(array $args, array $positions): bool
157+
protected function validateArgs(FuncCall $funcCall): bool
172158
{
173-
foreach ($positions as $position) {
174-
$arg = $args[$position] ?? '';
175-
if ($arg instanceof Arg && $arg->name instanceof Identifier && $arg->name->toString() === 'flags') {
176-
$flags = $this->valueResolver->getValue($arg);
177-
if ($flags !== JSON_INVALID_UTF8_IGNORE) {
178-
return false;
179-
}
180-
}
181-
if ($arg instanceof Arg && $arg->name instanceof Identifier && $arg->name->toString() === 'depth') {
182-
$depth = $this->valueResolver->getValue($arg);
183-
if ($depth <= 0) {
184-
return false;
185-
}
186-
if ($depth > self::JSON_MAX_DEPTH) {
187-
return false;
188-
}
189-
}
190-
}
191-
192-
return true;
193-
}
194-
195-
/**
196-
* @param Arg[] $args
197-
* @return int[]|string[]
198-
*/
199-
private function resolveNamedPositions(array $args): array
200-
{
201-
$positions = [];
202-
203-
foreach ($args as $position => $arg) {
204-
if (! $arg->name instanceof Identifier) {
205-
continue;
206-
}
159+
$depth = $funcCall->getArg('depth', 2);
160+
$flags = $funcCall->getArg('flags', 3);
207161

208-
if (! $this->isNames($arg->name, self::ARG_NAMES)) {
209-
continue;
162+
if ($flags instanceof Arg) {
163+
$flagsValue = $this->valueResolver->getValue($flags);
164+
if ($flagsValue !== JSON_INVALID_UTF8_IGNORE) {
165+
return false;
210166
}
211-
212-
$positions[] = $position;
213167
}
214168

215-
return $positions;
216-
}
217-
218-
/**
219-
* @return int[]|string[]
220-
*/
221-
private function resolveOriginalPositions(FuncCall $funcCall, Scope $scope): array
222-
{
223-
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($funcCall);
224-
if (! $functionReflection instanceof NativeFunctionReflection) {
225-
return [];
226-
}
227-
228-
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
229-
$functionReflection,
230-
$funcCall,
231-
$scope
232-
);
233-
234-
$positions = [];
235-
236-
foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
237-
if (in_array($parameterReflection->getName(), self::ARG_NAMES, true)) {
238-
$positions[] = $position;
169+
if ($depth instanceof Arg) {
170+
$depthValue = $this->valueResolver->getValue($depth);
171+
if ($depthValue <= 0 || $depthValue > self::JSON_MAX_DEPTH) {
172+
return false;
239173
}
240174
}
241175

242-
return $positions;
176+
return true;
243177
}
244178
}

tests/bootstrap.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
require_once __DIR__ . '/../vendor/autoload.php';
88

99
// silent deprecations, since we test them
10-
error_reporting(E_ALL ^ E_DEPRECATED);
10+
// error_reporting(E_ALL ^ E_DEPRECATED);
11+
error_reporting(E_ALL);
12+
set_error_handler(function (int $severity, string $message, string $file, int $line): bool {
13+
throw new \ErrorException($message, 0, $severity, $file, $line);
14+
});
1115

1216
// performance boost
1317
gc_disable();

0 commit comments

Comments
 (0)