Skip to content
Merged

Rectify #7216

Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion rules/Php80/Rector/NotIdentical/StrContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use Rector\Php80\NodeResolver\StrFalseComparisonResolver;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
Expand Down
24 changes: 13 additions & 11 deletions rules/Php83/Rector/BooleanAnd/JsonValidateRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class JsonValidateRector extends AbstractRector implements MinPhpVersionIn

public function __construct(
private readonly BinaryOpManipulator $binaryOpManipulator,
private ValueResolver $valueResolver,
private readonly ValueResolver $valueResolver,
) {
}

Expand All @@ -57,7 +57,7 @@ public function getRuleDefinition(): RuleDefinition
,
<<<'CODE_SAMPLE'
if (json_validate($json)) {
}
}
CODE_SAMPLE
),
]
Expand Down Expand Up @@ -89,13 +89,14 @@ public function refactor(Node $node): ?Node

$args = $funcCall->getArgs();

if (count($args) < 1 ){
if (count($args) < 1) {
return null;
}

if (! $this->validateArgs($funcCall)) {
return null;
}

$funcCall->name = new Name('json_validate');
$funcCall->args = $args;

Expand All @@ -116,8 +117,8 @@ public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall

$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')
fn (Node $node): bool => $node instanceof FuncCall && $this->isName($node->name, 'json_decode'),
fn (Node $node): bool => $node instanceof ConstFetch && $this->isName($node->name, 'null')
);

if (! $decodeMatch instanceof TwoNodeMatch) {
Expand All @@ -131,23 +132,24 @@ public function matchJsonValidateArg(BooleanAnd $booleanAnd): ?FuncCall

$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')
fn (Node $node): bool => $node instanceof FuncCall && $this->isName($node->name, 'json_last_error'),
fn (Node $node): bool => $node instanceof ConstFetch && $this->isName($node->name, 'JSON_ERROR_NONE')
);

if (! $errorMatch instanceof TwoNodeMatch) {
return null;
}

// always return the json_decode(...) call
$funcCall = $decodeMatch->getFirstExpr();
if (! $funcCall instanceof FuncCall) {
$expr = $decodeMatch->getFirstExpr();
if (! $expr instanceof FuncCall) {
return null;
}
return $funcCall;

return $expr;
}

protected function validateArgs(FuncCall $funcCall): bool
private function validateArgs(FuncCall $funcCall): bool
{
$depth = $funcCall->getArg('depth', 2);
$flags = $funcCall->getArg('flags', 3);
Expand Down
2 changes: 1 addition & 1 deletion src/ValueObject/PolyfillPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class PolyfillPackage
* @var string
*/
public const PHP_83 = 'symfony/polyfill-php83';

/**
* @var string
*/
Expand Down
Loading