From a3f51f10808a38bc8ca8024e57fedd7a3dbca2ef Mon Sep 17 00:00:00 2001 From: FOSSBilling Bot <121974958+fossbilling-bot@users.noreply.github.com> Date: Wed, 14 May 2025 14:51:35 +0100 Subject: [PATCH] Apply PHP Code Styling & Quality Changes (#2727) Co-authored-by: FOSSBilling Bot --- src/library/Box/TwigExtensions.php | 2 +- src/library/FOSSBilling/Tools.php | 21 +++++++++++++-------- src/modules/Servicehosting/Service.php | 21 ++++++++++++--------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/library/Box/TwigExtensions.php b/src/library/Box/TwigExtensions.php index b6bf65006..accc3ec71 100644 --- a/src/library/Box/TwigExtensions.php +++ b/src/library/Box/TwigExtensions.php @@ -275,7 +275,7 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte public function twig_size_filter($value): string { - return \FOSSBilling\Tools::humanReadableBytes($value); + return FOSSBilling\Tools::humanReadableBytes($value); } public function twig_markdown_filter(Twig\Environment $env, $value) diff --git a/src/library/FOSSBilling/Tools.php b/src/library/FOSSBilling/Tools.php index 71241ca3c..e3c474239 100644 --- a/src/library/FOSSBilling/Tools.php +++ b/src/library/FOSSBilling/Tools.php @@ -415,17 +415,22 @@ class Tools /** * Converts bytes to a human-readable format (B, KB, MB, GB and TB). - * - * @param int $bytes - * @return string */ public static function humanReadableBytes(int $bytes): string { - if ($bytes < 1024) return $bytes . ' B'; - if ($bytes < 1048576) return round($bytes / 1024, 2) . ' KB'; - if ($bytes < 1073741824) return round($bytes / 1048576, 2) . ' MB'; - if ($bytes < 1099511627776) return round($bytes / 1073741824, 2) . ' GB'; - + if ($bytes < 1024) { + return $bytes . ' B'; + } + if ($bytes < 1048576) { + return round($bytes / 1024, 2) . ' KB'; + } + if ($bytes < 1073741824) { + return round($bytes / 1048576, 2) . ' MB'; + } + if ($bytes < 1099511627776) { + return round($bytes / 1073741824, 2) . ' GB'; + } + return round($bytes / 1099511627776, 2) . ' TB'; } } diff --git a/src/modules/Servicehosting/Service.php b/src/modules/Servicehosting/Service.php index e4b94ce58..66ecac964 100644 --- a/src/modules/Servicehosting/Service.php +++ b/src/modules/Servicehosting/Service.php @@ -689,7 +689,9 @@ class Service implements InjectionAwareInterface $model->hostname = $data['hostname'] ?? null; $assigned_ips = $data['assigned_ips'] ?? ''; - if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($assigned_ips); + if (!empty($assigned_ips)) { + $model->assigned_ips = self::processAssignedIPs($assigned_ips); + } $model->active = $data['active'] ?? 1; $model->status_url = $data['status_url'] ?? null; @@ -733,7 +735,9 @@ class Service implements InjectionAwareInterface $model->hostname = $data['hostname'] ?? $model->hostname; $assigned_ips = $data['assigned_ips'] ?? ''; - if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($assigned_ips); + if (!empty($assigned_ips)) { + $model->assigned_ips = self::processAssignedIPs($assigned_ips); + } $model->active = $data['active'] ?? $model->active; $model->status_url = $data['status_url'] ?? $model->status_url; @@ -1087,15 +1091,16 @@ class Service implements InjectionAwareInterface } /** - * Post-processing for the assigned IPs. + * Post-processing for the assigned IPs. * The data from the server management form (/admin/servicehosting/server/{id}) sends the data like this: * assigned_ips: "10.0.0.1\n10.0.0.2\n" * As you see, it isn't really an array, it also doesn't filter out empty lines and whitespaces at all. - * + * * We can't rely on it as-is. So we need to make sure only the valid IP addresses are going inside the array. * We'll split on any type of line break (\n, \r\n, or \r) and make sure each IP address is valid. - * + * * @param string $assigned_ips Raw string from the form data (example form: /admin/servicehosting/server/{ip}) + * * @return string JSON encoded array of filtered valid IPs */ public static function processAssignedIPs(string $assigned_ips): string @@ -1105,12 +1110,10 @@ class Service implements InjectionAwareInterface // Trim each entry and remove any empty strings $array = array_map('trim', $array); - $array = array_filter($array, fn($ip) => $ip !== ''); + $array = array_filter($array, fn ($ip): bool => $ip !== ''); // Validate that each entry is a valid IP address (works both with IPv4 and IPv6) - $array = array_filter($array, function($ip) { - return filter_var($ip, FILTER_VALIDATE_IP); - }); + $array = array_filter($array, fn ($ip) => filter_var($ip, FILTER_VALIDATE_IP)); return json_encode(array_values($array)); }