From 5812a6bb68189415866f4ab4728d2497de7d5795 Mon Sep 17 00:00:00 2001 From: rodehoed <6515395+rodehoed@users.noreply.github.com> Date: Wed, 19 May 2021 08:47:35 +0200 Subject: [PATCH] Optimized the canPass() functions --- lib/Persistence/TrafficLimiter.php | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/Persistence/TrafficLimiter.php b/lib/Persistence/TrafficLimiter.php index 39755404..34584b52 100644 --- a/lib/Persistence/TrafficLimiter.php +++ b/lib/Persistence/TrafficLimiter.php @@ -120,24 +120,18 @@ class TrafficLimiter extends AbstractPersistence // Match $_ipKey to $ipRange and if it matches it will return with a true $address = \IPLib\Factory::addressFromString($_SERVER[self::$_ipKey]); $range = \IPLib\Factory::rangeFromString(trim($ipRange)); - // If $range is null something went wrong (possible invalid ip given in config) - if ($range == null) { - return false; - } else { - // Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false - try { - $contained = $address->matches($range); - } catch (\Exception $e) { - // If something is wrong with matching the ip, we set $contained to false - return false; - } - } - // Matches return true! - if ($contained === true) { - return true; - } else { - return false; + + // If $range is null something went wrong (possible invalid ip given in config). It's here becaue matches($range) does not accepts null vallue + if ($range == null) return false; + + // Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false + try { + return $address->matches($range); + } catch (\Exception $e) { + // If something is wrong with matching the ip, we do nothing } + + return false; } /**