From 1335a0e3cf11a0d4248e9eaaf748b89e6df741ef Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Tue, 4 Jan 2022 13:59:09 +0100 Subject: PHPStan level 5 (#4110) * Fix most PHPDocs errors Contributes to https://github.com/FreshRSS/FreshRSS/issues/4103 https://phpstan.org/writing-php-code/phpdoc-types * Avoid func_get_args Use variadic syntax instead https://php.net/manual/functions.arguments#functions.variable-arg-list And avoid dynamic functions names when possible to more easily identify calls and unused functions. Contributes to https://github.com/FreshRSS/FreshRSS/issues/4103 * PHPStan level 3 * PHPStand level 4 * Update default to PHPStan level 4 * Towards level 5 * Fix level 4 regression * Towards level 5 * Pass PHPStan level 5 * Towards level 6 * Remove erronenous regression from changelog https://github.com/FreshRSS/FreshRSS/pull/4116 --- app/Models/ActionController.php | 9 ++++++ app/Models/Auth.php | 2 +- app/Models/ConfigurationSetter.php | 1 + app/Models/Context.php | 21 +++++++++++-- app/Models/Entry.php | 13 ++++++-- app/Models/EntryDAO.php | 12 +++---- app/Models/EntryDAOSQLite.php | 10 +++--- app/Models/Feed.php | 7 +++-- app/Models/FormAuth.php | 2 +- app/Models/ReadingMode.php | 4 +-- app/Models/Share.php | 2 +- app/Models/StatsDAO.php | 16 +++++----- app/Models/StatsDAOPGSQL.php | 8 ++--- app/Models/SystemConfiguration.php | 28 +++++++++++++++++ app/Models/TagDAO.php | 8 ++--- app/Models/UserConfiguration.php | 64 ++++++++++++++++++++++++++++++++++++++ app/Models/UserQuery.php | 4 +-- app/Models/View.php | 12 +++++++ 18 files changed, 182 insertions(+), 41 deletions(-) create mode 100644 app/Models/ActionController.php create mode 100644 app/Models/SystemConfiguration.php create mode 100644 app/Models/UserConfiguration.php (limited to 'app/Models') diff --git a/app/Models/ActionController.php b/app/Models/ActionController.php new file mode 100644 index 000000000..2e0aaa730 --- /dev/null +++ b/app/Models/ActionController.php @@ -0,0 +1,9 @@ +salt; - $csrf = sha1($salt . uniqid(mt_rand(), true)); + $csrf = sha1($salt . uniqid('' . mt_rand(), true)); Minz_Session::_param('csrf', $csrf); } return $csrf; diff --git a/app/Models/ConfigurationSetter.php b/app/Models/ConfigurationSetter.php index 3c07db4a0..336a93351 100644 --- a/app/Models/ConfigurationSetter.php +++ b/app/Models/ConfigurationSetter.php @@ -371,6 +371,7 @@ class FreshRSS_ConfigurationSetter { $value = intval($value); $limits = $limits_keys[$key]; + // @phpstan-ignore-next-line if ((!isset($limits['min']) || $value >= $limits['min']) && (!isset($limits['max']) || $value <= $limits['max']) ) { diff --git a/app/Models/Context.php b/app/Models/Context.php index 6b2043bd3..62dc53774 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -5,8 +5,17 @@ * useful functions associated to the current view state. */ class FreshRSS_Context { + + /** + * @var FreshRSS_UserConfiguration|null + */ public static $user_conf = null; + + /** + * @var FreshRSS_SystemConfiguration|null + */ public static $system_conf = null; + public static $categories = array(); public static $tags = array(); @@ -49,7 +58,11 @@ class FreshRSS_Context { if ($reload || FreshRSS_Context::$system_conf == null) { //TODO: Keep in session what we need instead of always reloading from disk Minz_Configuration::register('system', DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php'); - FreshRSS_Context::$system_conf = Minz_Configuration::get('system'); + /** + * @var FreshRSS_SystemConfiguration $system_conf + */ + $system_conf = Minz_Configuration::get('system'); + FreshRSS_Context::$system_conf = $system_conf; // Register the configuration setter for the system configuration $configurationSetter = new FreshRSS_ConfigurationSetter(); FreshRSS_Context::$system_conf->_configurationSetter($configurationSetter); @@ -80,7 +93,11 @@ class FreshRSS_Context { FreshRSS_Context::$system_conf->configurationSetter()); Minz_Session::_param('currentUser', $username); - FreshRSS_Context::$user_conf = Minz_Configuration::get('user'); + /** + * @var FreshRSS_UserConfiguration $user_conf + */ + $user_conf = Minz_Configuration::get('user'); + FreshRSS_Context::$user_conf = $user_conf; } catch (Exception $ex) { Minz_Log::warning($ex->getMessage(), USERS_PATH . '/_/log.txt'); } diff --git a/app/Models/Entry.php b/app/Models/Entry.php index d3aa13327..cc1f4d9bc 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -260,7 +260,7 @@ class FreshRSS_Entry extends Minz_Model { } foreach ($booleanSearch->searches() as $filter) { $ok = true; - if ($ok && $filter->getMinDate()) { + if ($filter->getMinDate()) { $ok &= strnatcmp($this->id, $filter->getMinDate() . '000000') >= 0; } if ($ok && $filter->getNotMinDate()) { @@ -451,12 +451,18 @@ class FreshRSS_Entry extends Minz_Model { Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url); } - if ($html) { + if (is_string($html) && strlen($html) > 0) { require_once(LIB_PATH . '/lib_phpQuery.php'); + /** + * @var phpQueryObject @doc + */ $doc = phpQuery::newDocument($html); if ($maxRedirs > 0) { //Follow any HTML redirection + /** + * @var phpQueryObject @metas + */ $metas = $doc->find('meta[http-equiv][content]'); foreach ($metas as $meta) { if (strtolower(trim($meta->getAttribute('http-equiv'))) === 'refresh') { @@ -470,6 +476,9 @@ class FreshRSS_Entry extends Minz_Model { } } + /** + * @var phpQueryObject @content + */ $content = $doc->find($path); $html = trim(sanitizeHTML($content->__toString(), $url)); phpQuery::unloadDocuments(); diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index 2e4f52d0b..ff5777bb0 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -310,7 +310,7 @@ SQL; $hasWhere = false; $values = array(); if ($feedId !== false) { - $sql .= $hasWhere ? ' AND' : ' WHERE'; + $sql .= ' WHERE'; $hasWhere = true; $sql .= ' f.id=?'; $values[] = $feedId; @@ -342,7 +342,7 @@ SQL; * * @param integer|array $ids * @param boolean $is_read - * @return integer affected rows + * @return integer|false affected rows */ public function markRead($ids, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -415,7 +415,7 @@ SQL; * @param integer $idMax fail safe article ID * @param boolean $onlyFavorites * @param integer $priorityMin - * @return integer affected rows + * @return integer|false affected rows */ public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -458,7 +458,7 @@ SQL; * * @param integer $id category ID * @param integer $idMax fail safe article ID - * @return integer affected rows + * @return integer|false affected rows */ public function markReadCat($id, $idMax = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -496,7 +496,7 @@ SQL; * * @param integer $id_feed feed ID * @param integer $idMax fail safe article ID - * @return integer affected rows + * @return integer|false affected rows */ public function markReadFeed($id_feed, $idMax = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -544,7 +544,7 @@ SQL; * Mark all the articles in a tag as read. * @param integer $id tag ID, or empty for targetting any tag * @param integer $idMax max article ID - * @return integer affected rows + * @return integer|false affected rows */ public function markReadTag($id = 0, $idMax = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); diff --git a/app/Models/EntryDAOSQLite.php b/app/Models/EntryDAOSQLite.php index 65df483ff..649536bdf 100644 --- a/app/Models/EntryDAOSQLite.php +++ b/app/Models/EntryDAOSQLite.php @@ -77,7 +77,7 @@ DROP TABLE IF EXISTS `tmp`; $hasWhere = false; $values = array(); if ($feedId !== false) { - $sql .= $hasWhere ? ' AND' : ' WHERE'; + $sql .= ' WHERE'; $hasWhere = true; $sql .= ' id=?'; $values[] = $feedId; @@ -109,7 +109,7 @@ DROP TABLE IF EXISTS `tmp`; * * @param integer|array $ids * @param boolean $is_read - * @return integer affected rows + * @return integer|false affected rows */ public function markRead($ids, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -169,7 +169,7 @@ DROP TABLE IF EXISTS `tmp`; * @param integer $idMax fail safe article ID * @param boolean $onlyFavorites * @param integer $priorityMin - * @return integer affected rows + * @return integer|false affected rows */ public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -210,7 +210,7 @@ DROP TABLE IF EXISTS `tmp`; * * @param integer $id category ID * @param integer $idMax fail safe article ID - * @return integer affected rows + * @return integer|false affected rows */ public function markReadCat($id, $idMax = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); @@ -244,7 +244,7 @@ DROP TABLE IF EXISTS `tmp`; * Mark all the articles in a tag as read. * @param integer $id tag ID, or empty for targetting any tag * @param integer $idMax max article ID - * @return integer affected rows + * @return integer|false affected rows */ public function markReadTag($id = 0, $idMax = 0, $filters = null, $state = 0, $is_read = true) { FreshRSS_UserDAO::touch(); diff --git a/app/Models/Feed.php b/app/Models/Feed.php index 3dc965b32..972983384 100644 --- a/app/Models/Feed.php +++ b/app/Models/Feed.php @@ -259,6 +259,7 @@ class FreshRSS_Feed extends Minz_Model { public function load($loadDetails = false, $noCache = false) { if ($this->url !== null) { + // @phpstan-ignore-next-line if (CACHE_PATH === false) { throw new Minz_FileNotExistException( 'CACHE_PATH', @@ -462,10 +463,10 @@ class FreshRSS_Feed extends Minz_Model { $entry = new FreshRSS_Entry( $this->id(), $hasBadGuids ? '' : $guid, - $title === null ? '' : $title, + $title == '' ? '' : $title, $author_names, - $content === null ? '' : $content, - $link === null ? '' : $link, + $content == '' ? '' : $content, + $link == '' ? '' : $link, $date ? $date : time() ); $entry->_tags($tags); diff --git a/app/Models/FormAuth.php b/app/Models/FormAuth.php index 5211fd5d1..d6da637d1 100644 --- a/app/Models/FormAuth.php +++ b/app/Models/FormAuth.php @@ -50,7 +50,7 @@ class FreshRSS_FormAuth { public static function makeCookie($username, $password_hash) { do { - $token = sha1(FreshRSS_Context::$system_conf->salt . $username . uniqid(mt_rand(), true)); + $token = sha1(FreshRSS_Context::$system_conf->salt . $username . uniqid('' . mt_rand(), true)); $token_file = DATA_PATH . '/tokens/' . $token . '.txt'; } while (file_exists($token_file)); diff --git a/app/Models/ReadingMode.php b/app/Models/ReadingMode.php index 06af1704c..ddb413315 100644 --- a/app/Models/ReadingMode.php +++ b/app/Models/ReadingMode.php @@ -81,14 +81,14 @@ class FreshRSS_ReadingMode { } /** - * @return string + * @return array */ public function getUrlParams() { return $this->urlParams; } /** - * @param string $urlParams + * @param array $urlParams * @return FreshRSS_ReadingMode */ public function setUrlParams($urlParams) { diff --git a/app/Models/Share.php b/app/Models/Share.php index 695b16eca..0a341fa55 100644 --- a/app/Models/Share.php +++ b/app/Models/Share.php @@ -11,7 +11,7 @@ class FreshRSS_Share { /** * Register a new sharing option. - * @param array $share_options is an array defining the share option. + * @param array> $share_options is an array defining the share option. */ public static function register($share_options) { $type = $share_options['type']; diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php index 33e5426b5..612fe5cab 100644 --- a/app/Models/StatsDAO.php +++ b/app/Models/StatsDAO.php @@ -133,7 +133,7 @@ SQL; * * @param string $period format string to use for grouping * @param integer $feed id - * @return array + * @return array */ protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) { $restrict = ''; @@ -178,7 +178,7 @@ SQL; * Calculates the average number of article per hour per feed * * @param integer $feed id - * @return integer + * @return float */ public function calculateEntryAveragePerFeedPerHour($feed = null) { return $this->calculateEntryAveragePerFeedPerPeriod(1 / 24, $feed); @@ -188,7 +188,7 @@ SQL; * Calculates the average number of article per day of week per feed * * @param integer $feed id - * @return integer + * @return float */ public function calculateEntryAveragePerFeedPerDayOfWeek($feed = null) { return $this->calculateEntryAveragePerFeedPerPeriod(7, $feed); @@ -198,7 +198,7 @@ SQL; * Calculates the average number of article per month per feed * * @param integer $feed id - * @return integer + * @return float */ public function calculateEntryAveragePerFeedPerMonth($feed = null) { return $this->calculateEntryAveragePerFeedPerPeriod(30, $feed); @@ -209,7 +209,7 @@ SQL; * * @param float $period number used to divide the number of day in the period * @param integer $feed id - * @return integer + * @return float */ protected function calculateEntryAveragePerFeedPerPeriod($period, $feed = null) { $restrict = ''; @@ -337,7 +337,7 @@ SQL; /** * Gets days ready for graphs * - * @return string + * @return array */ public function getDays() { return $this->convertToTranslatedJson(array( @@ -354,7 +354,7 @@ SQL; /** * Gets months ready for graphs * - * @return string + * @return array */ public function getMonths() { return $this->convertToTranslatedJson(array( @@ -377,7 +377,7 @@ SQL; * Translates array content * * @param array $data - * @return array + * @return array */ private function convertToTranslatedJson($data = array()) { $translated = array_map(function($a) { diff --git a/app/Models/StatsDAOPGSQL.php b/app/Models/StatsDAOPGSQL.php index f36d29770..85f9d63f3 100644 --- a/app/Models/StatsDAOPGSQL.php +++ b/app/Models/StatsDAOPGSQL.php @@ -6,7 +6,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { * Calculates the number of article per hour of the day per feed * * @param integer $feed id - * @return string + * @return array */ public function calculateEntryRepartitionPerFeedPerHour($feed = null) { return $this->calculateEntryRepartitionPerFeedPerPeriod('hour', $feed); @@ -16,7 +16,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { * Calculates the number of article per day of week per feed * * @param integer $feed id - * @return string + * @return array */ public function calculateEntryRepartitionPerFeedPerDayOfWeek($feed = null) { return $this->calculateEntryRepartitionPerFeedPerPeriod('day', $feed); @@ -26,7 +26,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { * Calculates the number of article per month per feed * * @param integer $feed - * @return string + * @return array */ public function calculateEntryRepartitionPerFeedPerMonth($feed = null) { return $this->calculateEntryRepartitionPerFeedPerPeriod('month', $feed); @@ -37,7 +37,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { * * @param string $period format string to use for grouping * @param integer $feed id - * @return string + * @return array */ protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) { $restrict = ''; diff --git a/app/Models/SystemConfiguration.php b/app/Models/SystemConfiguration.php new file mode 100644 index 000000000..3b07bc9ff --- /dev/null +++ b/app/Models/SystemConfiguration.php @@ -0,0 +1,28 @@ + $curl_options + * @property string $default_user + * @property string $email_validation_token + * @property bool $force_email_validation + * @property-read bool $http_auth_auto_register + * @property-read string $http_auth_auto_register_email_field + * @property-read string $language + * @property array $limits + * @property-read string $meta_description + * @property-read bool $pubsubhubbub_enabled + * @property-read string $salt + * @property-read bool $simplepie_syslog_enabled + * @property string $unsafe_autologin_enabled + */ +class FreshRSS_SystemConfiguration extends Minz_Configuration { + +} diff --git a/app/Models/TagDAO.php b/app/Models/TagDAO.php index a0e492739..2b24921a2 100644 --- a/app/Models/TagDAO.php +++ b/app/Models/TagDAO.php @@ -74,8 +74,8 @@ SQL; * @param FreshRSS_Tag $tag */ public function addTagObject($tag) { - $tag = $this->searchByName($tag->name()); - if (!$tag) { + $tag0 = $this->searchByName($tag->name()); + if (!$tag0) { $values = array( 'name' => $tag->name(), 'attributes' => $tag->attributes(), @@ -198,7 +198,7 @@ SQL; } /** - * @return FreshRSS_Tag + * @return FreshRSS_Tag|null */ public function searchById($id) { $sql = 'SELECT * FROM `_tag` WHERE id=?'; @@ -211,7 +211,7 @@ SQL; } /** - * @return FreshRSS_Tag + * @return FreshRSS_Tag|null */ public function searchByName($name) { $sql = 'SELECT * FROM `_tag` WHERE name=?'; diff --git a/app/Models/UserConfiguration.php b/app/Models/UserConfiguration.php new file mode 100644 index 000000000..864bf4ec8 --- /dev/null +++ b/app/Models/UserConfiguration.php @@ -0,0 +1,64 @@ + $archiving + * @property bool $auto_load_more + * @property bool $auto_remove_article + * @property bool $bottomline_date + * @property bool $bottomline_favorite + * @property bool $bottomline_link + * @property bool $bottomline_read + * @property bool $bottomline_sharing + * @property bool $bottomline_tags + * @property string $content_width + * @property-read string $default_state + * @property string $default_view + * @property string|bool $display_categories + * @property bool $display_posts + * @property string $email_validation_token + * @property-read string $enabled + * @property string $feverKey + * @property bool $hide_read_feeds + * @property int $html5_notif_timeout + * @property-read string $is_admin + * @property int|null $keep_history_default + * @property string $language + * @property bool $lazyload + * @property string $mail_login + * @property bool $mark_updated_article_unread + * @property array $mark_when + * @property int $max_posts_per_rss + * @property-read array $limits + * @property int|null $old_entries + * @property bool $onread_jump_next + * @property string $passwordHash + * @property int $posts_per_page + * @property array> $queries + * @property bool $reading_confirm + * @property int $since_hours_posts_per_rss + * @property bool $show_fav_unread + * @property bool $show_favicons + * @property bool $show_nav_buttons + * @property string $sort_order + * @property array> $sharing + * @property array $shortcuts + * @property bool $sides_close_article + * @property bool $sticky_post + * @property string $theme + * @property string $token + * @property bool $topline_date + * @property bool $topline_display_authors + * @property bool $topline_favorite + * @property bool $topline_link + * @property bool $topline_read + * @property bool $topline_summary + * @property string $topline_thumbnail + * @property int $ttl_default + * @property-read bool $unsafe_autologin_enabled + * @property string $view_mode + * @property array $volatile + */ +class FreshRSS_UserConfiguration extends Minz_Configuration { + +} diff --git a/app/Models/UserQuery.php b/app/Models/UserQuery.php index 88ddd3629..7f8aa07ef 100644 --- a/app/Models/UserQuery.php +++ b/app/Models/UserQuery.php @@ -22,7 +22,7 @@ class FreshRSS_UserQuery { private $tag_dao; /** - * @param array $query + * @param array $query * @param FreshRSS_Searchable $feed_dao * @param FreshRSS_Searchable $category_dao */ @@ -55,7 +55,7 @@ class FreshRSS_UserQuery { /** * Convert the current object to an array. * - * @return array + * @return array */ public function toArray() { return array_filter(array( diff --git a/app/Models/View.php b/app/Models/View.php index 22bc2c49a..e3a591155 100644 --- a/app/Models/View.php +++ b/app/Models/View.php @@ -4,9 +4,11 @@ class FreshRSS_View extends Minz_View { // Main views public $callbackBeforeEntries; + public $callbackBeforeFeeds; public $callbackBeforePagination; public $categories; public $category; + public $current_user; public $entries; public $entry; public $feed; @@ -33,6 +35,7 @@ class FreshRSS_View extends Minz_View { public $status_files; public $status_php; public $update_to_apply; + public $status_database; // Archiving public $nb_total; @@ -46,12 +49,19 @@ class FreshRSS_View extends Minz_View { public $list_keys; // User queries + /** + * @var array + */ public $queries; + /** + * @var FreshRSS_UserQuery|null + */ public $query; // Export / Import public $content; public $entriesRaw; + public $entriesId; public $entryIdsTagNames; public $list_title; public $queryId; @@ -87,6 +97,7 @@ class FreshRSS_View extends Minz_View { public $selectorSuccess; // Extensions + public $available_extensions; public $ext_details; public $extension_list; public $extension; @@ -95,6 +106,7 @@ class FreshRSS_View extends Minz_View { // Errors public $code; public $errorMessage; + public $message; // Statistics public $average; -- cgit v1.2.3