diff options
author | Simon Rettberg | 2023-11-15 17:53:47 +0100 |
---|---|---|
committer | Simon Rettberg | 2023-11-15 17:53:47 +0100 |
commit | aa49154e5a9ee135c3f0286ddda8a01a5290b1ed (patch) | |
tree | 0397403f11a8a890ee11458ea0b1887796480115 /inc/util.inc.php | |
parent | idea: Always inspect as if strict_types is enabled (diff) | |
download | slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.tar.gz slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.tar.xz slx-admin-aa49154e5a9ee135c3f0286ddda8a01a5290b1ed.zip |
Fix more type errors, stricter typing
Diffstat (limited to 'inc/util.inc.php')
-rw-r--r-- | inc/util.inc.php | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/inc/util.inc.php b/inc/util.inc.php index 0c402a6a..a5ccdc77 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + use JetBrains\PhpStorm\NoReturn; class Util @@ -98,7 +100,7 @@ class Util * @param int $shift how many units to skip, i.e. if you pass in KiB or MiB * @return string human-readable string representing the given file size */ - public static function readableFileSize(float $bytes, int $decimals = -1, int $shift = 0): string + public static function readableFileSize($bytes, int $decimals = -1, int $shift = 0): string { // round doesn't reliably work for large floats, pick workaround depending on OS if (PHP_INT_SIZE === 4) { @@ -113,10 +115,10 @@ class Util } else { $bytes /= 1024 ** $factor; if ($decimals === -1) { - $decimals = 2 - floor(strlen((int)$bytes) - 1); + $decimals = 2 - strlen((string)floor($bytes)) - 1; } } - return Dictionary::number($bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); + return Dictionary::number((float)$bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); } public static function sanitizeFilename(string $name): string @@ -131,25 +133,24 @@ class Util * * @param string $path path to check for safety * @param string $prefix required prefix of $path - * @return false|string */ - public static function safePath(string $path, string $prefix = ''): string + public static function safePath(string $path, string $prefix = ''): ?string { if (empty($path)) - return false; + return null; $path = trim($path); if ($path[0] == '/' || preg_match('/[\x00-\x19?*]/', $path)) - return false; + return null; if (strpos($path, '..') !== false) - return false; + return null; if (substr($path, 0, 2) !== './') $path = "./$path"; - if (empty($prefix)) - return $path; - if (substr($prefix, 0, 2) !== './') - $prefix = "./$prefix"; - if (substr($path, 0, strlen($prefix)) !== $prefix) - return false; + if (!empty($prefix)) { + if (substr($prefix, 0, 2) !== './') + $prefix = "./$prefix"; + if (substr($path, 0, strlen($prefix)) !== $prefix) + return null; + } return $path; } @@ -381,7 +382,6 @@ class Util */ public static function formatDuration(int $seconds, bool $showSecs = true): string { - settype($seconds, 'int'); static $UNITS = ['y' => 31536000, 'mon' => 2592000, 'd' => 86400]; $parts = []; $prev = false; @@ -397,7 +397,7 @@ class Util $prev = true; } } - $parts[] = gmdate($showSecs ? 'H:i:s' : 'H:i', $seconds); + $parts[] = gmdate($showSecs ? 'H:i:s' : 'H:i', (int)$seconds); return implode(' ', $parts); } |