浏览代码

Cleaning code and typehinting (#5064)

* Cleaning code and typehinting

* Fix remarque alphabetic order

* Cleaning

* rollback self::

* Update Context.php

* Fix remarques

* Fix remarques

* Fix remarques

* Remarque's from Alkarex

* Remarque's from Alkarex

* Cast higher up

* Fix Level 5

* Claiming Level 6
Cf. https://github.com/FreshRSS/FreshRSS/pull/5230

* Address my comments

* indexController as Level 6 as well

* Fixed some wrong types

---------

Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Luc SANCHEZ 3 年之前
父节点
当前提交
ac3dd96f48
共有 4 个文件被更改,包括 94 次插入48 次删除
  1. 11 10
      app/Controllers/indexController.php
  2. 81 36
      app/Models/Context.php
  3. 2 0
      composer.json
  4. 0 2
      tests/phpstan-next.txt

+ 11 - 10
app/Controllers/indexController.php

@@ -8,7 +8,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	/**
 	 * This action only redirect on the default view mode (normal or global)
 	 */
-	public function indexAction() {
+	public function indexAction(): void {
 		$preferred_output = FreshRSS_Context::$user_conf->view_mode;
 		Minz_Request::forward(array(
 			'c' => 'index',
@@ -19,7 +19,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	/**
 	 * This action displays the normal view of FreshRSS.
 	 */
-	public function normalAction() {
+	public function normalAction(): void {
 		$allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
 		if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
 			Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
@@ -98,14 +98,14 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	 *
 	 * @todo: change this view into specific CSS rules?
 	 */
-	public function readerAction() {
+	public function readerAction(): void {
 		$this->normalAction();
 	}
 
 	/**
 	 * This action displays the global view of FreshRSS.
 	 */
-	public function globalAction() {
+	public function globalAction(): void {
 		$allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
 		if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
 			Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
@@ -141,7 +141,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	/**
 	 * This action displays the RSS feed of FreshRSS.
 	 */
-	public function rssAction() {
+	public function rssAction(): void {
 		$allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
 		$token = FreshRSS_Context::$user_conf->token;
 		$token_param = Minz_Request::param('token', '');
@@ -174,7 +174,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 		header('Content-Type: application/rss+xml; charset=utf-8');
 	}
 
-	public function opmlAction() {
+	public function opmlAction(): void {
 		$allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
 		$token = FreshRSS_Context::$user_conf->token;
 		$token_param = Minz_Request::param('token', '');
@@ -197,7 +197,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 			$id = $get[1];
 		} else {
 			$type = $get;
-			$id = '';
+			$id = 0;
 		}
 
 		$catDAO = FreshRSS_Factory::createCategoryDao();
@@ -244,6 +244,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 
 	/**
 	 * This method returns a list of entries based on the Context object.
+	 * @return iterable<FreshRSS_Entry>
 	 */
 	public static function listEntriesByContext() {
 		$entryDAO = FreshRSS_Factory::createEntryDao();
@@ -277,7 +278,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	/**
 	 * This action displays the about page of FreshRSS.
 	 */
-	public function aboutAction() {
+	public function aboutAction(): void {
 		FreshRSS_View::prependTitle(_t('index.about.title') . ' · ');
 	}
 
@@ -287,7 +288,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	 * The content of the page is the content of data/tos.html.
 	 * It returns 404 if there is no EULA/TOS.
 	 */
-	public function tosAction() {
+	public function tosAction(): void {
 		$terms_of_service = file_get_contents(TOS_FILENAME);
 		if (!$terms_of_service) {
 			Minz_Error::error(404);
@@ -301,7 +302,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 	/**
 	 * This action displays logs of FreshRSS for the current user.
 	 */
-	public function logsAction() {
+	public function logsAction(): void {
 		if (!FreshRSS_Auth::hasAccess()) {
 			Minz_Error::error(403);
 		}

+ 81 - 36
app/Models/Context.php

@@ -9,47 +9,92 @@ final class FreshRSS_Context {
 	/**
 	 * @var FreshRSS_UserConfiguration|null
 	 */
-	public static $user_conf = null;
+	public static $user_conf;
 
 	/**
 	 * @var FreshRSS_SystemConfiguration|null
 	 */
-	public static $system_conf = null;
-
+	public static $system_conf;
+	/**
+	 * @var array<FreshRSS_Category>
+	 */
 	public static $categories = array();
+	/**
+	 * @var array<string>
+	 */
 	public static $tags = array();
-
+	/**
+	 * @var string
+	 */
 	public static $name = '';
+	/**
+	 * @var string
+	 */
 	public static $description = '';
-
+	/**
+	 * @var int
+	 */
 	public static $total_unread = 0;
-	public static $total_starred = array(
+
+	/** @var array{'all':int,'read':int,'unread':int} */
+	public static $total_starred = [
 		'all' => 0,
 		'read' => 0,
 		'unread' => 0,
-	);
+	];
 
+	/**
+	 * @var int
+	 */
 	public static $get_unread = 0;
-	public static $current_get = array(
+
+	/** @var array{'all':bool,'starred':bool,'feed':int|false,'category':int|false,'tag':int|false,'tags':bool} */
+	public static $current_get = [
 		'all' => false,
 		'starred' => false,
 		'feed' => false,
 		'category' => false,
 		'tag' => false,
 		'tags' => false,
-	);
-	public static $next_get = 'a';
+	];
 
+	/**
+	 * @var string
+	 */
+	public static $next_get = 'a';
+	/**
+	 * @var int
+	 */
 	public static $state = 0;
+	/**
+	 * @var string
+	 */
 	public static $order = 'DESC';
+	/**
+	 * @var int
+	 */
 	public static $number = 0;
 	/** @var FreshRSS_BooleanSearch */
 	public static $search;
+	/**
+	 * @var string
+	 */
 	public static $first_id = '';
+	/**
+	 * @var string
+	 */
 	public static $next_id = '';
+	/**
+	 * @var string
+	 */
 	public static $id_max = '';
+	/**
+	 * @var int
+	 */
 	public static $sinceHours = 0;
-
+	/**
+	 * @var bool
+	 */
 	public static $isCli = false;
 
 	/**
@@ -69,6 +114,7 @@ final class FreshRSS_Context {
 	/**
 	 * Initialize the context for the current user.
 	 * @return FreshRSS_UserConfiguration|false
+	 * @throws Minz_ConfigurationParamException
 	 */
 	public static function initUser(string $username = '', bool $userMustExist = true) {
 		FreshRSS_Context::$user_conf = null;
@@ -144,8 +190,11 @@ final class FreshRSS_Context {
 	 *   - nb (default: conf->posts_per_page)
 	 *   - next (default: empty string)
 	 *   - hours (default: 0)
+	 * @throws FreshRSS_Context_Exception
+	 * @throws Minz_ConfigurationNamespaceException
+	 * @throws Minz_PDOConnectionException
 	 */
-	public static function updateUsingRequest() {
+	public static function updateUsingRequest(): void {
 		if (empty(self::$categories)) {
 			$catDAO = FreshRSS_Factory::createCategoryDao();
 			self::$categories = $catDAO->listSortedCategories();
@@ -158,12 +207,12 @@ final class FreshRSS_Context {
 			self::$categories, 1
 		);
 
-		self::_get(Minz_Request::param('get', 'a'));
+		self::_get(Minz_Request::param('get', 'a', false));
 
 		self::$state = Minz_Request::param(
 			'state', self::$user_conf->default_state
 		);
-		$state_forced_by_user = Minz_Request::param('state', false) !== false;
+		$state_forced_by_user = Minz_Request::param('state') !== false;
 		if (!$state_forced_by_user && !self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
 			if (self::$user_conf->default_view === 'adaptive' && self::$get_unread <= 0) {
 				self::$state |= FreshRSS_Entry::STATE_READ;
@@ -178,34 +227,31 @@ final class FreshRSS_Context {
 		self::$order = Minz_Request::param(
 			'order', self::$user_conf->sort_order
 		);
-		self::$number = intval(Minz_Request::param('nb', self::$user_conf->posts_per_page));
+		self::$number = (int)Minz_Request::param('nb', self::$user_conf->posts_per_page);
 		if (self::$number > self::$user_conf->max_posts_per_rss) {
 			self::$number = max(
 				self::$user_conf->max_posts_per_rss,
 				self::$user_conf->posts_per_page);
 		}
 		self::$first_id = Minz_Request::param('next', '');
-		self::$sinceHours = intval(Minz_Request::param('hours', 0));
+		self::$sinceHours = (int)Minz_Request::param('hours', 0);
 	}
 
 	/**
 	 * Returns if the current state includes $state parameter.
-	 * @param int $state
 	 */
-	public static function isStateEnabled($state) {
+	public static function isStateEnabled(int $state): int {
 		return self::$state & $state;
 	}
 
 	/**
 	 * Returns the current state with or without $state parameter.
-	 * @param int $state
 	 */
-	public static function getRevertState($state) {
+	public static function getRevertState(int $state): int {
 		if (self::$state & $state) {
 			return self::$state & ~$state;
-		} else {
-			return self::$state | $state;
 		}
+		return self::$state | $state;
 	}
 
 	/**
@@ -215,25 +261,25 @@ final class FreshRSS_Context {
 	 * the second is the id.
 	 * @return string|array{string,bool|int}
 	 */
-	public static function currentGet($array = false) {
+	public static function currentGet(bool $asArray = false) {
 		if (self::$current_get['all']) {
 			return 'a';
 		} elseif (self::$current_get['starred']) {
 			return 's';
 		} elseif (self::$current_get['feed']) {
-			if ($array) {
+			if ($asArray) {
 				return array('f', self::$current_get['feed']);
 			} else {
 				return 'f_' . self::$current_get['feed'];
 			}
 		} elseif (self::$current_get['category']) {
-			if ($array) {
+			if ($asArray) {
 				return array('c', self::$current_get['category']);
 			} else {
 				return 'c_' . self::$current_get['category'];
 			}
 		} elseif (self::$current_get['tag']) {
-			if ($array) {
+			if ($asArray) {
 				return array('t', self::$current_get['tag']);
 			} else {
 				return 't_' . self::$current_get['tag'];
@@ -273,7 +319,7 @@ final class FreshRSS_Context {
 	}
 
 	/**
-	 * @return bool true if $get parameter correspond to the $current_get attribute.
+	 * @return bool whether $get parameter corresponds to the $current_get attribute.
 	 */
 	public static function isCurrentGet(string $get): bool {
 		$type = substr($get, 0, 1);
@@ -309,10 +355,13 @@ final class FreshRSS_Context {
 	 *
 	 * $name and $get_unread attributes are also updated as $next_get
 	 * Raise an exception if id or $get is invalid.
+	 * @throws FreshRSS_Context_Exception
+	 * @throws Minz_ConfigurationNamespaceException
+	 * @throws Minz_PDOConnectionException
 	 */
-	public static function _get($get) {
+	public static function _get(string $get): void {
 		$type = $get[0];
-		$id = intval(substr($get, 2));
+		$id = (int)substr($get, 2);
 
 		if (empty(self::$categories)) {
 			$catDAO = FreshRSS_Factory::createCategoryDao();
@@ -397,7 +446,7 @@ final class FreshRSS_Context {
 	/**
 	 * Set the value of $next_get attribute.
 	 */
-	private static function _nextGet() {
+	private static function _nextGet(): void {
 		$get = self::currentGet();
 		// By default, $next_get == $get
 		self::$next_get = $get;
@@ -469,10 +518,8 @@ final class FreshRSS_Context {
 	 *   - it is activated in the configuration
 	 *   - the "read" state is not enable
 	 *   - the "unread" state is enable
-	 *
-	 * @return boolean
 	 */
-	public static function isAutoRemoveAvailable() {
+	public static function isAutoRemoveAvailable(): bool {
 		if (!self::$user_conf->auto_remove_article) {
 			return false;
 		}
@@ -490,10 +537,8 @@ final class FreshRSS_Context {
 	 * by the user when it is selected in the configuration page or by the
 	 * application when the context allows to auto-remove articles when they
 	 * are read.
-	 *
-	 * @return boolean
 	 */
-	public static function isStickyPostEnabled() {
+	public static function isStickyPostEnabled(): bool {
 		if (self::$user_conf->sticky_post) {
 			return true;
 		}

+ 2 - 0
composer.json

@@ -25,9 +25,11 @@
         "ext-gmp": "*",
         "ext-intl": "*",
         "ext-json": "*",
+        "ext-libxml": "*",
         "ext-mbstring": "*",
         "ext-openssl": "*",
         "ext-pcre": "*",
+        "ext-pdo": "*",
         "ext-pdo_sqlite": "*",
         "ext-session": "*",
         "ext-simplexml": "*",

+ 0 - 2
tests/phpstan-next.txt

@@ -6,7 +6,6 @@
 
 ./app/Controllers/extensionController.php
 ./app/Controllers/feedController.php
-./app/Controllers/indexController.php
 ./app/Controllers/updateController.php
 ./app/Controllers/userController.php
 ./app/Exceptions/AlreadySubscribedException.php
@@ -21,7 +20,6 @@
 ./app/Models/CategoryDAO.php
 ./app/Models/CategoryDAOSQLite.php
 ./app/Models/ConfigurationSetter.php
-./app/Models/Context.php
 ./app/Models/DatabaseDAO.php
 ./app/Models/DatabaseDAOPGSQL.php
 ./app/Models/DatabaseDAOSQLite.php