1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 05:07:36 +02:00

Replace switch with match (#1655)

The `match` expression is available as of PHP 8.0 and provides a more
intuitive interface than `switch` when a single value is returned.

Note that it performs an identity check (===) between the value and
case instead of a weak equality check (==).

https://www.php.net/manual/en/control-structures.match.php
This commit is contained in:
Dan Hemberger
2022-04-19 12:51:29 -07:00
committed by GitHub
parent 14f39fe0ce
commit e4bb5c5cf3
3 changed files with 29 additions and 58 deletions

View File

@@ -260,39 +260,23 @@ class ErrorHandler
*/ */
private static function codeToString($code): string private static function codeToString($code): string
{ {
switch ($code) { return match ($code) {
case E_ERROR: E_ERROR => 'E_ERROR',
return 'E_ERROR'; E_WARNING => 'E_WARNING',
case E_WARNING: E_PARSE => 'E_PARSE',
return 'E_WARNING'; E_NOTICE => 'E_NOTICE',
case E_PARSE: E_CORE_ERROR => 'E_CORE_ERROR',
return 'E_PARSE'; E_CORE_WARNING => 'E_CORE_WARNING',
case E_NOTICE: E_COMPILE_ERROR => 'E_COMPILE_ERROR',
return 'E_NOTICE'; E_COMPILE_WARNING => 'E_COMPILE_WARNING',
case E_CORE_ERROR: E_USER_ERROR => 'E_USER_ERROR',
return 'E_CORE_ERROR'; E_USER_WARNING => 'E_USER_WARNING',
case E_CORE_WARNING: E_USER_NOTICE => 'E_USER_NOTICE',
return 'E_CORE_WARNING'; E_STRICT => 'E_STRICT',
case E_COMPILE_ERROR: E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
return 'E_COMPILE_ERROR'; E_DEPRECATED => 'E_DEPRECATED',
case E_COMPILE_WARNING: E_USER_DEPRECATED => 'E_USER_DEPRECATED',
return 'E_COMPILE_WARNING'; default => 'Unknown PHP error',
case E_USER_ERROR: };
return 'E_USER_ERROR';
case E_USER_WARNING:
return 'E_USER_WARNING';
case E_USER_NOTICE:
return 'E_USER_NOTICE';
case E_STRICT:
return 'E_STRICT';
case E_RECOVERABLE_ERROR:
return 'E_RECOVERABLE_ERROR';
case E_DEPRECATED:
return 'E_DEPRECATED';
case E_USER_DEPRECATED:
return 'E_USER_DEPRECATED';
}
return 'Unknown PHP error';
} }
} }

View File

@@ -98,14 +98,10 @@ class JsonFormatter extends NormalizerFormatter
*/ */
public function formatBatch(array $records): string public function formatBatch(array $records): string
{ {
switch ($this->batchMode) { return match ($this->batchMode) {
case static::BATCH_MODE_NEWLINES: static::BATCH_MODE_NEWLINES => $this->formatBatchNewlines($records),
return $this->formatBatchNewlines($records); default => $this->formatBatchJson($records),
};
case static::BATCH_MODE_JSON:
default:
return $this->formatBatchJson($records);
}
} }
/** /**

View File

@@ -168,22 +168,13 @@ final class Utils
*/ */
private static function throwEncodeError(int $code, $data): never private static function throwEncodeError(int $code, $data): never
{ {
switch ($code) { $msg = match ($code) {
case JSON_ERROR_DEPTH: JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
$msg = 'Maximum stack depth exceeded'; JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
break; JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
case JSON_ERROR_STATE_MISMATCH: JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
$msg = 'Underflow or the modes mismatch'; default => 'Unknown error',
break; };
case JSON_ERROR_CTRL_CHAR:
$msg = 'Unexpected control character found';
break;
case JSON_ERROR_UTF8:
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$msg = 'Unknown error';
}
throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true)); throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true));
} }