From 4e05c94912aa92d70f779d843fd9b63206581357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Sat, 14 Feb 2026 16:09:21 +0100 Subject: [PATCH] fix: correctly determine file size and avoid type mismatch --- src/Twig/AppRuntime.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Twig/AppRuntime.php b/src/Twig/AppRuntime.php index 5033021..7da302f 100644 --- a/src/Twig/AppRuntime.php +++ b/src/Twig/AppRuntime.php @@ -51,10 +51,14 @@ class AppRuntime implements RuntimeExtensionInterface public function formatBytes(int $bytes, ?int $precision = 2): string { - $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - $factor = floor((strlen($bytes) - 1) / 3); + if (0 === $bytes) { + return '0 B'; + } - return sprintf("%.{$precision}f", $bytes / (1024 ** $factor)).@$size[$factor]; + $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $factor = (int) floor(log($bytes, 1024)); + + return sprintf("%.{$precision}f %s", $bytes / (1024 ** $factor), $units[$factor]); } public function formatMoney(int $amount): string