|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\Php83\Rector\BooleanAnd; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\NotIdentical; |
| 11 | +use PhpParser\Node\Expr\ConstFetch; |
| 12 | +use PhpParser\Node\Expr\FuncCall; |
| 13 | +use PhpParser\Node\Name; |
| 14 | +use Rector\NodeManipulator\BinaryOpManipulator; |
| 15 | +use Rector\Php71\ValueObject\TwoNodeMatch; |
| 16 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Rector\ValueObject\PhpVersionFeature; |
| 19 | +use Rector\ValueObject\PolyfillPackage; |
| 20 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 21 | +use Rector\VersionBonding\Contract\RelatedPolyfillInterface; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | +/** |
| 25 | + * @see \Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\JsonValidateRectorTest |
| 26 | + */ |
| 27 | +final class JsonValidateRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @readonly |
| 31 | + */ |
| 32 | + private BinaryOpManipulator $binaryOpManipulator; |
| 33 | + private ValueResolver $valueResolver; |
| 34 | + protected const ARG_NAMES = ['json', 'associative', 'depth', 'flags']; |
| 35 | + private const JSON_MAX_DEPTH = 0x7fffffff; |
| 36 | + public function __construct(BinaryOpManipulator $binaryOpManipulator, ValueResolver $valueResolver) |
| 37 | + { |
| 38 | + $this->binaryOpManipulator = $binaryOpManipulator; |
| 39 | + $this->valueResolver = $valueResolver; |
| 40 | + } |
| 41 | + public function provideMinPhpVersion() : int |
| 42 | + { |
| 43 | + return PhpVersionFeature::JSON_VALIDATE; |
| 44 | + } |
| 45 | + public function getRuleDefinition() : RuleDefinition |
| 46 | + { |
| 47 | + return new RuleDefinition('Replace json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE with json_validate()', [new CodeSample(<<<'CODE_SAMPLE' |
| 48 | +if (json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE) { |
| 49 | +} |
| 50 | + |
| 51 | +CODE_SAMPLE |
| 52 | +, <<<'CODE_SAMPLE' |
| 53 | +if (json_validate($json)) { |
| 54 | +} |
| 55 | +CODE_SAMPLE |
| 56 | +)]); |
| 57 | + } |
| 58 | + /** |
| 59 | + * @return array<class-string<Node>> |
| 60 | + */ |
| 61 | + public function getNodeTypes() : array |
| 62 | + { |
| 63 | + return [BooleanAnd::class]; |
| 64 | + } |
| 65 | + /** |
| 66 | + * @param BooleanAnd $node |
| 67 | + */ |
| 68 | + public function refactor(Node $node) : ?Node |
| 69 | + { |
| 70 | + $funcCall = $this->matchJsonValidateArg($node); |
| 71 | + if (!$funcCall instanceof FuncCall) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + if ($funcCall->isFirstClassCallable()) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + $args = $funcCall->getArgs(); |
| 78 | + if (\count($args) < 1) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + if (!$this->validateArgs($funcCall)) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + $funcCall->name = new Name('json_validate'); |
| 85 | + $funcCall->args = $args; |
| 86 | + return $funcCall; |
| 87 | + } |
| 88 | + public function providePolyfillPackage() : string |
| 89 | + { |
| 90 | + return PolyfillPackage::PHP_83; |
| 91 | + } |
| 92 | + public function matchJsonValidateArg(BooleanAnd $booleanAnd) : ?FuncCall |
| 93 | + { |
| 94 | + // match: json_decode(...) !== null OR null !== json_decode(...) |
| 95 | + if (!$booleanAnd->left instanceof NotIdentical) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + $decodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($booleanAnd->left, fn($node) => $node instanceof FuncCall && $this->isName($node->name, 'json_decode'), fn($node) => $node instanceof ConstFetch && $this->isName($node->name, 'null')); |
| 99 | + if (!$decodeMatch instanceof TwoNodeMatch) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + // match: json_last_error() === JSON_ERROR_NONE OR JSON_ERROR_NONE === json_last_error() |
| 103 | + if (!$booleanAnd->right instanceof Identical) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + $errorMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($booleanAnd->right, fn($node) => $node instanceof FuncCall && $this->isName($node->name, 'json_last_error'), fn($node) => $node instanceof ConstFetch && $this->isName($node->name, 'JSON_ERROR_NONE')); |
| 107 | + if (!$errorMatch instanceof TwoNodeMatch) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + // always return the json_decode(...) call |
| 111 | + $funcCall = $decodeMatch->getFirstExpr(); |
| 112 | + if (!$funcCall instanceof FuncCall) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + return $funcCall; |
| 116 | + } |
| 117 | + protected function validateArgs(FuncCall $funcCall) : bool |
| 118 | + { |
| 119 | + $depth = $funcCall->getArg('depth', 2); |
| 120 | + $flags = $funcCall->getArg('flags', 3); |
| 121 | + if ($flags instanceof Arg) { |
| 122 | + $flagsValue = $this->valueResolver->getValue($flags); |
| 123 | + if ($flagsValue !== \JSON_INVALID_UTF8_IGNORE) { |
| 124 | + return \false; |
| 125 | + } |
| 126 | + } |
| 127 | + if ($depth instanceof Arg) { |
| 128 | + $depthValue = $this->valueResolver->getValue($depth); |
| 129 | + if ($depthValue <= 0 || $depthValue > self::JSON_MAX_DEPTH) { |
| 130 | + return \false; |
| 131 | + } |
| 132 | + } |
| 133 | + return \true; |
| 134 | + } |
| 135 | +} |
0 commit comments