From 09839f34519d5b9eb2c64e73869ce7159909846d Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 31 Aug 2023 19:04:45 +0900 Subject: [PATCH] phpstan fixes --- www/admin/class_test.session.php | 35 +++++++++++++++----------- www/admin/class_test.session.read.php | 16 ++++++------ www/lib/CoreLibs/Combined/DateTime.php | 20 +++++++++------ www/lib/CoreLibs/Output/Image.php | 26 ++++++++++++++----- 4 files changed, 61 insertions(+), 36 deletions(-) diff --git a/www/admin/class_test.session.php b/www/admin/class_test.session.php index 0f685e87..7cc610c2 100644 --- a/www/admin/class_test.session.php +++ b/www/admin/class_test.session.php @@ -78,16 +78,18 @@ if (isset($_SESSION)) { # print "[UNSET] To set session name valid: " . ($session->checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "
"; -if (false === ($session_id = $session->startSession($session_name))) { - print "[FAILED] Session start failed: " . $session->getErrorStr() . "
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[SET] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } // set again -if (false === ($session_id = $session->startSession($session_name))) { - print "[2 FAILED] Session start failed: " . $session->getErrorStr() . "
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[2 SET] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[2 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } print "[SET] Current session id: " . $session->getSessionId() . "
"; print "[SET] Current session name: " . $session->getSessionName() . "
"; @@ -125,10 +127,11 @@ print "[READ MAGIC] unset Isset: " . (isset($session->setwrap) ? 'Yes' : 'No') . // differnt session name $session_name = 'class-test-session-ALT'; -if (false === ($session_id = $session->startSession($session_name))) { - print "[3 FAILED] Session start failed: " . $session->getErrorStr() . "
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[3 SET] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[3 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } print "[SET AGAIN] Current session id: " . $session->getSessionId() . "
"; @@ -141,10 +144,11 @@ $_SESSION['will_never_be_written'] = 'empty'; // open again $session_name = 'class-test-session'; -if (false === ($session_id = $session->startSession($session_name))) { - print "[4 FAILED] Session start failed: " . $session->getErrorStr() . "
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[4 SET] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[4 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } print "[START AGAIN] Current session id: " . $session->getSessionId() . "
"; $_SESSION['will_be_written_again'] = 'Full'; @@ -153,10 +157,11 @@ $_SESSION['will_be_written_again'] = 'Full'; $session->writeClose(); // invalid $session_name = '123'; -if (false === ($session_id = $session->startSession($session_name))) { - print "[5 FAILED] Session start failed: " . $session->getErrorStr() . "
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[5 SET] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[5 FAILED] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } print "[BAD NAME] Current session id: " . $session->getSessionId() . "
"; print "[BAD NAME] Current session name: " . $session->getSessionName() . "
"; diff --git a/www/admin/class_test.session.read.php b/www/admin/class_test.session.read.php index ec40f7c7..b871ea8c 100644 --- a/www/admin/class_test.session.read.php +++ b/www/admin/class_test.session.read.php @@ -69,16 +69,18 @@ print "[UNSET] Current session status: " . getSessionStatusString($session->getS print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; // start -if (false === ($session_id = $session->startSession($session_name))) { - print "Session start failed: " . $session->getErrorStr() . "
"; -} else { - print "Current session id: " . $session_id . "
"; +try { + $session_id = $session->startSession($session_name); + print "[1] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[1] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } // set again -if (false === ($session_id = $session->startSession($session_name))) { - print "[2] Session start failed
"; -} else { +try { + $session_id = $session->startSession($session_name); print "[2] Current session id: " . $session_id . "
"; +} catch (\Exception $e) { + print "[2] Session start failed:
" . $e->getMessage() . "
" . $e . "
"; } print "[SET] Current session id: " . $session->getSessionId() . "
"; print "[SET] Current session name: " . $session->getSessionName() . "
"; diff --git a/www/lib/CoreLibs/Combined/DateTime.php b/www/lib/CoreLibs/Combined/DateTime.php index fd6f84ef..286b8382 100644 --- a/www/lib/CoreLibs/Combined/DateTime.php +++ b/www/lib/CoreLibs/Combined/DateTime.php @@ -324,7 +324,7 @@ class DateTime * @return int int -1 (se) as difference * @throws \UnexpectedValueException On empty start/end values */ - public static function compareDate(string $start_date, string $end_date): int|bool + public static function compareDate(string $start_date, string $end_date): int { // pre check for empty or wrong if ($start_date == '--' || $end_date == '--' || empty($start_date) || empty($end_date)) { @@ -337,18 +337,20 @@ class DateTime if (($end_timestamp = strtotime($end_date)) === false) { throw new \UnexpectedValueException("Error parsing end date through strtotime()"); } + $comp = 0; // convert anything to Y-m-d and then to timestamp // this is to remove any time parts $start_timestamp = strtotime(date('Y-m-d', $start_timestamp)); $end_timestamp = strtotime(date('Y-m-d', $end_timestamp)); // compare, or end with false if ($start_timestamp < $end_timestamp) { - return -1; + $comp = -1; } elseif ($start_timestamp == $end_timestamp) { - return 0; + $comp = 0; } elseif ($start_timestamp > $end_timestamp) { - return 1; + $comp = 1; } + return $comp; } /** @@ -365,7 +367,7 @@ class DateTime * @return int -1 (se) as difference * @throws \UnexpectedValueException On empty start/end values */ - public static function compareDateTime(string $start_datetime, string $end_datetime): int|bool + public static function compareDateTime(string $start_datetime, string $end_datetime): int { // pre check for empty or wrong if ($start_datetime == '--' || $end_datetime == '--' || empty($start_datetime) || empty($end_datetime)) { @@ -378,14 +380,16 @@ class DateTime if (($end_timestamp = strtotime($end_datetime)) === false) { throw new \UnexpectedValueException("Error parsing end timestamp through strtotime()"); } + $comp = 0; // compare, or return false if ($start_timestamp < $end_timestamp) { - return -1; + $comp = -1; } elseif ($start_timestamp == $end_timestamp) { - return 0; + $comp = 0; } elseif ($start_timestamp > $end_timestamp) { - return 1; + $comp = 1; } + return $comp; } /** diff --git a/www/lib/CoreLibs/Output/Image.php b/www/lib/CoreLibs/Output/Image.php index a7d494fa..9d385117 100644 --- a/www/lib/CoreLibs/Output/Image.php +++ b/www/lib/CoreLibs/Output/Image.php @@ -292,12 +292,18 @@ class Image // image, copy source image, offset in image, source x/y, new size, source image size $thumb = imagecreatetruecolor($thumb_width_r, $thumb_height_r); if ($thumb === false) { - return false; + throw new \RuntimeException( + 'imagecreatetruecolor failed: ' . $thumbnail . ', ' . $filename, + 1 + ); } if ($img_type == IMAGETYPE_PNG) { $imagecolorallocatealpha = imagecolorallocatealpha($thumb, 0, 0, 0, 127); if ($imagecolorallocatealpha === false) { - return false; + throw new \RuntimeException( + 'imagecolorallocatealpha failed: ' . $thumbnail . ', ' . $filename, + 2 + ); } // preservere transaprency imagecolortransparent( @@ -359,8 +365,10 @@ class Image imagedestroy($source); imagedestroy($thumb); } else { - $exception_message = 'Invalid source image file. Only JPEG/PNG are allowed: ' . $filename; - $thumbnail = false; + throw new \RuntimeException( + 'Invalid source image file. Only JPEG/PNG are allowed: ' . $filename, + 3 + ); } } } else { @@ -401,14 +409,20 @@ class Image } $thumb = imagecreatetruecolor($thumb_width, $thumb_height); if ($thumb === false) { - return false; + throw new \RuntimeException( + 'imagecreatetruecolor dummy failed: ' . $thumbnail . ', ' . $filename, + 3 + ); } // add outside border px = 5% (rounded up) // eg 50px -> 2.5px $gray = imagecolorallocate($thumb, 200, 200, 200); $white = imagecolorallocate($thumb, 255, 255, 255); if ($gray === false || $white === false) { - return false; + throw new \RuntimeException( + 'imagecolorallocate/imagecolorallocate dummy failed: ' . $thumbnail . ', ' . $filename, + 2 + ); } // fill gray background imagefill($thumb, 0, 0, $gray);