]> _ Git - cubist_util.git/commitdiff
wip #6629
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 15 Jan 2024 16:33:50 +0000 (17:33 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 15 Jan 2024 16:33:50 +0000 (17:33 +0100)
src/Math.php
src/PHP.php

index daf91728bd7cc479ff87fd9adf58f4ce23814c08..7cd2a60228b537643a2e66e2675a17436c2e78db 100644 (file)
@@ -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;
index b3d5ccd0a712034fe2dc891ea3a2a50592879333..48749c92e8a5cf44fee2de24df158a68a8568a10 100644 (file)
@@ -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');
         }