Bladeren bron

PHPStan Level 6 for more files (#5275)

Contributes to https://github.com/FreshRSS/FreshRSS/issues/4112
Alexandre Alapetite 3 jaren geleden
bovenliggende
commit
743ca371bb
6 gewijzigde bestanden met toevoegingen van 53 en 34 verwijderingen
  1. 13 7
      app/Models/FilterAction.php
  2. 20 7
      app/Models/Themes.php
  3. 1 1
      app/Models/View.php
  4. 6 6
      lib/Minz/Log.php
  5. 13 9
      lib/Minz/Translate.php
  6. 0 4
      tests/phpstan-next.txt

+ 13 - 7
app/Models/FilterAction.php

@@ -4,22 +4,26 @@ class FreshRSS_FilterAction {
 
 	/** @var FreshRSS_BooleanSearch */
 	private $booleanSearch = null;
+	/** @var array<string>|null */
 	private $actions = null;
 
-	private function __construct($booleanSearch, $actions) {
+	/** @param array<string> $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<string> */
+	public function actions(): array {
+		return $this->actions ?? [];
 	}
 
-	public function _actions($actions) {
+	/** @param array<string> $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<string>} */
+	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']);
 		}

+ 20 - 7
app/Models/Themes.php

@@ -1,18 +1,23 @@
 <?php
 
 class FreshRSS_Themes extends Minz_Model {
+	/** @var string */
 	private static $themesUrl = '/themes/';
+	/** @var string */
 	private static $defaultIconsUrl = '/themes/icons/';
+	/** @var string */
 	public static $defaultTheme = 'Origine';
 
-	public static function getList() {
+	/** @return array<string> */
+	public static function getList(): array {
 		return array_values(array_diff(
 			scandir(PUBLIC_PATH . self::$themesUrl),
 			array('..', '.')
 		));
 	}
 
-	public static function get() {
+	/** @return array<string,array{'id':string,'name':string,'author':string,'description':string,'version':float|string,'files':array<string>,'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<string>,'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<string,int> */
 	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<string>,'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+

+ 1 - 1
app/Models/View.php

@@ -81,7 +81,7 @@ class FreshRSS_View extends Minz_View {
 	public $size_user;
 
 	// Display
-	/** @var array<string> */
+	/** @var array<string,array{'id':string,'name':string,'author':string,'description':string,'version':float|string,'files':array<string>,'theme-color'?:string|array{'dark'?:string,'light'?:string,'default'?:string}}> */
 	public $themes;
 
 	// Shortcuts

+ 6 - 6
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);
 	}
 }

+ 13 - 9
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<string>
 	 */
 	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<string,array<string>>
 	 */
 	private static $lang_files = array();
 
 	/**
 	 * $translates is a cache for i18n translation.
+	 * @var array<string,mixed>
 	 */
 	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<string> 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);
 }

+ 0 - 4
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