From 743ca371bbd8c412c2cd4ded6a5a44544abc5a65 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 8 Apr 2023 17:37:42 +0200 Subject: PHPStan Level 6 for more files (#5275) Contributes to https://github.com/FreshRSS/FreshRSS/issues/4112 --- app/Models/FilterAction.php | 20 +++++++++++++------- app/Models/Themes.php | 27 ++++++++++++++++++++------- app/Models/View.php | 2 +- lib/Minz/Log.php | 12 ++++++------ lib/Minz/Translate.php | 22 +++++++++++++--------- tests/phpstan-next.txt | 4 ---- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/app/Models/FilterAction.php b/app/Models/FilterAction.php index 394b573a4..44ff5674a 100644 --- a/app/Models/FilterAction.php +++ b/app/Models/FilterAction.php @@ -4,22 +4,26 @@ class FreshRSS_FilterAction { /** @var FreshRSS_BooleanSearch */ private $booleanSearch = null; + /** @var array|null */ private $actions = null; - private function __construct($booleanSearch, $actions) { + /** @param array $actions */ + private function __construct(FreshRSS_BooleanSearch $booleanSearch, array $actions) { $this->booleanSearch = $booleanSearch; $this->_actions($actions); } - public function booleanSearch() { + public function booleanSearch(): FreshRSS_BooleanSearch { return $this->booleanSearch; } - public function actions() { - return $this->actions; + /** @return array */ + public function actions(): array { + return $this->actions ?? []; } - public function _actions($actions) { + /** @param array $actions */ + public function _actions(?array $actions): void { if (is_array($actions)) { $this->actions = array_unique($actions); } else { @@ -27,7 +31,8 @@ class FreshRSS_FilterAction { } } - public function toJSON() { + /** @return array{'search'?:string,'actions'?:array} */ + public function toJSON(): array { if (is_array($this->actions) && $this->booleanSearch != null) { return array( 'search' => $this->booleanSearch->getRawInput(), @@ -37,7 +42,8 @@ class FreshRSS_FilterAction { return []; } - public static function fromJSON($json) { + /** @param array|mixed|null $json */ + public static function fromJSON($json): ?FreshRSS_FilterAction { if (!empty($json['search']) && !empty($json['actions']) && is_array($json['actions'])) { return new FreshRSS_FilterAction(new FreshRSS_BooleanSearch($json['search']), $json['actions']); } diff --git a/app/Models/Themes.php b/app/Models/Themes.php index 86125c5f5..ce63b24b2 100644 --- a/app/Models/Themes.php +++ b/app/Models/Themes.php @@ -1,18 +1,23 @@ */ + public static function getList(): array { return array_values(array_diff( scandir(PUBLIC_PATH . self::$themesUrl), array('..', '.') )); } - public static function get() { + /** @return array,'theme-color'?:string|array{'dark'?:string,'light'?:string,'default'?:string}}> */ + public static function get(): array { $themes_list = self::getList(); $list = array(); foreach ($themes_list as $theme_dir) { @@ -24,7 +29,10 @@ class FreshRSS_Themes extends Minz_Model { return $list; } - public static function get_infos($theme_id) { + /** + * @return false|array{'id':string,'name':string,'author':string,'description':string,'version':float|string,'files':array,'theme-color'?:string|array{'dark'?:string,'light'?:string,'default'?:string}} + */ + public static function get_infos(string $theme_id) { $theme_dir = PUBLIC_PATH . self::$themesUrl . $theme_id; if (is_dir($theme_dir)) { $json_filename = $theme_dir . '/metadata.json'; @@ -43,10 +51,15 @@ class FreshRSS_Themes extends Minz_Model { return false; } + /** @var string */ private static $themeIconsUrl; + /** @var array */ private static $themeIcons; - public static function load($theme_id) { + /** + * @return false|array{'id':string,'name':string,'author':string,'description':string,'version':float|string,'files':array,'theme-color'?:string|array{'dark'?:string,'light'?:string,'default'?:string}} + */ + public static function load(string $theme_id) { $infos = self::get_infos($theme_id); if (!$infos) { if ($theme_id !== self::$defaultTheme) { //Fall-back to default theme @@ -68,14 +81,14 @@ class FreshRSS_Themes extends Minz_Model { return $infos; } - public static function title($name) { + public static function title(string $name): string { static $titles = [ 'opml-dyn' => 'sub.category.dynamic_opml', ]; return $titles[$name] ?? ''; } - public static function alt($name) { + public static function alt(string $name): string { static $alts = array( 'add' => '➕', //✚ 'all' => '☰', @@ -119,7 +132,7 @@ class FreshRSS_Themes extends Minz_Model { 'view-reader' => '📜', 'warning' => '⚠️', //△ ); - return isset($name) ? $alts[$name] : ''; + return $alts[$name] ?? ''; } // TODO: Change for enum in PHP 8.1+ diff --git a/app/Models/View.php b/app/Models/View.php index 28e6dbb35..e908bdfad 100644 --- a/app/Models/View.php +++ b/app/Models/View.php @@ -81,7 +81,7 @@ class FreshRSS_View extends Minz_View { public $size_user; // Display - /** @var array */ + /** @var array,'theme-color'?:string|array{'dark'?:string,'light'?:string,'default'?:string}}> */ public $themes; // Shortcuts diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 117f231ac..20e8578be 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -19,7 +19,7 @@ class Minz_Log { * @param string $file_name fichier de log * @throws Minz_PermissionDeniedException */ - public static function record ($information, $level, $file_name = null) { + public static function record(string $information, int $level, ?string $file_name = null): void { $env = getenv('FRESHRSS_ENV'); if ($env == '') { try { @@ -78,7 +78,7 @@ class Minz_Log { * @param string $file_name * @throws Minz_PermissionDeniedException */ - protected static function ensureMaxLogSize($file_name) { + protected static function ensureMaxLogSize(string $file_name): void { $maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576; // @phpstan-ignore-next-line if ($maxSize > 0 && @filesize($file_name) > $maxSize) { @@ -106,16 +106,16 @@ class Minz_Log { * Some helpers to Minz_Log::record() method * Parameters are the same of those of the record() method. */ - public static function debug($msg, $file_name = null) { + public static function debug(string $msg, ?string $file_name = null): void { self::record($msg, LOG_DEBUG, $file_name); } - public static function notice($msg, $file_name = null) { + public static function notice(string $msg, ?string $file_name = null): void { self::record($msg, LOG_NOTICE, $file_name); } - public static function warning($msg, $file_name = null) { + public static function warning(string $msg, ?string $file_name = null): void { self::record($msg, LOG_WARNING, $file_name); } - public static function error($msg, $file_name = null) { + public static function error(string $msg, ?string $file_name = null): void { self::record($msg, LOG_ERR, $file_name); } } diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index 07d48ec08..e4c1020d5 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -11,21 +11,25 @@ class Minz_Translate { /** * $path_list is the list of registered base path to search translations. + * @var array */ private static $path_list = array(); /** * $lang_name is the name of the current language to use. + * @var string */ private static $lang_name; /** * $lang_files is a list of registered i18n files. + * @var array> */ private static $lang_files = array(); /** * $translates is a cache for i18n translation. + * @var array */ private static $translates = array(); @@ -33,7 +37,7 @@ class Minz_Translate { * Init the translation object. * @param string $lang_name the lang to show. */ - public static function init($lang_name = null) { + public static function init(?string $lang_name = null): void { self::$lang_name = $lang_name; self::$lang_files = array(); self::$translates = array(); @@ -47,7 +51,7 @@ class Minz_Translate { * Reset the translation object with a new language. * @param string $lang_name the new language to use */ - public static function reset($lang_name) { + public static function reset(string $lang_name): void { self::$lang_name = $lang_name; self::$lang_files = array(); self::$translates = array(); @@ -60,7 +64,7 @@ class Minz_Translate { * Return the list of available languages. * @return array containing langs found in different registered paths. */ - public static function availableLanguages() { + public static function availableLanguages(): array { $list_langs = array(); self::registerPath(APP_PATH . '/i18n'); @@ -110,7 +114,7 @@ class Minz_Translate { * Register a new path. * @param string $path a path containing i18n directories (e.g. ./en/, ./fr/). */ - public static function registerPath($path) { + public static function registerPath(string $path): void { if (!in_array($path, self::$path_list) && is_dir($path)) { self::$path_list[] = $path; self::loadLang($path); @@ -121,7 +125,7 @@ class Minz_Translate { * Load translations of the current language from the given path. * @param string $path the path containing i18n directories. */ - private static function loadLang($path) { + private static function loadLang(string $path): void { $lang_path = $path . '/' . self::$lang_name; if (!file_exists($lang_path) || self::$lang_name == '') { // The lang path does not exist, nothing more to do. @@ -150,7 +154,7 @@ class Minz_Translate { * Load the files associated to $key into $translates. * @param string $key the top level i18n key we want to load. */ - private static function loadKey($key) { + private static function loadKey(string $key): bool { // The top level key is not in $lang_files, it means it does not exist! if (!isset(self::$lang_files[$key])) { Minz_Log::debug($key . ' is not a valid top level key'); @@ -183,7 +187,7 @@ class Minz_Translate { * @return string value corresponding to the key. * If no value is found, return the key itself. */ - public static function t($key, ...$args) { + public static function t(string $key, ...$args): string { $group = explode('.', $key); if (count($group) < 2) { @@ -238,7 +242,7 @@ class Minz_Translate { /** * Return the current language. */ - public static function language() { + public static function language(): string { return self::$lang_name; } } @@ -249,6 +253,6 @@ class Minz_Translate { * @param string $key * @param mixed ...$args */ -function _t($key, ...$args) { +function _t(string $key, ...$args): string { return Minz_Translate::t($key, ...$args); } diff --git a/tests/phpstan-next.txt b/tests/phpstan-next.txt index a44d490d7..9c2508c2a 100644 --- a/tests/phpstan-next.txt +++ b/tests/phpstan-next.txt @@ -12,19 +12,15 @@ ./app/Models/Entry.php ./app/Models/Feed.php ./app/Models/FeedDAO.php -./app/Models/FilterAction.php ./app/Models/Search.php ./app/Models/Share.php ./app/Models/TagDAO.php -./app/Models/Themes.php ./app/Services/ImportService.php ./cli/i18n/I18nData.php ./cli/i18n/I18nFile.php ./cli/i18n/I18nValue.php ./lib/http-conditional.php ./lib/Minz/Dispatcher.php -./lib/Minz/Log.php ./lib/Minz/Migrator.php ./lib/Minz/Paginator.php ./lib/Minz/Session.php -./lib/Minz/Translate.php -- cgit v1.2.3