From 30c7a61a9b410f023c56ef19b9389a61647d8768 Mon Sep 17 00:00:00 2001 From: Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> Date: Thu, 16 Nov 2023 22:43:00 +0100 Subject: 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 Co-authored-by: Alexandre Alapetite --- lib/Minz/ActionController.php | 2 + lib/Minz/ActionException.php | 2 + lib/Minz/Configuration.php | 1 + lib/Minz/ConfigurationException.php | 2 +- lib/Minz/ConfigurationNamespaceException.php | 1 + lib/Minz/ConfigurationParamException.php | 1 + lib/Minz/ConfigurationSetterInterface.php | 1 + .../ControllerNotActionControllerException.php | 2 + lib/Minz/ControllerNotExistException.php | 2 + lib/Minz/CurrentPagePaginationException.php | 2 + lib/Minz/Dispatcher.php | 2 + lib/Minz/Error.php | 2 + lib/Minz/Exception.php | 2 + lib/Minz/Extension.php | 1 + lib/Minz/ExtensionException.php | 1 + lib/Minz/ExtensionManager.php | 1 + lib/Minz/FileNotExistException.php | 2 + lib/Minz/FrontController.php | 2 + lib/Minz/Helper.php | 2 + lib/Minz/Log.php | 2 + lib/Minz/Mailer.php | 1 + lib/Minz/Migrator.php | 1 + lib/Minz/Model.php | 2 + lib/Minz/ModelArray.php | 2 + lib/Minz/ModelPdo.php | 3 +- lib/Minz/PDOConnectionException.php | 2 + lib/Minz/Paginator.php | 2 + lib/Minz/Pdo.php | 1 + lib/Minz/PdoMysql.php | 1 + lib/Minz/PdoPgsql.php | 1 + lib/Minz/PdoSqlite.php | 1 + lib/Minz/PermissionDeniedException.php | 2 + lib/Minz/Request.php | 13 +++--- lib/Minz/Session.php | 52 +++++++++++++++++++++- lib/Minz/Translate.php | 2 + lib/Minz/Url.php | 1 + lib/Minz/User.php | 3 +- lib/Minz/View.php | 2 + lib/SimplePie/SimplePie/IRI.php | 6 +-- lib/favicons.php | 8 +++- lib/http-conditional.php | 2 + lib/lib_date.php | 2 + lib/lib_install.php | 1 + lib/lib_rss.php | 2 + 44 files changed, 133 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/Minz/ActionController.php b/lib/Minz/ActionController.php index 68ef0695c..65771aa62 100644 --- a/lib/Minz/ActionController.php +++ b/lib/Minz/ActionController.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/ActionException.php b/lib/Minz/ActionException.php index 3c08b4892..dafc47a15 100644 --- a/lib/Minz/ActionException.php +++ b/lib/Minz/ActionException.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Error.php b/lib/Minz/Error.php index b6eac5174..e5a2cc794 100644 --- a/lib/Minz/Error.php +++ b/lib/Minz/Error.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Exception.php b/lib/Minz/Exception.php index f2d3b876b..c306a14ca 100644 --- a/lib/Minz/Exception.php +++ b/lib/Minz/Exception.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 60aba319d..272f62f6b 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Mailer.php b/lib/Minz/Mailer.php index efdc9ea03..bd5e97ceb 100644 --- a/lib/Minz/Mailer.php +++ b/lib/Minz/Mailer.php @@ -1,4 +1,5 @@ diff --git a/lib/Minz/ModelArray.php b/lib/Minz/ModelArray.php index 1b17c1da6..090536623 100644 --- a/lib/Minz/ModelArray.php +++ b/lib/Minz/ModelArray.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index 1e67fef9f..c78f44f2e 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -1,4 +1,5 @@ current_user . '/db.sqlite'; - $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); + $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, null, null, $driver_options); $this->pdo->setPrefix(''); break; case 'pgsql': diff --git a/lib/Minz/PDOConnectionException.php b/lib/Minz/PDOConnectionException.php index 2b015607e..391db5cae 100644 --- a/lib/Minz/PDOConnectionException.php +++ b/lib/Minz/PDOConnectionException.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php index 477ce7c1f..3d613e832 100644 --- a/lib/Minz/Pdo.php +++ b/lib/Minz/Pdo.php @@ -1,4 +1,5 @@ @@ -333,7 +335,7 @@ class Minz_Request { private static function setNotification(string $type, string $content): void { Minz_Session::lock(); - $requests = Minz_Session::param('requests', []); + $requests = Minz_Session::paramArray('requests'); $requests[self::requestId()] = [ 'time' => time(), 'notification' => [ 'type' => $type, 'content' => $content ], @@ -350,14 +352,15 @@ class Minz_Request { self::setNotification('bad', $content); } - /** @return array|null */ + /** @return array{type:string,content:string}|null */ public static function getNotification(): ?array { $notif = null; Minz_Session::lock(); - $requests = Minz_Session::param('requests'); - if (is_array($requests)) { + /** @var array */ + $requests = Minz_Session::paramArray('requests'); + if (!empty($requests)) { //Delete abandoned notifications - $requests = array_filter($requests, static function (array $r) { return isset($r['time']) && $r['time'] > time() - 3600; }); + $requests = array_filter($requests, static function (array $r) { return $r['time'] > time() - 3600; }); $requestId = self::requestId(); if (!empty($requests[$requestId]['notification'])) { 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 @@ > */ + 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 diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index dfeee2ff9..8f2e2527a 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -1,4 +1,6 @@ diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php index f6fa809cf..0c17e3ec7 100644 --- a/lib/Minz/Url.php +++ b/lib/Minz/Url.php @@ -1,4 +1,5 @@ diff --git a/lib/SimplePie/SimplePie/IRI.php b/lib/SimplePie/SimplePie/IRI.php index a02de682c..36c051bff 100644 --- a/lib/SimplePie/SimplePie/IRI.php +++ b/lib/SimplePie/SimplePie/IRI.php @@ -263,9 +263,9 @@ class SimplePie_IRI * * Returns false if $base is not absolute, otherwise an IRI. * - * @param IRI|string $base (Absolute) Base IRI - * @param IRI|string $relative Relative IRI - * @return IRI|false + * @param SimplePie_IRI|string $base (Absolute) Base IRI + * @param SimplePie_IRI|string $relative Relative IRI + * @return SimplePie_IRI|false */ public static function absolutize($base, $relative) { diff --git a/lib/favicons.php b/lib/favicons.php index b161215b2..5cf4295f5 100644 --- a/lib/favicons.php +++ b/lib/favicons.php @@ -1,4 +1,6 @@ $url)); + $iri = $href->get_iri(); + $favicon = downloadHttp($iri, array(CURLOPT_REFERER => $url)); if (isImgMime($favicon)) { return $favicon; } diff --git a/lib/http-conditional.php b/lib/http-conditional.php index 21382b735..2ed597a71 100644 --- a/lib/http-conditional.php +++ b/lib/http-conditional.php @@ -1,4 +1,6 @@