Skip to content

Commit a6f77c0

Browse files
ArshidArshid
authored andcommitted
[php 8.3] Add json_validate rule
1 parent 5340f45 commit a6f77c0

1 file changed

Lines changed: 14 additions & 87 deletions

File tree

rules/Php83/Rector/BooleanAnd/JsonValidateRector.php

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@
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;
16-
use PHPStan\Analyser\Scope;
17-
use PHPStan\Reflection\Native\NativeFunctionReflection;
18-
use Rector\NodeAnalyzer\ArgsAnalyzer;
1915
use Rector\NodeManipulator\BinaryOpManipulator;
20-
use Rector\NodeTypeResolver\Node\AttributeKey;
21-
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
2216
use Rector\Php71\ValueObject\TwoNodeMatch;
2317
use Rector\PhpParser\Node\Value\ValueResolver;
2418
use Rector\Rector\AbstractRector;
25-
use Rector\Reflection\ReflectionResolver;
2619
use Rector\ValueObject\PhpVersionFeature;
2720
use Rector\ValueObject\PolyfillPackage;
2821
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -41,8 +34,6 @@ final class JsonValidateRector extends AbstractRector implements MinPhpVersionIn
4134

4235
public function __construct(
4336
private readonly BinaryOpManipulator $binaryOpManipulator,
44-
private readonly ReflectionResolver $reflectionResolver,
45-
private readonly ArgsAnalyzer $argsAnalyzer,
4637
private ValueResolver $valueResolver,
4738
) {
4839
}
@@ -96,21 +87,13 @@ public function refactor(Node $node): ?Node
9687
return null;
9788
}
9889

99-
$scope = $node->getAttribute(AttributeKey::SCOPE);
100-
if (! $scope instanceof Scope) {
101-
return null;
102-
}
103-
10490
$args = $funcCall->getArgs();
105-
$positions = $this->argsAnalyzer->hasNamedArg($args)
106-
? $this->resolveNamedPositions($args)
107-
: $this->resolveOriginalPositions($funcCall, $scope);
10891

109-
if ($positions === []) {
92+
if (count($args) < 1 ){
11093
return null;
11194
}
11295

113-
if (! $this->validateArgs($args, $positions)) {
96+
if (! $this->validateArgs($funcCall)) {
11497
return null;
11598
}
11699
$funcCall->name = new Name('json_validate');
@@ -164,81 +147,25 @@ public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall
164147
return $funcCall;
165148
}
166149

167-
/**
168-
* @param Arg[] $args
169-
* @param int[]|string[] $positions
170-
*/
171-
protected function validateArgs(array $args, array $positions): bool
172-
{
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
150+
protected function validateArgs(FuncCall $funcCall): bool
200151
{
201-
$positions = [];
152+
$depth = $funcCall->getArg('depth', 2);
153+
$flags = $funcCall->getArg('flags', 3);
202154

203-
foreach ($args as $position => $arg) {
204-
if (! $arg->name instanceof Identifier) {
205-
continue;
155+
if ($flags instanceof Arg) {
156+
$flagsValue = $this->valueResolver->getValue($flags);
157+
if ($flagsValue !== JSON_INVALID_UTF8_IGNORE) {
158+
return false;
206159
}
207-
208-
if (! $this->isNames($arg->name, self::ARG_NAMES)) {
209-
continue;
210-
}
211-
212-
$positions[] = $position;
213-
}
214-
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 [];
226160
}
227161

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;
162+
if ($depth instanceof Arg) {
163+
$depthValue = $this->valueResolver->getValue($depth);
164+
if ($depthValue <= 0 || $depthValue > self::JSON_MAX_DEPTH) {
165+
return false;
239166
}
240167
}
241168

242-
return $positions;
169+
return true;
243170
}
244171
}

0 commit comments

Comments
 (0)