Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function jsonThrowOnError()
json_decode($json, true, 215);

json_decode($json, true, 122, JSON_THROW_ON_ERROR);

json_decode($json, true, 122, JSON_UNESCAPED_UNICODE);
}

?>
Expand All @@ -26,6 +28,8 @@ function jsonThrowOnError()
json_decode($json, true, 215, JSON_THROW_ON_ERROR);

json_decode($json, true, 122, JSON_THROW_ON_ERROR);

json_decode($json, true, 122, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
}

?>
81 changes: 72 additions & 9 deletions rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersionInterface
{
private bool $hasChanged = false;
private const FLAGS = ['JSON_THROW_ON_ERROR'];

public function __construct(
private readonly ValueResolver $valueResolver,
Expand Down Expand Up @@ -135,20 +136,22 @@

private function processJsonEncode(FuncCall $funcCall): ?FuncCall
{
$flags = [];
if (isset($funcCall->args[1])) {
return null;
$flags = $this->getFlags($funcCall->args[1]);
}
if (!is_null($newArg = $this->getArgWithFlags($flags))) {
$this->hasChanged = true;
$funcCall->args[1] = $newArg;
}

$this->hasChanged = true;

$funcCall->args[1] = new Arg($this->createConstFetch('JSON_THROW_ON_ERROR'));
return $funcCall;
}

private function processJsonDecode(FuncCall $funcCall): ?FuncCall
{
$flags = [];
if (isset($funcCall->args[3])) {
return null;
$flags = $this->getFlags($funcCall->args[3]);
}

// set default to inter-args
Expand All @@ -160,9 +163,10 @@
$funcCall->args[2] = new Arg(new Int_(512));
}

$this->hasChanged = true;
$funcCall->args[3] = new Arg($this->createConstFetch('JSON_THROW_ON_ERROR'));

if (!is_null($newArg = $this->getArgWithFlags($flags))) {
$this->hasChanged = true;
$funcCall->args[3] = $newArg;
}
return $funcCall;
}

Expand All @@ -186,4 +190,63 @@

return is_array($value);
}

private function getFlags(Arg|Node\Expr\BinaryOp\BitwiseOr|ConstFetch $arg, array $result = []): array
{
if ($arg instanceof ConstFetch) {
$constFetch = $arg;
} else {
if ($arg instanceof Arg) {
$array = $arg->value->jsonSerialize();
} else {
$array = $arg->jsonSerialize();
}
if ($arg->value instanceof ConstFetch) { // single flag
$constFetch = $arg->value;
} else { // multiple flag
$result = $this->getFlags($array['left'], $result);
$constFetch = $array['right'];
}
}
if (!is_null($constFetch)) {
$result[] = $constFetch->jsonSerialize()['name']->getFirst();
}
return $result;
}

private function getArgWithFlags(array $flags): Arg|null
{
$oldNbFlags = count($flags);
$flags = array_values(array_unique([...$flags, ...self::FLAGS]));
$newNbFlags = count($flags);
if ($oldNbFlags === $newNbFlags) {
return null;
}
if ($newNbFlags === 1) {
return new Arg($this->createConstFetch($flags[0]));
}
/** @var ConstFetch[] $constFetchs */

Check warning on line 228 in rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php

View workflow job for this annotation

GitHub Actions / Check for typos

"Fetchs" should be "Fetches".
$constFetchs = [];

Check warning on line 229 in rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php

View workflow job for this annotation

GitHub Actions / Check for typos

"Fetchs" should be "Fetches".
foreach ($flags as $flag) {
$constFetchs[] = $this->createConstFetch($flag);

Check warning on line 231 in rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php

View workflow job for this annotation

GitHub Actions / Check for typos

"Fetchs" should be "Fetches".
}
$result = null;
foreach ($constFetchs as $i => $constFetch) {

Check warning on line 234 in rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php

View workflow job for this annotation

GitHub Actions / Check for typos

"Fetchs" should be "Fetches".
if ($i === 1) {
continue;
}
if (is_null($result)) {
$result = new Node\Expr\BinaryOp\BitwiseOr(
$constFetch,
$constFetchs[$i + 1],

Check warning on line 241 in rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php

View workflow job for this annotation

GitHub Actions / Check for typos

"Fetchs" should be "Fetches".
);
} else {
$result = new Node\Expr\BinaryOp\BitwiseOr(
$result,
$constFetch
);
}
}
return new Arg($result);
}
}
Loading