Skip to content

Commit

Permalink
Use JSON_THROW_ON_ERROR
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSuisse committed Sep 13, 2019
1 parent 886cd58 commit 70a6135
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
15 changes: 2 additions & 13 deletions src/Storage/APCUStore.php
Expand Up @@ -26,7 +26,6 @@
use function implode; use function implode;
use function json_decode; use function json_decode;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function pack; use function pack;
use function sort; use function sort;
use function strcmp; use function strcmp;
Expand Down Expand Up @@ -371,15 +370,10 @@ private static function sortSamples(array &$samples) : void


/** /**
* @param string[] $values * @param string[] $values
*
* @throws RuntimeException
*/ */
private function encodeLabelValues(array $values) : string private function encodeLabelValues(array $values) : string
{ {
$json = json_encode($values); $json = json_encode($values, JSON_THROW_ON_ERROR);
if ($json === false) {
throw new RuntimeException(json_last_error_msg());
}


return base64_encode($json); return base64_encode($json);
} }
Expand All @@ -395,11 +389,6 @@ private function decodeLabelValues(string $values) : array
if ($json === false) { if ($json === false) {
throw new RuntimeException('Cannot base64 decode label values'); throw new RuntimeException('Cannot base64 decode label values');
} }
$decodedValues = json_decode($json, true); return json_decode($json, true, JSON_THROW_ON_ERROR);
if ($decodedValues === false) {
throw new RuntimeException(json_last_error_msg());
}

return $decodedValues;
} }
} }
15 changes: 2 additions & 13 deletions src/Storage/InMemoryStore.php
Expand Up @@ -20,7 +20,6 @@
use function implode; use function implode;
use function json_decode; use function json_decode;
use function json_encode; use function json_encode;
use function json_last_error_msg;
use function sort; use function sort;
use function strcmp; use function strcmp;
use function usort; use function usort;
Expand Down Expand Up @@ -321,15 +320,10 @@ private function metaData(MetricName $name, string $help, LabelNames $labelNames


/** /**
* @param string[] $values * @param string[] $values
*
* @throws RuntimeException
*/ */
private function encodeLabelValues(array $values) : string private function encodeLabelValues(array $values) : string
{ {
$json = json_encode($values); $json = json_encode($values, JSON_THROW_ON_ERROR);
if ($json === false) {
throw new RuntimeException(json_last_error_msg());
}


return base64_encode($json); return base64_encode($json);
} }
Expand All @@ -345,11 +339,6 @@ private function decodeLabelValues(string $values) : array
if ($json === false) { if ($json === false) {
throw new RuntimeException('Cannot base64 decode label values'); throw new RuntimeException('Cannot base64 decode label values');
} }
$decodedValues = json_decode($json, true); return json_decode($json, true, 512, JSON_THROW_ON_ERROR);
if ($decodedValues === false) {
throw new RuntimeException(json_last_error_msg());
}

return $decodedValues;
} }
} }

0 comments on commit 70a6135

Please sign in to comment.