From e62b21ca71a60dceb01a59475e6c9c21dd1a2686 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 11 Jan 2023 18:51:07 +0700 Subject: [PATCH] utilize samsonasik/ArrayLookup --- composer.json | 1 + src/HeroFunction.php | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 268446d8..f99d2626 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "laminas/laminas-psr7bridge": "^1.8", "laminas/laminas-uri": "^2.10", "psr/container": "^1.1.2 || 2.0", + "samsonasik/array-lookup": "^1.0", "seld/jsonlint": "^1.9", "webmozart/assert": "^1.11" }, diff --git a/src/HeroFunction.php b/src/HeroFunction.php index ee439cd3..0806e9cf 100644 --- a/src/HeroFunction.php +++ b/src/HeroFunction.php @@ -4,6 +4,7 @@ namespace ErrorHeroModule; +use ArrayLookup\AtLeast; use Seld\JsonLint\JsonParser; use Throwable; @@ -24,23 +25,17 @@ function detectMessageContentType(string $message): string function isExcludedException(array $excludeExceptionsConfig, Throwable $throwable): bool { $exceptionOrErrorClass = $throwable::class; + $message = $throwable->getMessage(); - $isExcluded = false; - foreach ($excludeExceptionsConfig as $excludeExceptionConfig) { - if ($exceptionOrErrorClass === $excludeExceptionConfig) { - $isExcluded = true; - break; + $filter = static function (string|array $excludeExceptionConfig) use ($exceptionOrErrorClass, $message): bool { + if ($excludeExceptionConfig === $exceptionOrErrorClass) { + return true; } - if ( - is_array($excludeExceptionConfig) + return is_array($excludeExceptionConfig) && $excludeExceptionConfig[0] === $exceptionOrErrorClass - && $excludeExceptionConfig[1] === $throwable->getMessage() - ) { - $isExcluded = true; - break; - } - } + && $excludeExceptionConfig[1] === $message; + }; - return $isExcluded; + return AtLeast::once($excludeExceptionsConfig, $filter); }