Skip to content

Commit

Permalink
rector.php: add JsonThrowOnErrorRector rule
Browse files Browse the repository at this point in the history
Using the JSON_THROW_ON_ERROR flag of `json_encode` ensures that we
throw on error instead of returning false. This is safer, and means we
don't need to check the return value against false.

http://wiki.php.net/rfc/json_throw_on_error
https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md#jsonthrowonerrorrector
  • Loading branch information
hemberger committed Feb 4, 2023
1 parent 00e20a0 commit 78baae6
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 8 deletions.
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Rector\Config\RectorConfig;
use Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector;
use Rector\Php80\Rector\FuncCall\ClassOnObjectRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
Expand All @@ -13,6 +14,7 @@
]);
$rectorConfig->importNames(true, false);
$rectorConfig->rule(DirNameFileConstantToDirConstantRector::class);
$rectorConfig->rule(JsonThrowOnErrorRector::class);
$rectorConfig->rule(NullCoalescingOperatorRector::class);
$rectorConfig->rule(ClassOnObjectRector::class);
$rectorConfig->rule(FirstClassCallableRector::class);
Expand Down
2 changes: 1 addition & 1 deletion src/htdocs/map_warps.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
$data = json_encode([
'nodes' => $nodes,
'links' => $links,
]);
], JSON_THROW_ON_ERROR);

$template = Template::getInstance();
$template->assign('GameName', $game->getName());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Default/smr.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function handleUserError(string $message): never {
$errorHREF = $container->href();
// json_encode the HREF as a safety precaution
$template = Template::getInstance();
$template->addJavascriptForAjax('EVAL', 'location.href = ' . json_encode($errorHREF));
$template->addJavascriptForAjax('EVAL', 'location.href = ' . json_encode($errorHREF, JSON_THROW_ON_ERROR));
}
$container->go();
}
Expand Down
7 changes: 2 additions & 5 deletions src/lib/Smr/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ public function addJavascriptForAjax(string $varName, mixed $obj): string {
if (isset($this->ajaxJS[$varName])) {
throw new Exception('Trying to set javascript val twice: ' . $varName);
}
$json = json_encode($obj);
if ($json === false) {
throw new Exception('Failed to encode to json: ' . $varName);
}
$json = json_encode($obj, JSON_THROW_ON_ERROR);
return $this->ajaxJS[$varName] = $json;
}

Expand Down Expand Up @@ -275,7 +272,7 @@ protected function convertHtmlToAjaxXml(string $str, bool $returnXml): string {
}
}
if ($returnXml && count($this->jsAlerts) > 0) {
$js = '<ALERT>' . json_encode($this->jsAlerts) . '</ALERT>';
$js = '<ALERT>' . json_encode($this->jsAlerts, JSON_THROW_ON_ERROR) . '</ALERT>';
}
if (strlen($js) > 0) {
$xml .= '<JS>' . $js . '</JS>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

foreach ($this->jsAlerts as $string) {
?>alert(<?php echo json_encode($string); ?>);<?php
?>alert(<?php echo json_encode($string, JSON_THROW_ON_ERROR); ?>);<?php
}

if (!empty($this->listjsInclude)) { ?>
Expand Down

0 comments on commit 78baae6

Please sign in to comment.