phpstan fixes

This commit is contained in:
Clemens Schwaighofer
2023-08-31 19:04:45 +09:00
parent 067e0aed5d
commit 09839f3451
4 changed files with 61 additions and 36 deletions

View File

@@ -78,16 +78,18 @@ if (isset($_SESSION)) {
#
print "[UNSET] To set session name valid: "
. ($session->checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[SET] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[FAILED] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
// set again
if (false === ($session_id = $session->startSession($session_name))) {
print "[2 FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[2 SET] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[2 FAILED] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
print "[SET] Current session id: " . $session->getSessionId() . "<br>";
print "[SET] Current session name: " . $session->getSessionName() . "<br>";
@@ -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() . "<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[3 SET] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[3 FAILED] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
print "[SET AGAIN] Current session id: " . $session->getSessionId() . "<br>";
@@ -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() . "<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[4 SET] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[4 FAILED] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
print "[START AGAIN] Current session id: " . $session->getSessionId() . "<br>";
$_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() . "<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[5 SET] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[5 FAILED] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
print "[BAD NAME] Current session id: " . $session->getSessionId() . "<br>";
print "[BAD NAME] Current session name: " . $session->getSessionName() . "<br>";

View File

@@ -69,16 +69,18 @@ print "[UNSET] Current session status: " . getSessionStatusString($session->getS
print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
// start
if (false === ($session_id = $session->startSession($session_name))) {
print "Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "Current session id: " . $session_id . "<br>";
try {
$session_id = $session->startSession($session_name);
print "[1] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[1] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
// set again
if (false === ($session_id = $session->startSession($session_name))) {
print "[2] Session start failed<br>";
} else {
try {
$session_id = $session->startSession($session_name);
print "[2] Current session id: " . $session_id . "<br>";
} catch (\Exception $e) {
print "[2] Session start failed:<br>" . $e->getMessage() . "<br>" . $e . "<br>";
}
print "[SET] Current session id: " . $session->getSessionId() . "<br>";
print "[SET] Current session name: " . $session->getSessionName() . "<br>";

View File

@@ -324,7 +324,7 @@ class DateTime
* @return int int -1 (s<e)/0 (s=e)/1 (s>e) 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 (s<e)/0 (s=e)/1 (s>e) 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;
}
/**

View File

@@ -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);