From 288ed04ccc30b58373576dc3be811aee43e67034 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Fri, 31 Mar 2023 08:23:39 +0200 Subject: PHPStan level 6 for all PDO and Exception classes (#5239) * PHPStan level 6 for all PDO and Exception classes Contributes to https://github.com/FreshRSS/FreshRSS/issues/4112 * Fix type * Now also our remaining own librairies * Motivation for a few more files * A few more DAO classes * Last interface --- lib/Minz/ActionException.php | 2 +- lib/Minz/Configuration.php | 2 +- lib/Minz/ConfigurationException.php | 2 +- .../ControllerNotActionControllerException.php | 2 +- lib/Minz/ControllerNotExistException.php | 2 +- lib/Minz/CurrentPagePaginationException.php | 2 +- lib/Minz/Error.php | 4 ++-- lib/Minz/Exception.php | 2 +- lib/Minz/ExtensionException.php | 2 +- lib/Minz/FileNotExistException.php | 2 +- lib/Minz/Helper.php | 2 ++ lib/Minz/Mailer.php | 3 +++ lib/Minz/ModelPdo.php | 1 + lib/Minz/PDOConnectionException.php | 2 +- lib/Minz/Pdo.php | 25 +++++++++++++++++----- lib/Minz/PdoMysql.php | 8 +++++-- lib/Minz/PdoPgsql.php | 3 ++- lib/Minz/PdoSqlite.php | 8 +++++-- lib/Minz/PermissionDeniedException.php | 2 +- lib/favicons.php | 9 ++++---- lib/lib_date.php | 12 +++++------ lib/lib_install.php | 9 ++++---- 22 files changed, 69 insertions(+), 37 deletions(-) (limited to 'lib') diff --git a/lib/Minz/ActionException.php b/lib/Minz/ActionException.php index 53f0aab04..3c08b4892 100644 --- a/lib/Minz/ActionException.php +++ b/lib/Minz/ActionException.php @@ -1,6 +1,6 @@ $extensions_enabled * @property-read string $mailer - * @property-read string $smtp + * @property-read array $smtp * @property string $title */ class Minz_Configuration { diff --git a/lib/Minz/ConfigurationException.php b/lib/Minz/ConfigurationException.php index f294c3341..498420a67 100644 --- a/lib/Minz/ConfigurationException.php +++ b/lib/Minz/ConfigurationException.php @@ -1,7 +1,7 @@ $logs['notice'] * @param bool $redirect indique s'il faut forcer la redirection (les logs ne seront pas transmis) */ - public static function error ($code = 404, $logs = array (), $redirect = true) { + public static function error(int $code = 404, array $logs = [], bool $redirect = true): void { $logs = self::processLogs ($logs); $error_filename = APP_PATH . '/Controllers/errorController.php'; @@ -52,7 +52,7 @@ class Minz_Error { * @param array|string $logs logs sorted by category (error, warning, notice) * @return array list of matching logs, without the category, according to environment preferences (production / development) */ - private static function processLogs ($logs) { + private static function processLogs($logs) { $conf = Minz_Configuration::get('system'); $env = $conf->environment; $logs_ok = array (); diff --git a/lib/Minz/Exception.php b/lib/Minz/Exception.php index b5e71e0d8..f2d3b876b 100644 --- a/lib/Minz/Exception.php +++ b/lib/Minz/Exception.php @@ -4,7 +4,7 @@ class Minz_Exception extends Exception { const WARNING = 10; const NOTICE = 20; - public function __construct ($message, $code = self::ERROR) { + public function __construct(string $message, int $code = self::ERROR) { if ($code != Minz_Exception::ERROR && $code != Minz_Exception::WARNING && $code != Minz_Exception::NOTICE) { diff --git a/lib/Minz/ExtensionException.php b/lib/Minz/ExtensionException.php index b86a97798..c5bba60a9 100644 --- a/lib/Minz/ExtensionException.php +++ b/lib/Minz/ExtensionException.php @@ -1,7 +1,7 @@ $var + * @return string|array */ public static function htmlspecialchars_utf8($var) { if (is_array($var)) { diff --git a/lib/Minz/Mailer.php b/lib/Minz/Mailer.php index 92e396f99..ba434ec8f 100644 --- a/lib/Minz/Mailer.php +++ b/lib/Minz/Mailer.php @@ -32,8 +32,11 @@ class Minz_Mailer { */ protected $view; + /** @var string */ private $mailer; + /** @var array */ private $smtp_config; + /** @var int */ private $debug_level; /** diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index b8c1ad89b..0e813ddca 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -12,6 +12,7 @@ class Minz_ModelPdo { /** * Shares the connection to the database between all instances. + * @var bool */ public static $usesSharedPdo = true; diff --git a/lib/Minz/PDOConnectionException.php b/lib/Minz/PDOConnectionException.php index 11e4d1029..2b015607e 100644 --- a/lib/Minz/PDOConnectionException.php +++ b/lib/Minz/PDOConnectionException.php @@ -1,6 +1,6 @@ |null $options */ + public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) { parent::__construct($dsn, $username, $passwd, $options); $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } - abstract public function dbType(); + abstract public function dbType(): string; + /** @var string */ private $prefix = ''; public function prefix(): string { return $this->prefix; } - public function setPrefix(string $prefix) { + public function setPrefix(string $prefix): void { $this->prefix = $prefix; } @@ -33,6 +35,10 @@ abstract class Minz_Pdo extends PDO { } // PHP8+: PDO::lastInsertId(?string $name = null): string|false + /** + * @param string|null $name + * @return string|false + */ #[\ReturnTypeWillChange] public function lastInsertId($name = null) { if ($name != null) { @@ -42,6 +48,11 @@ abstract class Minz_Pdo extends PDO { } // PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false + /** + * @param string $statement + * @param array|null $driver_options + * @return PDOStatement|false + */ #[\ReturnTypeWillChange] public function prepare($statement, $driver_options = []) { $statement = $this->preSql($statement); @@ -49,15 +60,19 @@ abstract class Minz_Pdo extends PDO { } // PHP8+: PDO::exec(string $statement): int|false + /** + * @param string $statement + * @return int|false + */ #[\ReturnTypeWillChange] public function exec($statement) { $statement = $this->preSql($statement); return parent::exec($statement); } - // PHP8+: PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs): PDOStatement|false + /** @return PDOStatement|false */ #[\ReturnTypeWillChange] - public function query($query, $fetch_mode = null, ...$fetch_mode_args) { + public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args) { $query = $this->preSql($query); return $fetch_mode ? parent::query($query, $fetch_mode, ...$fetch_mode_args) : parent::query($query); } diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php index b66cccf28..ee411d949 100644 --- a/lib/Minz/PdoMysql.php +++ b/lib/Minz/PdoMysql.php @@ -6,7 +6,8 @@ */ class Minz_PdoMysql extends Minz_Pdo { - public function __construct(string $dsn, $username = null, $passwd = null, $options = null) { + /** @param array|null $options */ + public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) { parent::__construct($dsn, $username, $passwd, $options); $this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); } @@ -15,7 +16,10 @@ class Minz_PdoMysql extends Minz_Pdo { return 'mysql'; } - // PHP8+: PDO::lastInsertId(?string $name = null): string|false + /** + * @param string|null $name + * @return string|false + */ #[\ReturnTypeWillChange] public function lastInsertId($name = null) { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php index cae0fe476..b239d3544 100644 --- a/lib/Minz/PdoPgsql.php +++ b/lib/Minz/PdoPgsql.php @@ -6,7 +6,8 @@ */ class Minz_PdoPgsql extends Minz_Pdo { - public function __construct(string $dsn, $username = null, $passwd = null, $options = null) { + /** @param array|null $options */ + public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) { parent::__construct($dsn, $username, $passwd, $options); $this->exec("SET NAMES 'UTF8';"); } diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php index 2c90413a1..479879ffe 100644 --- a/lib/Minz/PdoSqlite.php +++ b/lib/Minz/PdoSqlite.php @@ -6,7 +6,8 @@ */ class Minz_PdoSqlite extends Minz_Pdo { - public function __construct(string $dsn, $username = null, $passwd = null, $options = null) { + /** @param array|null $options */ + public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) { parent::__construct($dsn, $username, $passwd, $options); $this->exec('PRAGMA foreign_keys = ON;'); } @@ -15,7 +16,10 @@ class Minz_PdoSqlite extends Minz_Pdo { return 'sqlite'; } - // PHP8+: PDO::lastInsertId(?string $name = null): string|false + /** + * @param string|null $name + * @return string|false + */ #[\ReturnTypeWillChange] public function lastInsertId($name = null) { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL diff --git a/lib/Minz/PermissionDeniedException.php b/lib/Minz/PermissionDeniedException.php index 61be530d3..91a4b310e 100644 --- a/lib/Minz/PermissionDeniedException.php +++ b/lib/Minz/PermissionDeniedException.php @@ -1,6 +1,6 @@ $curlOptions */ +function downloadHttp(string &$url, array $curlOptions = []): string { syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url); $url = checkUrl($url); if (!$url) { @@ -49,7 +50,7 @@ function downloadHttp(&$url, $curlOptions = array()) { return $info['http_code'] == 200 ? $response : ''; } -function searchFavicon(&$url) { +function searchFavicon(string &$url): string { $dom = new DOMDocument(); $html = downloadHttp($url); if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) { @@ -84,7 +85,7 @@ function searchFavicon(&$url) { return ''; } -function download_favicon($url, $dest) { +function download_favicon(string $url, string $dest): bool { $url = trim($url); $favicon = searchFavicon($url); if ($favicon == '') { diff --git a/lib/lib_date.php b/lib/lib_date.php index cb1f1d1e2..00356927f 100644 --- a/lib/lib_date.php +++ b/lib/lib_date.php @@ -42,13 +42,13 @@ function example($dateInterval) { } */ -function _dateFloor($isoDate) { +function _dateFloor(string $isoDate): string { $x = explode('T', $isoDate, 2); $t = isset($x[1]) ? str_pad($x[1], 6, '0') : '000000'; return str_pad($x[0], 8, '01') . 'T' . $t; } -function _dateCeiling($isoDate) { +function _dateCeiling(string $isoDate): string { $x = explode('T', $isoDate, 2); $t = isset($x[1]) && strlen($x[1]) > 1 ? str_pad($x[1], 6, '59') : '235959'; switch (strlen($x[0])) { @@ -62,11 +62,11 @@ function _dateCeiling($isoDate) { } } -function _noDelimit($isoDate) { +function _noDelimit(?string $isoDate): ?string { return $isoDate === null || $isoDate === '' ? null : str_replace(array('-', ':'), '', $isoDate); //FIXME: Bug with negative time zone } -function _dateRelative($d1, $d2) { +function _dateRelative(?string $d1, ?string $d2): ?string { if ($d2 === null) { return $d1 !== null && $d1[0] !== 'P' ? $d1 : null; } elseif ($d2 !== '' && $d2[0] != 'P' && $d1 !== null && $d1[0] !== 'P') { @@ -81,10 +81,10 @@ function _dateRelative($d1, $d2) { /** * Parameter $dateInterval is a string containing an ISO 8601 time interval. - * Returns an array with the minimum and maximum Unix timestamp of this interval, + * @return array{int|null|false,int|null|false} an array with the minimum and maximum Unix timestamp of this interval, * or null if open interval, or false if error. */ -function parseDateInterval($dateInterval) { +function parseDateInterval(string $dateInterval) { $dateInterval = trim($dateInterval); $dateInterval = str_replace('--', '/', $dateInterval); $dateInterval = strtoupper($dateInterval); diff --git a/lib/lib_install.php b/lib/lib_install.php index 931de21a2..18f4a732a 100644 --- a/lib/lib_install.php +++ b/lib/lib_install.php @@ -3,7 +3,8 @@ Minz_Configuration::register('default_system', join_path(FRESHRSS_PATH, 'config.default.php')); Minz_Configuration::register('default_user', join_path(FRESHRSS_PATH, 'config-user.default.php')); -function checkRequirements($dbType = '') { +/** @return array */ +function checkRequirements(string $dbType = ''): array { $php = version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION) >= 0; $curl = extension_loaded('curl'); $pdo_mysql = extension_loaded('pdo_mysql'); @@ -74,11 +75,11 @@ function checkRequirements($dbType = '') { ); } -function generateSalt() { +function generateSalt(): string { return sha1(uniqid('' . mt_rand(), true).implode('', stat(__FILE__))); } -function initDb() { +function initDb(): string { $conf = FreshRSS_Context::$system_conf; $db = $conf->db; if (empty($db['pdo_options'])) { @@ -115,7 +116,7 @@ function initDb() { return $databaseDAO->testConnection(); } -function setupMigrations() { +function setupMigrations(): bool { $migrations_path = APP_PATH . '/migrations'; $migrations_version_path = DATA_PATH . '/applied_migrations.txt'; -- cgit v1.2.3