diff options
| author | 2023-03-31 08:23:39 +0200 | |
|---|---|---|
| committer | 2023-03-31 08:23:39 +0200 | |
| commit | 288ed04ccc30b58373576dc3be811aee43e67034 (patch) | |
| tree | 27f4c571e04d64c97737416dfa2b8d65f481dfd8 /lib | |
| parent | c9d5fe2da12cbc3a071ebf9a518afe2789bb3d61 (diff) | |
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
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Minz/ActionException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/Configuration.php | 2 | ||||
| -rw-r--r-- | lib/Minz/ConfigurationException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/ControllerNotActionControllerException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/ControllerNotExistException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/CurrentPagePaginationException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/Error.php | 4 | ||||
| -rw-r--r-- | lib/Minz/Exception.php | 2 | ||||
| -rw-r--r-- | lib/Minz/ExtensionException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/FileNotExistException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/Helper.php | 2 | ||||
| -rw-r--r-- | lib/Minz/Mailer.php | 3 | ||||
| -rw-r--r-- | lib/Minz/ModelPdo.php | 1 | ||||
| -rw-r--r-- | lib/Minz/PDOConnectionException.php | 2 | ||||
| -rw-r--r-- | lib/Minz/Pdo.php | 25 | ||||
| -rw-r--r-- | lib/Minz/PdoMysql.php | 8 | ||||
| -rw-r--r-- | lib/Minz/PdoPgsql.php | 3 | ||||
| -rw-r--r-- | lib/Minz/PdoSqlite.php | 8 | ||||
| -rw-r--r-- | lib/Minz/PermissionDeniedException.php | 2 | ||||
| -rw-r--r-- | lib/favicons.php | 9 | ||||
| -rw-r--r-- | lib/lib_date.php | 12 | ||||
| -rw-r--r-- | lib/lib_install.php | 9 |
22 files changed, 69 insertions, 37 deletions
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 @@ <?php class Minz_ActionException extends Minz_Exception { - public function __construct ($controller_name, $action_name, $code = self::ERROR) { + 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. $action_name = rawurlencode($action_name); diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index 6d4aed0ab..a9a4ae03a 100644 --- a/lib/Minz/Configuration.php +++ b/lib/Minz/Configuration.php @@ -8,7 +8,7 @@ * @property-read string $environment * @property-read array<string> $extensions_enabled * @property-read string $mailer - * @property-read string $smtp + * @property-read array<string|int|bool> $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 @@ <?php class Minz_ConfigurationException extends Minz_Exception { - public function __construct($error, $code = self::ERROR) { + public function __construct(string $error, int $code = self::ERROR) { $message = 'Configuration error: ' . $error; parent::__construct($message, $code); } diff --git a/lib/Minz/ControllerNotActionControllerException.php b/lib/Minz/ControllerNotActionControllerException.php index 5cf418404..19e5df1b4 100644 --- a/lib/Minz/ControllerNotActionControllerException.php +++ b/lib/Minz/ControllerNotActionControllerException.php @@ -1,6 +1,6 @@ <?php class Minz_ControllerNotActionControllerException extends Minz_Exception { - public function __construct ($controller_name, $code = self::ERROR) { + public function __construct(string $controller_name, int $code = self::ERROR) { $message = 'Controller `' . $controller_name . '` isn’t instance of ActionController'; parent::__construct ($message, $code); diff --git a/lib/Minz/ControllerNotExistException.php b/lib/Minz/ControllerNotExistException.php index a024e1cbd..2d2178974 100644 --- a/lib/Minz/ControllerNotExistException.php +++ b/lib/Minz/ControllerNotExistException.php @@ -1,6 +1,6 @@ <?php class Minz_ControllerNotExistException extends Minz_Exception { - public function __construct ($code = self::ERROR) { + public function __construct(int $code = self::ERROR) { $message = 'Controller not found!'; parent::__construct ($message, $code); } diff --git a/lib/Minz/CurrentPagePaginationException.php b/lib/Minz/CurrentPagePaginationException.php index 3e3d9d1b4..973913afb 100644 --- a/lib/Minz/CurrentPagePaginationException.php +++ b/lib/Minz/CurrentPagePaginationException.php @@ -1,6 +1,6 @@ <?php class Minz_CurrentPagePaginationException extends Minz_Exception { - public function __construct ($page) { + public function __construct(int $page) { $message = 'Page number `' . $page . '` doesn\'t exist'; parent::__construct ($message, self::ERROR); diff --git a/lib/Minz/Error.php b/lib/Minz/Error.php index 3162c6f99..e23499a10 100644 --- a/lib/Minz/Error.php +++ b/lib/Minz/Error.php @@ -19,7 +19,7 @@ class Minz_Error { * > $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,string>|string $logs logs sorted by category (error, warning, notice) * @return array<string> 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 @@ <?php class Minz_ExtensionException extends Minz_Exception { - public function __construct ($message, $extension_name = false, $code = self::ERROR) { + public function __construct(string $message, ?string $extension_name = null, int $code = self::ERROR) { if ($extension_name) { $message = 'An error occurred in `' . $extension_name . '` extension with the message: ' . $message; } else { diff --git a/lib/Minz/FileNotExistException.php b/lib/Minz/FileNotExistException.php index f97f161af..b467a5208 100644 --- a/lib/Minz/FileNotExistException.php +++ b/lib/Minz/FileNotExistException.php @@ -1,6 +1,6 @@ <?php class Minz_FileNotExistException extends Minz_Exception { - public function __construct ($file_name, $code = self::ERROR) { + public function __construct(string $file_name, int $code = self::ERROR) { $message = 'File not found: `' . $file_name.'`'; parent::__construct ($message, $code); diff --git a/lib/Minz/Helper.php b/lib/Minz/Helper.php index 2c011e1bf..3e09c4758 100644 --- a/lib/Minz/Helper.php +++ b/lib/Minz/Helper.php @@ -12,6 +12,8 @@ class Minz_Helper { /** * Wrapper for htmlspecialchars. * Force UTf-8 value and can be used on array too. + * @param string|array<string> $var + * @return string|array<string> */ 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<string|int|bool> */ 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 @@ <?php class Minz_PDOConnectionException extends Minz_Exception { - public function __construct ($error, $user, $code = self::ERROR) { + public function __construct(string $error, string $user, int $code = self::ERROR) { $message = 'Access to database is denied for `' . $user . '`: ' . $error; parent::__construct ($message, $code); diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php index 8c9cd9076..2efff61d4 100644 --- a/lib/Minz/Pdo.php +++ b/lib/Minz/Pdo.php @@ -6,18 +6,20 @@ */ abstract class Minz_Pdo extends PDO { - public function __construct(string $dsn, $username = null, $passwd = null, $options = null) { + /** @param array<int,int|string>|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<int,string>|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<int,int|string>|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<int,int|string>|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<int,int|string>|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 @@ <?php class Minz_PermissionDeniedException extends Minz_Exception { - public function __construct ($file_name, $code = self::ERROR) { + public function __construct(string $file_name, int $code = self::ERROR) { $message = 'Permission is denied for `' . $file_name.'`'; parent::__construct ($message, $code); diff --git a/lib/favicons.php b/lib/favicons.php index fd03f61c8..abaa5e63a 100644 --- a/lib/favicons.php +++ b/lib/favicons.php @@ -2,7 +2,7 @@ const FAVICONS_DIR = DATA_PATH . '/favicons/'; const DEFAULT_FAVICON = PUBLIC_PATH . '/themes/icons/default_favicon.ico'; -function isImgMime($content) { +function isImgMime(string $content): bool { //Based on https://github.com/ArthurHoaro/favicon/blob/3a4f93da9bb24915b21771eb7873a21bde26f5d1/src/Favicon/Favicon.php#L311-L319 if ($content == '') { return false; @@ -21,7 +21,8 @@ function isImgMime($content) { return $isImage; } -function downloadHttp(&$url, $curlOptions = array()) { +/** @param array<int,int|bool> $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<string,string> */ +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'; |
