diff options
| author | 2023-11-16 22:43:00 +0100 | |
|---|---|---|
| committer | 2023-11-16 22:43:00 +0100 | |
| commit | 30c7a61a9b410f023c56ef19b9389a61647d8768 (patch) | |
| tree | bb58408980ce5b86f1d2b4a9be29d55b2d46dbb1 /lib/Minz/Session.php | |
| parent | ee99e7e2cc228500efc1b539954c0ca6cd4c146d (diff) | |
Use strict_types (#5830)
* Little's optimisations and booleans in conditions
* Apply strict type
* Apply strict type
* Apply strict type
* Fix multiple bugs with PHP 8.2 and 8.3
* Many declares missing, more errors fixed
* Apply strict type
* Another approach
* Stronger typing for Minz_Session
* Fix case of SQLite
---------
Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'lib/Minz/Session.php')
| -rw-r--r-- | lib/Minz/Session.php | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/lib/Minz/Session.php b/lib/Minz/Session.php index 137f66b7a..4372c2683 100644 --- a/lib/Minz/Session.php +++ b/lib/Minz/Session.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * The Minz_Session class handles user’s session @@ -60,11 +61,60 @@ class Minz_Session { * @param string $p the parameter to retrieve * @param mixed|false $default the default value if the parameter doesn’t exist * @return mixed|false the value of the session variable, false if doesn’t exist + * @deprecated Use typed versions instead */ - public static function param(string $p, $default = false) { + private static function param(string $p, $default = false) { return $_SESSION[$p] ?? $default; } + /** @return array<string|int,string|array<string,mixed>> */ + public static function paramArray(string $key): array { + if (empty($_SESSION[$key]) || !is_array($_SESSION[$key])) { + return []; + } + return $_SESSION[$key]; + } + + public static function paramTernary(string $key): ?bool { + if (isset($_SESSION[$key])) { + $p = $_SESSION[$key]; + $tp = is_string($p) ? trim($p) : true; + if ($tp === '' || $tp === 'null') { + return null; + } elseif ($p == false || $tp == '0' || $tp === 'false' || $tp === 'no') { + return false; + } + return true; + } + return null; + } + + public static function paramBoolean(string $key): bool { + if (null === $value = self::paramTernary($key)) { + return false; + } + return $value; + } + + public static function paramInt(string $key): int { + if (!empty($_SESSION[$key])) { + return intval($_SESSION[$key]); + } + return 0; + } + + public static function paramString(string $key): string { + if (isset($_SESSION[$key])) { + $s = $_SESSION[$key]; + if (is_string($s)) { + return $s; + } + if (is_int($s) || is_bool($s)) { + return (string)$s; + } + } + return ''; + } /** * Allows you to create or update a session variable |
