Skip to content
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f99fbbd
[php 8.3] Add json_validate rule
Aug 30, 2025
bfd6894
[php 8.3] Add json_validate rule
Aug 30, 2025
ebb70d1
[php 8.3] Add json_validate rule
Aug 30, 2025
634fb9d
[php 8.3] Add json_validate rule
Aug 30, 2025
c640e56
[php 8.3] Add json_validate rule
Aug 30, 2025
d73f7bb
[php 8.3] Add json_validate rule
Aug 30, 2025
57ff8c0
[php 8.3] Add json_validate rule
Aug 30, 2025
4d2be54
[php 8.3] Add json_validate rule
Aug 30, 2025
5763420
[php 8.3] Add json_validate rule
Aug 30, 2025
70794c6
[php 8.3] Add json_validate rule
Aug 30, 2025
e62a32c
[php 8.3] Add json_validate rule
Aug 30, 2025
996ff1a
[php 8.3] Add json_validate rule
Aug 30, 2025
ebc2672
[php 8.3] Add json_validate rule
Aug 30, 2025
cd85f76
[php 8.3] Add json_validate rule
Aug 30, 2025
6c975d8
[php 8.3] Add json_validate rule
Aug 30, 2025
e23d2c7
[php 8.3] Add json_validate rule
Aug 30, 2025
3377025
[php 8.3] Add json_validate rule
Aug 30, 2025
be1813a
[php 8.3] Add json_validate rule
Aug 30, 2025
cfceaab
[php 8.3] Add json_validate rule
Aug 30, 2025
73f0f22
[php 8.3] Add json_validate rule
Aug 30, 2025
097388d
[php 8.3] Add json_validate rule
Aug 30, 2025
4a94748
[php 8.3] Add json_validate rule
Aug 30, 2025
3f78b41
[php 8.3] Add json_validate rule
Aug 30, 2025
70d193c
[php 8.3] Add json_validate rule
Aug 30, 2025
162a30e
[php 8.3] Add json_validate rule
Sep 1, 2025
337c8c2
[php 8.3] Add json_validate rule
Sep 1, 2025
e241278
[php 8.3] Add json_validate rule
Sep 1, 2025
8a47321
[php 8.3] Add json_validate rule
Sep 1, 2025
e9b2512
[php 8.3] Add json_validate rule
Sep 1, 2025
a26229b
[php 8.3] Add json_validate rule
Sep 1, 2025
0bef253
[php 8.3] Add json_validate rule
Sep 1, 2025
3b8c9b2
[8.4][DeprecatedAnnotationToDeprecatedAttributeRector] Remove invalid…
DanielEScherzer Aug 30, 2025
e8d6723
[automated] Apply Coding Standard (#7203)
TomasVotruba Aug 31, 2025
bf3716b
[Php85] Add ChrArgModuloRector (#7190)
arshidkv12 Aug 31, 2025
0424066
Remove removed "Rector\Symfony\Set\FOSRestSetList/JMSSetList/Sensiol…
samsonasik Sep 1, 2025
5dcdd8f
[php 8.3] Add json_validate rule
Sep 1, 2025
1259088
[php 8.3] Add json_validate rule
Sep 1, 2025
e4412d6
Merge branch 'main' into json_validate
arshidkv12 Sep 1, 2025
2ef3815
[php 8.3] Add json_validate rule
Sep 2, 2025
c5a036e
[php 8.3] Add json_validate rule
Sep 2, 2025
216124c
[php 8.3] Add json_validate rule
Sep 2, 2025
04e2ac3
[php 8.3] Add json_validate rule
Sep 3, 2025
bfc6cc1
[php 8.3] Add json_validate rule
Sep 3, 2025
f9ad159
[php 8.3] Add json_validate rule
Sep 3, 2025
77c8a6e
[php 8.3] Add json_validate rule
Sep 3, 2025
22de000
[php 8.3] Add json_validate rule
Sep 3, 2025
d55c5c4
[php 8.3] Add json_validate rule
Sep 3, 2025
6df1782
Merge branch 'rectorphp:main' into json_validate
arshidkv12 Sep 3, 2025
5340f45
[php 8.3] Add json_validate rule
Sep 3, 2025
a6f77c0
[php 8.3] Add json_validate rule
Sep 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\BooleanAnd\JsonValidateRector;
use Rector\Php83\Rector\Class_\ReadOnlyAnonymousClassRector;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
Expand All @@ -18,5 +19,6 @@
RemoveGetClassGetParentClassNoArgsRector::class,
ReadOnlyAnonymousClassRector::class,
DynamicClassConstFetchRector::class,
JsonValidateRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
Copy link
Copy Markdown
Member

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:

-Json_validate.php.inc
+json_validate.php.inc

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed :)

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fixture content seems equal with other fixture, seems copy paste content.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);
Comment thread
samsonasik marked this conversation as resolved.
Outdated

$rectorConfig->rule(JsonValidateRector::class);
};
120 changes: 120 additions & 0 deletions rules/Php83/Rector/BooleanAnd/JsonValidateRector.php
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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_decode() allow pass depth and flag argument, and from index 1 to 3, they are optional so isset on function arguments per index target is needed.

}

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;
Comment thread
samsonasik marked this conversation as resolved.
Outdated
Comment thread
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;
}
}
11 changes: 11 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ final class PhpVersionFeature
*/
public const STR_CONTAINS = PhpVersion::PHP_80;

/**
* @var int
*/
public const JSON_VALIDATE = PhpVersion::PHP_80;
Comment thread
samsonasik marked this conversation as resolved.
Outdated

/**
* @var int
*/
Expand Down Expand Up @@ -798,4 +803,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION = PhpVersion::PHP_85;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_string_which_are_not_one_byte_long_to_ord
* @var int
*/
public const DEPRECATE_ORD_WITH_MULTIBYTE_STRING = PhpVersion::PHP_85;
Comment thread
samsonasik marked this conversation as resolved.
Outdated
}
Loading