diff options
author | Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> | 2023-11-16 22:43:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-16 22:43:00 +0100 |
commit | 30c7a61a9b410f023c56ef19b9389a61647d8768 (patch) | |
tree | bb58408980ce5b86f1d2b4a9be29d55b2d46dbb1 /lib | |
parent | ee99e7e2cc228500efc1b539954c0ca6cd4c146d (diff) | |
download | freshrss-30c7a61a9b410f023c56ef19b9389a61647d8768.tar.gz freshrss-30c7a61a9b410f023c56ef19b9389a61647d8768.zip |
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')
44 files changed, 133 insertions, 13 deletions
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 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + class Minz_ActionException extends Minz_Exception { public function __construct(string $controller_name, string $action_name, int $code = self::ERROR) { // Just for security, as we are not supposed to get non-alphanumeric characters. diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index b667aa515..1aa4cae86 100644 --- a/lib/Minz/Configuration.php +++ b/lib/Minz/Configuration.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * Manage configuration for the application. diff --git a/lib/Minz/ConfigurationException.php b/lib/Minz/ConfigurationException.php index 498420a67..dbea6ea87 100644 --- a/lib/Minz/ConfigurationException.php +++ b/lib/Minz/ConfigurationException.php @@ -1,5 +1,5 @@ <?php - +declare(strict_types=1); class Minz_ConfigurationException extends Minz_Exception { public function __construct(string $error, int $code = self::ERROR) { $message = 'Configuration error: ' . $error; diff --git a/lib/Minz/ConfigurationNamespaceException.php b/lib/Minz/ConfigurationNamespaceException.php index f4278c5d6..febe456c4 100644 --- a/lib/Minz/ConfigurationNamespaceException.php +++ b/lib/Minz/ConfigurationNamespaceException.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); class Minz_ConfigurationNamespaceException extends Minz_ConfigurationException { } diff --git a/lib/Minz/ConfigurationParamException.php b/lib/Minz/ConfigurationParamException.php index eac977935..93789b4dc 100644 --- a/lib/Minz/ConfigurationParamException.php +++ b/lib/Minz/ConfigurationParamException.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); class Minz_ConfigurationParamException extends Minz_ConfigurationException { } diff --git a/lib/Minz/ConfigurationSetterInterface.php b/lib/Minz/ConfigurationSetterInterface.php index ac8c154ba..f141a1a6f 100644 --- a/lib/Minz/ConfigurationSetterInterface.php +++ b/lib/Minz/ConfigurationSetterInterface.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); interface Minz_ConfigurationSetterInterface { diff --git a/lib/Minz/ControllerNotActionControllerException.php b/lib/Minz/ControllerNotActionControllerException.php index 19e5df1b4..249ace2e1 100644 --- a/lib/Minz/ControllerNotActionControllerException.php +++ b/lib/Minz/ControllerNotActionControllerException.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + class Minz_ControllerNotActionControllerException extends Minz_Exception { public function __construct(string $controller_name, int $code = self::ERROR) { $message = 'Controller `' . $controller_name . '` isn’t instance of ActionController'; diff --git a/lib/Minz/ControllerNotExistException.php b/lib/Minz/ControllerNotExistException.php index 2d2178974..3ce181f09 100644 --- a/lib/Minz/ControllerNotExistException.php +++ b/lib/Minz/ControllerNotExistException.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + class Minz_ControllerNotExistException extends Minz_Exception { public function __construct(int $code = self::ERROR) { $message = 'Controller not found!'; diff --git a/lib/Minz/CurrentPagePaginationException.php b/lib/Minz/CurrentPagePaginationException.php index b3c8b2915..7d70c5af8 100644 --- a/lib/Minz/CurrentPagePaginationException.php +++ b/lib/Minz/CurrentPagePaginationException.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + class Minz_CurrentPagePaginationException extends Minz_Exception { public function __construct(int $page) { $message = 'Page number `' . $page . '` doesn’t exist'; diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php index da46ce6cd..d5a1d7eb2 100644 --- a/lib/Minz/Dispatcher.php +++ b/lib/Minz/Dispatcher.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + class Minz_Exception extends Exception { const ERROR = 0; const WARNING = 10; diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 8bc3ce964..7063f1c04 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * The extension base class. diff --git a/lib/Minz/ExtensionException.php b/lib/Minz/ExtensionException.php index 730730c1d..83d60062e 100644 --- a/lib/Minz/ExtensionException.php +++ b/lib/Minz/ExtensionException.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); class Minz_ExtensionException extends Minz_Exception { public function __construct(string $message, string $extension_name = '', int $code = self::ERROR) { diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php index fbb6d2a17..e1443b9c3 100644 --- a/lib/Minz/ExtensionManager.php +++ b/lib/Minz/ExtensionManager.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * An extension manager to load extensions present in CORE_EXTENSIONS_PATH and THIRDPARTY_EXTENSIONS_PATH. diff --git a/lib/Minz/FileNotExistException.php b/lib/Minz/FileNotExistException.php index b467a5208..24cb1997a 100644 --- a/lib/Minz/FileNotExistException.php +++ b/lib/Minz/FileNotExistException.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + class Minz_FileNotExistException extends Minz_Exception { public function __construct(string $file_name, int $code = self::ERROR) { $message = 'File not found: `' . $file_name.'`'; diff --git a/lib/Minz/FrontController.php b/lib/Minz/FrontController.php index d91fcfa68..09e986904 100644 --- a/lib/Minz/FrontController.php +++ b/lib/Minz/FrontController.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + # ***** BEGIN LICENSE BLOCK ***** # MINZ - a free PHP Framework like Zend Framework # Copyright (C) 2011 Marien Fressinaud diff --git a/lib/Minz/Helper.php b/lib/Minz/Helper.php index 8ecc1af8d..642b9304c 100644 --- a/lib/Minz/Helper.php +++ b/lib/Minz/Helper.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; diff --git a/lib/Minz/Migrator.php b/lib/Minz/Migrator.php index 91a4e6cb1..c1978dc69 100644 --- a/lib/Minz/Migrator.php +++ b/lib/Minz/Migrator.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * The Minz_Migrator helps to migrate data (in a database or not) or the diff --git a/lib/Minz/Model.php b/lib/Minz/Model.php index bf6da1599..ee65d7d9c 100644 --- a/lib/Minz/Model.php +++ b/lib/Minz/Model.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); /** * MINZ - Copyright 2011 Marien Fressinaud @@ -57,7 +58,7 @@ class Minz_ModelPdo { break; case 'sqlite': $dsn = 'sqlite:' . DATA_PATH . '/users/' . $this->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 @@ <?php +declare(strict_types=1); + class Minz_PDOConnectionException extends Minz_Exception { public function __construct(string $error, string $user, int $code = self::ERROR) { $message = 'Access to database is denied for `' . $user . '`: ' . $error; diff --git a/lib/Minz/Paginator.php b/lib/Minz/Paginator.php index 6b185c8b8..df145f4bf 100644 --- a/lib/Minz/Paginator.php +++ b/lib/Minz/Paginator.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); /** * MINZ - Copyright 2011 Marien Fressinaud diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php index df17a2f52..0171141ba 100644 --- a/lib/Minz/PdoMysql.php +++ b/lib/Minz/PdoMysql.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * MINZ - Copyright 2011 Marien Fressinaud diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php index e0efb8ad1..2abf168d9 100644 --- a/lib/Minz/PdoPgsql.php +++ b/lib/Minz/PdoPgsql.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * MINZ - Copyright 2011 Marien Fressinaud diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php index a66923ff2..1fd5c8d7a 100644 --- a/lib/Minz/PdoSqlite.php +++ b/lib/Minz/PdoSqlite.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * MINZ - Copyright 2011 Marien Fressinaud diff --git a/lib/Minz/PermissionDeniedException.php b/lib/Minz/PermissionDeniedException.php index 91a4b310e..240a8c3d4 100644 --- a/lib/Minz/PermissionDeniedException.php +++ b/lib/Minz/PermissionDeniedException.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + class Minz_PermissionDeniedException extends Minz_Exception { public function __construct(string $file_name, int $code = self::ERROR) { $message = 'Permission is denied for `' . $file_name.'`'; diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php index af2ba1243..0ac1a9fe3 100644 --- a/lib/Minz/Request.php +++ b/lib/Minz/Request.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> @@ -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<string,string>|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<string,array{time:int,notification:array{type:string,content:string}}> */ + $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 @@ <?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 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 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); /** * The Minz_Url class handles URLs across the MINZ framework diff --git a/lib/Minz/User.php b/lib/Minz/User.php index 252584e83..8e90a4fb2 100644 --- a/lib/Minz/User.php +++ b/lib/Minz/User.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * The Minz_User class handles the user information. @@ -13,7 +14,7 @@ final class Minz_User { * @return string the name of the current user, or null if there is none */ public static function name(): ?string { - $currentUser = trim(Minz_Session::param(Minz_User::CURRENT_USER, '')); + $currentUser = trim(Minz_Session::paramString(Minz_User::CURRENT_USER)); return $currentUser === '' ? null : $currentUser; } diff --git a/lib/Minz/View.php b/lib/Minz/View.php index 87b85e190..da6e55a23 100644 --- a/lib/Minz/View.php +++ b/lib/Minz/View.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * MINZ - Copyright 2011 Marien Fressinaud * Sous licence AGPL3 <http://www.gnu.org/licenses/> 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 @@ <?php +declare(strict_types=1); + const FAVICONS_DIR = DATA_PATH . '/favicons/'; const DEFAULT_FAVICON = PUBLIC_PATH . '/themes/icons/default_favicon.ico'; @@ -99,8 +101,12 @@ function searchFavicon(string &$url): string { } $href = SimplePie_IRI::absolutize($baseUrl, $href); + if ($href == false) { + return ''; + } - $favicon = downloadHttp($href, array(CURLOPT_REFERER => $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 @@ <?php +declare(strict_types=1); + /* Enable support for HTTP/1.x conditional requests in PHP. Goal: Optimisation diff --git a/lib/lib_date.php b/lib/lib_date.php index 70c1517af..ee8bf92f6 100644 --- a/lib/lib_date.php +++ b/lib/lib_date.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + /** * Author: Alexandre Alapetite https://alexandre.alapetite.fr * 2014-06-01 diff --git a/lib/lib_install.php b/lib/lib_install.php index 80af02d4f..720c4bf77 100644 --- a/lib/lib_install.php +++ b/lib/lib_install.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); FreshRSS_SystemConfiguration::register('default_system', join_path(FRESHRSS_PATH, 'config.default.php')); FreshRSS_UserConfiguration::register('default_user', join_path(FRESHRSS_PATH, 'config-user.default.php')); diff --git a/lib/lib_rss.php b/lib/lib_rss.php index 648f11a74..7a65a0433 100644 --- a/lib/lib_rss.php +++ b/lib/lib_rss.php @@ -1,4 +1,6 @@ <?php +declare(strict_types=1); + if (version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION, '<')) { die(sprintf('FreshRSS error: FreshRSS requires PHP %s+!', FRESHRSS_MIN_PHP_VERSION)); } |