From 28261d8a7ff5f6dbf54da3d50933e161e38254f0 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 22 Oct 2024 13:51:28 +0200 Subject: [PATCH] Fix PhpStan complaining about arithmetic operations between string and int --- library/Director/Application/MemoryLimit.php | 9 +++++---- .../IcingaConfig/IcingaConfigHelper.php | 10 ++++++---- phpstan-baseline.neon | 20 ------------------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/library/Director/Application/MemoryLimit.php b/library/Director/Application/MemoryLimit.php index beb04609..817bfa80 100644 --- a/library/Director/Application/MemoryLimit.php +++ b/library/Director/Application/MemoryLimit.php @@ -35,19 +35,20 @@ class MemoryLimit $val = trim($string); if (preg_match('/^(\d+)([KMG])$/', $val, $m)) { - $val = $m[1]; + $val = (int) $m[1]; + switch ($m[2]) { case 'G': $val *= 1024; - // Intentional fall-through + // no break case 'M': $val *= 1024; - // Intentional fall-through + // no break case 'K': $val *= 1024; } } - return intval($val); + return (int) $val; } } diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 634337f0..2310c9f1 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -298,18 +298,20 @@ class IcingaConfigHelper )); } + $duration = (int) $m[1]; + switch ($m[2]) { case 'd': - $value += $m[1] * 86400; + $value += $duration * 86400; break; case 'h': - $value += $m[1] * 3600; + $value += $duration * 3600; break; case 'm': - $value += $m[1] * 60; + $value += $duration * 60; break; default: - $value += (int) $m[1]; + $value += $duration; } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 259e9406..ad8ee24a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -240,11 +240,6 @@ parameters: count: 1 path: application/views/helpers/FormStoredPassword.php - - - message: "#^Binary operation \"\\*\\=\" between string and 1024 results in an error\\.$#" - count: 1 - path: library/Director/Application/MemoryLimit.php - - message: "#^Constructor of class Icinga\\\\Module\\\\Director\\\\Core\\\\RestApiClient has an unused parameter \\$cn\\.$#" count: 1 @@ -395,21 +390,6 @@ parameters: count: 1 path: library/Director/IcingaConfig/AgentWizard.php - - - message: "#^Binary operation \"\\*\" between string and 3600 results in an error\\.$#" - count: 1 - path: library/Director/IcingaConfig/IcingaConfigHelper.php - - - - message: "#^Binary operation \"\\*\" between string and 60 results in an error\\.$#" - count: 1 - path: library/Director/IcingaConfig/IcingaConfigHelper.php - - - - message: "#^Binary operation \"\\*\" between string and 86400 results in an error\\.$#" - count: 1 - path: library/Director/IcingaConfig/IcingaConfigHelper.php - - message: "#^Call to an undefined method Icinga\\\\Module\\\\Director\\\\Web\\\\Form\\\\QuickForm\\:\\:getSentOrObjectSetting\\(\\)\\.$#" count: 3