-
-
Notifications
You must be signed in to change notification settings - Fork 439
[php 8.3] Add json_validate rule #7200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
f99fbbd
bfd6894
ebb70d1
634fb9d
c640e56
d73f7bb
57ff8c0
4d2be54
5763420
70794c6
e62a32c
996ff1a
ebc2672
cd85f76
6c975d8
e23d2c7
3377025
be1813a
cfceaab
73f0f22
097388d
4a94748
3f78b41
70d193c
162a30e
337c8c2
e241278
8a47321
e9b2512
a26229b
0bef253
3b8c9b2
e8d6723
bf3716b
0424066
5dcdd8f
1259088
e4412d6
2ef3815
c5a036e
216124c
04e2ac3
bfc6cc1
f9ad159
77c8a6e
22de000
d55c5c4
6df1782
5340f45
a6f77c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE){ | ||
| echo 1; | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_validate($json)){ | ||
| echo 1; | ||
| } | ||
| ?> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fixture content seems equal with other fixture, seems copy paste content.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it. |
||
| echo 1; | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_validate($json)){ | ||
| echo 1; | ||
| } | ||
| ?> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if ($flag) { | ||
| echo "skip"; | ||
| } elseif (json_decode($config, true) !== null && json_last_error() === JSON_ERROR_NONE) { | ||
| echo "valid config"; | ||
| } | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if ($flag) { | ||
| echo "skip"; | ||
| } elseif (json_validate($config)) { | ||
| echo "valid config"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_decode('{"a":1}', true) !== null && json_last_error() === JSON_ERROR_NONE) { | ||
| echo "inline"; | ||
| } | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector\Fixture; | ||
|
|
||
| if (json_validate('{"a":1}')) { | ||
| echo "inline"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\BooleanAnd\JsonValidateRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class JsonValidateRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Php83\Rector\BooleanAnd\JsonValidateRector; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->phpVersion(PhpVersion::PHP_80); | ||
|
samsonasik marked this conversation as resolved.
Outdated
|
||
|
|
||
| $rectorConfig->rule(JsonValidateRector::class); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| 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_80; | ||
|
samsonasik marked this conversation as resolved.
Outdated
samsonasik marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use lower character on fixture if possible for fixture filnames:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems still use upper case fixfture file name, please use lower case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github diff seems still show old name, is this file not removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed