From: Vincent Vanwaelscappel Date: Mon, 15 Jan 2024 16:33:50 +0000 (+0100) Subject: wip #6629 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=f15e99193c36a9b70d5a0cf523d8ff42680e9abb;p=cubist_util.git wip #6629 --- diff --git a/src/Math.php b/src/Math.php index daf9172..7cd2a60 100644 --- a/src/Math.php +++ b/src/Math.php @@ -4,11 +4,16 @@ namespace Cubist\Util; class Math { - public static function toBytes($val) { + /** + * @param $val + * @return float + */ + public static function toBytes($val) + { $val = trim($val); - $last = strtolower($val[strlen($val)-1]); - $val = substr($val, 0, -1); - switch($last) { + $last = strtolower($val[strlen($val) - 1]); + $val = (float)substr($val, 0, -1); + switch ($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; diff --git a/src/PHP.php b/src/PHP.php index b3d5ccd..48749c9 100644 --- a/src/PHP.php +++ b/src/PHP.php @@ -24,7 +24,8 @@ class PHP public static function isCloseToMemoryLimit($throw = false, $ratio = 0.9) { - $res = (memory_get_usage() / Math::toBytes(ini_get('memory_limit'))) > $ratio; + $limit = Math::toBytes(ini_get('memory_limit')); + $res = $limit > 0 && (memory_get_usage() / $limit) > $ratio; if ($throw && $res) { throw new \Exception('Memory is close to the limit'); }