Skip to content

Commit

Permalink
[DowngradePhp73] Refactor DowngradePhp73JsonConstRector so downgrade-…
Browse files Browse the repository at this point in the history
…able (#134)
  • Loading branch information
samsonasik committed Jun 21, 2023
1 parent 9cfcb6a commit 2500281
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ private function refactorIf(If_ $if): ?If_
return $if;
}

private function resolveFuncCall(StmtExpression $stmtExpression): ?FuncCall
{
$expr = $stmtExpression->expr;
if ($expr instanceof Assign) {
if ($expr->expr instanceof FuncCall) {
return $expr->expr;
}

return null;
}

if ($expr instanceof FuncCall) {
return $expr;
}

return null;
}

/**
* Add an alternative throwing error behavior after any `json_encode`
* or `json_decode` function called with the `JSON_THROW_ON_ERROR` flag set.
Expand All @@ -137,16 +155,8 @@ private function refactorIf(If_ $if): ?If_
*/
private function refactorStmt(StmtExpression $stmtExpression): ?array
{
$nodes = [$stmtExpression];

// retrieve a `FuncCall`, if any, from the statement
$funcCall = match ($stmtExpression->expr::class) {
Assign::class => $stmtExpression->expr->expr instanceof FuncCall
? $stmtExpression->expr->expr
: null,
FuncCall::class => $stmtExpression->expr,
default => null,
};
$funcCall = $this->resolveFuncCall($stmtExpression);

// Nothing to do if no `FuncCall` found
if (! $funcCall instanceof FuncCall) {
Expand All @@ -163,6 +173,7 @@ private function refactorStmt(StmtExpression $stmtExpression): ?array
return null;
}

$nodes = [$stmtExpression];
$nodes[] = new If_(
new NotIdentical(
new FuncCall(new Name('json_last_error')),
Expand Down

0 comments on commit 2500281

Please sign in to comment.