From c72dd5ccc4407c8604ac42a0c01ae59ec74bbb9e Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 27 Jan 2023 12:51:53 +0300 Subject: [PATCH] small optimization of getting name of the JSON error constant --- src/AbstractJsonEncoder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AbstractJsonEncoder.php b/src/AbstractJsonEncoder.php index 08964e0..caece70 100644 --- a/src/AbstractJsonEncoder.php +++ b/src/AbstractJsonEncoder.php @@ -403,7 +403,7 @@ private function outputJson($value, $token) $error = json_last_error(); if ($error !== JSON_ERROR_NONE) { - $this->addError(sprintf('%s (%s)', json_last_error_msg(), $this->getJsonErrorName($error))); + $this->addError(sprintf('%s (%s)', json_last_error_msg(), $this->getJsonErrorConstantName($error))); } $this->output($encoded, $token); @@ -414,9 +414,9 @@ private function outputJson($value, $token) * @param int $error The error code to find * @return string The name for the error code */ - private function getJsonErrorName($error) + private function getJsonErrorConstantName($error) { - $matches = array_keys(get_defined_constants(), $error, true); + $matches = array_keys(get_defined_constants(true)['json'], $error, true); $prefix = 'JSON_ERROR_'; $prefixLength = strlen($prefix); $name = 'UNKNOWN_ERROR';