From a81656c3ed5b8fe0f31794a4fbe0d1a907fca8e8 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Fri, 6 Sep 2024 09:06:46 +0200 Subject: Upgrade to PHP 8.1 (#6711) * Upgrade to PHP 8.1 As discussed in https://github.com/FreshRSS/FreshRSS/discussions/5474 https://www.php.net/releases/8.0/en.php https://www.php.net/releases/8.1/en.php Upgrade to available native type declarations https://php.net/language.types.declarations Upgrade to https://phpunit.de/announcements/phpunit-10.html which requires PHP 8.1+ (good timing, as version 9 was not maintained anymore) Upgrade `:oldest` Docker dev image to oldest Alpine version supporting PHP 8.1: Alpine 3.16, which includes PHP 8.1.22. * Include 6736 https://github.com/FreshRSS/FreshRSS/pull/6736 --- lib/Minz/Configuration.php | 12 ++++----- lib/Minz/ConfigurationSetterInterface.php | 2 +- lib/Minz/Extension.php | 16 +++--------- lib/Minz/ExtensionManager.php | 2 +- lib/Minz/FrontController.php | 3 +-- lib/Minz/Helper.php | 5 +--- lib/Minz/Log.php | 1 - lib/Minz/Mailer.php | 4 --- lib/Minz/Migrator.php | 5 +--- lib/Minz/ModelPdo.php | 2 -- lib/Minz/Paginator.php | 4 +-- lib/Minz/Pdo.php | 22 +++------------- lib/Minz/PdoMysql.php | 5 +--- lib/Minz/PdoSqlite.php | 5 +--- lib/Minz/Request.php | 4 +-- lib/Minz/Session.php | 2 +- lib/Minz/Translate.php | 4 +-- lib/Minz/Url.php | 11 ++------ lib/Minz/View.php | 7 +---- lib/lib_date.php | 2 +- lib/lib_rss.php | 43 +++---------------------------- 21 files changed, 34 insertions(+), 127 deletions(-) (limited to 'lib') diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index 7205f3009..89aea4fae 100644 --- a/lib/Minz/Configuration.php +++ b/lib/Minz/Configuration.php @@ -56,10 +56,9 @@ class Minz_Configuration { * Return the configuration related to a given namespace. * * @param string $namespace the name of the configuration to get. - * @return static object * @throws Minz_ConfigurationNamespaceException if the namespace does not exist. */ - public static function get(string $namespace) { + public static function get(string $namespace): static { if (!isset(self::$config_list[$namespace])) { throw new Minz_ConfigurationNamespaceException( $namespace . ' namespace does not exist' @@ -156,7 +155,7 @@ class Minz_Configuration { * @param mixed $default default value to return if key does not exist. * @return array|mixed value corresponding to the key. */ - public function param(string $key, $default = null) { + public function param(string $key, mixed $default = null): mixed { if (isset($this->data[$key])) { return $this->data[$key]; } elseif (!is_null($default)) { @@ -171,7 +170,7 @@ class Minz_Configuration { * A wrapper for param(). * @return array|mixed */ - public function __get(string $key) { + public function __get(string $key): mixed { return $this->param($key); } @@ -181,7 +180,7 @@ class Minz_Configuration { * @param string $key the param name to set. * @param mixed $value the value to set. If null, the key is removed from the configuration. */ - public function _param(string $key, $value = null): void { + public function _param(string $key, mixed $value = null): void { if ($this->configuration_setter !== null && $this->configuration_setter->support($key)) { $this->configuration_setter->handle($this->data, $key, $value); } elseif (isset($this->data[$key]) && is_null($value)) { @@ -193,9 +192,8 @@ class Minz_Configuration { /** * A wrapper for _param(). - * @param mixed $value */ - public function __set(string $key, $value): void { + public function __set(string $key, mixed $value): void { $this->_param($key, $value); } diff --git a/lib/Minz/ConfigurationSetterInterface.php b/lib/Minz/ConfigurationSetterInterface.php index f141a1a6f..451d4cb0e 100644 --- a/lib/Minz/ConfigurationSetterInterface.php +++ b/lib/Minz/ConfigurationSetterInterface.php @@ -16,5 +16,5 @@ interface Minz_ConfigurationSetterInterface { * @param string $key the key to update. * @param mixed $value the value to set. */ - public function handle(&$data, string $key, $value): void; + public function handle(array &$data, string $key, mixed $value): void; } diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 95d28af8a..69b9c569c 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -105,7 +105,7 @@ abstract class Minz_Extension { * * @return string|false html content from ext_dir/configure.phtml, false if it does not exist. */ - final public function getConfigureView() { + final public function getConfigureView(): string|false { $filename = $this->path . '/configure.phtml'; if (!file_exists($filename)) { return false; @@ -146,7 +146,7 @@ abstract class Minz_Extension { return $this->version; } /** @return 'system'|'user' */ - final public function getType() { + final public function getType(): string { return $this->type; } @@ -296,11 +296,7 @@ abstract class Minz_Extension { return []; } - /** - * @param mixed $default - * @return mixed - */ - final public function getSystemConfigurationValue(string $key, $default = null) { + final public function getSystemConfigurationValue(string $key, mixed $default = null): mixed { if (!is_array($this->system_configuration)) { $this->system_configuration = $this->getSystemConfiguration(); } @@ -311,11 +307,7 @@ abstract class Minz_Extension { return $default; } - /** - * @param mixed $default - * @return mixed - */ - final public function getUserConfigurationValue(string $key, $default = null) { + final public function getUserConfigurationValue(string $key, mixed $default = null): mixed { if (!is_array($this->user_configuration)) { $this->user_configuration = $this->getUserConfiguration(); } diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php index d9e38955b..2d7c92d6b 100644 --- a/lib/Minz/ExtensionManager.php +++ b/lib/Minz/ExtensionManager.php @@ -369,7 +369,7 @@ final class Minz_ExtensionManager { * @return mixed|null final chained result of the hooks. If nothing is changed, * the initial argument is returned. */ - private static function callOneToOne(string $hook_name, $arg) { + private static function callOneToOne(string $hook_name, mixed $arg): mixed { $result = $arg; foreach (self::$hook_list[$hook_name]['list'] as $function) { $result = call_user_func($function, $arg); diff --git a/lib/Minz/FrontController.php b/lib/Minz/FrontController.php index e57fb69c6..3a86d2d6d 100644 --- a/lib/Minz/FrontController.php +++ b/lib/Minz/FrontController.php @@ -79,9 +79,8 @@ class Minz_FrontController { /** * Kills the programme - * @return never */ - public static function killApp(string $txt = '') { + public static function killApp(string $txt = ''): never { header('HTTP/1.1 500 Internal Server Error', true, 500); if (function_exists('errorMessageInfo')) { //If the application has defined a custom error message function diff --git a/lib/Minz/Helper.php b/lib/Minz/Helper.php index 50243ded0..91c1bdc00 100644 --- a/lib/Minz/Helper.php +++ b/lib/Minz/Helper.php @@ -18,11 +18,8 @@ final class Minz_Helper { * @phpstan-template T of mixed * @phpstan-param T $var * @phpstan-return T - * - * @param mixed $var - * @return mixed */ - public static function htmlspecialchars_utf8($var) { + public static function htmlspecialchars_utf8(mixed $var): mixed { if (is_array($var)) { // @phpstan-ignore argument.type, return.type return array_map([self::class, 'htmlspecialchars_utf8'], $var); diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 648c2c663..8bf193ffe 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -76,7 +76,6 @@ class Minz_Log { * This method can be called multiple times for one script execution, but its result will not change unless * you call clearstatcache() in between. We won’t do do that for performance reasons. * - * @param string $file_name * @throws Minz_PermissionDeniedException */ protected static function ensureMaxLogSize(string $file_name): void { diff --git a/lib/Minz/Mailer.php b/lib/Minz/Mailer.php index 8e1211807..c657bd486 100644 --- a/lib/Minz/Mailer.php +++ b/lib/Minz/Mailer.php @@ -18,10 +18,6 @@ use PHPMailer\PHPMailer\Exception; * $this->view->_path('user_mailer/email_need_validation.txt.php') * ``` * - * Minz_Mailer uses the PHPMailer library under the hood. The latter requires - * PHP >= 5.5 to work. If you instantiate a Minz_Mailer with PHP < 5.5, a - * warning will be logged. - * * The email is sent by calling the `mail` method. */ class Minz_Mailer { diff --git a/lib/Minz/Migrator.php b/lib/Minz/Migrator.php index c1978dc69..39c834765 100644 --- a/lib/Minz/Migrator.php +++ b/lib/Minz/Migrator.php @@ -19,9 +19,6 @@ class Minz_Migrator /** * Execute a list of migrations, skipping versions indicated in a file * - * @param string $migrations_path - * @param string $applied_migrations_path - * * @return true|string Returns true if execute succeeds to apply * migrations, or a string if it fails. * @throws DomainException if there is no migrations corresponding to the @@ -31,7 +28,7 @@ class Minz_Migrator * * @throws BadFunctionCallException if a callback isn’t callable. */ - public static function execute(string $migrations_path, string $applied_migrations_path) { + public static function execute(string $migrations_path, string $applied_migrations_path): string|bool { $applied_migrations = @file_get_contents($applied_migrations_path); if ($applied_migrations === false) { return "Cannot open the {$applied_migrations_path} file"; diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index b39ae2f81..f27ae5dc7 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -86,8 +86,6 @@ class Minz_ModelPdo { /** * Create the connection to the database using the variables * HOST, BASE, USER and PASS variables defined in the configuration file - * @param string|null $currentUser - * @param Minz_Pdo|null $currentPdo * @throws Minz_ConfigurationException * @throws Minz_PDOConnectionException */ diff --git a/lib/Minz/Paginator.php b/lib/Minz/Paginator.php index 3b3c0961e..727fe42d3 100644 --- a/lib/Minz/Paginator.php +++ b/lib/Minz/Paginator.php @@ -64,7 +64,7 @@ class Minz_Paginator { * @param Minz_Model $item l'élément à retrouver * @return int|false la page à laquelle se trouve l’élément, false si non trouvé */ - public function pageByItem($item) { + public function pageByItem($item): int|false { $i = 0; do { @@ -82,7 +82,7 @@ class Minz_Paginator { * @param Minz_Model $item the element to search * @return int|false the position of the element, or false if not found */ - public function positionByItem($item) { + public function positionByItem($item): int|false { $i = 0; do { diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php index 33d84eb45..705c2d58c 100644 --- a/lib/Minz/Pdo.php +++ b/lib/Minz/Pdo.php @@ -37,58 +37,44 @@ abstract class Minz_Pdo extends PDO { return $this->autoPrefix($statement); } - // PHP8+: PDO::lastInsertId(?string $name = null): string|false /** - * @param string|null $name - * @return string|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` */ #[\Override] - #[\ReturnTypeWillChange] - public function lastInsertId($name = null) { + public function lastInsertId(?string $name = null): string|false { if ($name != null) { $name = $this->preSql($name); } return parent::lastInsertId($name); } - // PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false /** - * @param string $query * @param array $options - * @return PDOStatement|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` * @phpstan-ignore method.childParameterType, throws.unusedType */ #[\Override] - #[\ReturnTypeWillChange] - public function prepare($query, $options = []) { + public function prepare(string $query, array $options = []): PDOStatement|false { $query = $this->preSql($query); return parent::prepare($query, $options); } - // PHP8+: PDO::exec(string $statement): int|false /** - * @param string $statement - * @return int|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` * @phpstan-ignore throws.unusedType */ #[\Override] - #[\ReturnTypeWillChange] - public function exec($statement) { + public function exec(string $statement): int|false { $statement = $this->preSql($statement); return parent::exec($statement); } /** - * @return PDOStatement|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` * @phpstan-ignore throws.unusedType */ #[\Override] - #[\ReturnTypeWillChange] - public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args) { + public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args): PDOStatement|false { $query = $this->preSql($query); return $fetch_mode === null ? parent::query($query) : parent::query($query, $fetch_mode, ...$fetch_mode_args); } diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php index 3f7a804a3..d7fca0168 100644 --- a/lib/Minz/PdoMysql.php +++ b/lib/Minz/PdoMysql.php @@ -22,13 +22,10 @@ class Minz_PdoMysql extends Minz_Pdo { } /** - * @param string|null $name - * @return string|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` */ #[\Override] - #[\ReturnTypeWillChange] - public function lastInsertId($name = null) { + public function lastInsertId(?string $name = null): string|false { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL } } diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php index 537b6cdc6..6aa83690c 100644 --- a/lib/Minz/PdoSqlite.php +++ b/lib/Minz/PdoSqlite.php @@ -22,13 +22,10 @@ class Minz_PdoSqlite extends Minz_Pdo { } /** - * @param string|null $name - * @return string|false * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION` */ #[\Override] - #[\ReturnTypeWillChange] - public function lastInsertId($name = null) { + public function lastInsertId(?string $name = null): string|false { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL } } diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php index 1f15730fb..fcece464b 100644 --- a/lib/Minz/Request.php +++ b/lib/Minz/Request.php @@ -44,7 +44,7 @@ class Minz_Request { * @return mixed value of the parameter * @deprecated use typed versions instead */ - public static function param(string $key, $default = false, bool $specialchars = false) { + public static function param(string $key, mixed $default = false, bool $specialchars = false): mixed { if (isset(self::$params[$key])) { $p = self::$params[$key]; if (is_string($p) || is_array($p)) { @@ -156,7 +156,7 @@ class Minz_Request { } /** @return array{c?:string,a?:string,params?:array} */ - public static function originalRequest() { + public static function originalRequest(): array { return self::$originalRequest; } diff --git a/lib/Minz/Session.php b/lib/Minz/Session.php index 99b7fef45..c08ad688b 100644 --- a/lib/Minz/Session.php +++ b/lib/Minz/Session.php @@ -63,7 +63,7 @@ class Minz_Session { * @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) { + public static function param(string $p, $default = false): mixed { return $_SESSION[$p] ?? $default; } diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index 183fa48ca..a8dc889ee 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -255,9 +255,7 @@ class Minz_Translate { /** * Alias for Minz_Translate::t() - * @param string $key - * @param bool|float|int|string ...$args */ -function _t(string $key, ...$args): string { +function _t(string $key, bool|float|int|string ...$args): string { return Minz_Translate::t($key, ...$args); } diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php index 310067382..3948414d8 100644 --- a/lib/Minz/Url.php +++ b/lib/Minz/Url.php @@ -13,11 +13,10 @@ class Minz_Url { * $url['params'] = array of additional parameters * or as a string * @param string $encoding how to encode & (& ou & pour html) - * @param bool|string $absolute * @return string Formatted URL * @throws Minz_ConfigurationException */ - public static function display($url = [], string $encoding = 'html', $absolute = false): string { + public static function display($url = [], string $encoding = 'html', bool|string $absolute = false): string { $isArray = is_array($url); if ($isArray) { @@ -160,13 +159,7 @@ class Minz_Url { } } -/** - * @param string $controller - * @param string $action - * @param string|int ...$args - * @return string|false - */ -function _url(string $controller, string $action, ...$args) { +function _url(string $controller, string $action, int|string ...$args): string|false { $nb_args = count($args); if ($nb_args % 2 !== 0) { diff --git a/lib/Minz/View.php b/lib/Minz/View.php index 44b7378b6..f7dceef0a 100644 --- a/lib/Minz/View.php +++ b/lib/Minz/View.php @@ -232,8 +232,6 @@ class Minz_View { /** * Append a `` element referencing stylesheet. - * @param string $url - * @param string $media * @param bool $cond Conditional comment for IE, now deprecated and ignored @deprecated */ public static function appendStyle(string $url, string $media = 'all', bool $cond = false): void { @@ -298,7 +296,6 @@ class Minz_View { } /** * Prepend a `