From a784305cc764a7e039ad87074f541db68a653a5a Mon Sep 17 00:00:00 2001 From: wn_ Date: Tue, 12 Nov 2024 03:15:53 +0000 Subject: Address PHPStan findings as of 2.0.1 --- classes/API.php | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'classes/API.php') diff --git a/classes/API.php b/classes/API.php index 6dbb1f246..d6d622c52 100644 --- a/classes/API.php +++ b/classes/API.php @@ -235,7 +235,7 @@ class API extends Handler { } function updateArticle(): bool { - $article_ids = explode(",", clean($_REQUEST["article_ids"])); + $article_ids = array_filter(explode(",", clean($_REQUEST["article_ids"] ?? ""))); $mode = (int) clean($_REQUEST["mode"]); $data = clean($_REQUEST["data"] ?? ""); $field_raw = (int)clean($_REQUEST["field"]); @@ -550,26 +550,22 @@ class API extends Handler { /* Virtual feeds */ - $vfeeds = PluginHost::getInstance()->get_feeds(Feeds::CATEGORY_SPECIAL); - - if (is_array($vfeeds)) { - foreach ($vfeeds as $feed) { - if (!implements_interface($feed['sender'], 'IVirtualFeed')) - continue; + foreach (PluginHost::getInstance()->get_feeds(Feeds::CATEGORY_SPECIAL) as $feed) { + if (!implements_interface($feed['sender'], 'IVirtualFeed')) + continue; - /** @var IVirtualFeed $feed['sender'] */ - $unread = $feed['sender']->get_unread($feed['id']); + /** @var IVirtualFeed $feed['sender'] */ + $unread = $feed['sender']->get_unread($feed['id']); - if ($unread || !$unread_only) { - $row = [ - 'id' => PluginHost::pfeed_to_feed_id($feed['id']), - 'title' => $feed['title'], - 'unread' => $unread, - 'cat_id' => Feeds::CATEGORY_SPECIAL, - ]; + if ($unread || !$unread_only) { + $row = [ + 'id' => PluginHost::pfeed_to_feed_id($feed['id']), + 'title' => $feed['title'], + 'unread' => $unread, + 'cat_id' => Feeds::CATEGORY_SPECIAL, + ]; - array_push($feeds, $row); - } + array_push($feeds, $row); } } @@ -696,10 +692,11 @@ class API extends Handler { if (!$is_cat && is_numeric($feed_id) && $feed_id < PLUGIN_FEED_BASE_INDEX && $feed_id > LABEL_BASE_INDEX) { $pfeed_id = PluginHost::feed_to_pfeed_id($feed_id); - /** @var IVirtualFeed|false $handler */ $handler = PluginHost::getInstance()->get_feed_handler($pfeed_id); - if ($handler) { + if ($handler && implements_interface($handler, 'IVirtualFeed')) { + /** @var Plugin&IVirtualFeed $handler */ + $params = array( "feed" => $feed_id, "limit" => $limit, @@ -858,7 +855,7 @@ class API extends Handler { array_push($headlines, $headline_row); } - } else if (is_numeric($result) && $result == -1) { + } else if ($result == -1) { $headlines_header['first_id_changed'] = true; } -- cgit v1.2.3-54-g00ecf From dca2ae60a1c7a7951ee769aefeb19c4576c57aa6 Mon Sep 17 00:00:00 2001 From: wn_ Date: Tue, 12 Nov 2024 03:38:45 +0000 Subject: Remove some PHPStan ignores and make others rule-specific. --- classes/API.php | 3 +-- classes/Debug.php | 2 +- classes/FeedItem_Atom.php | 2 -- classes/Feeds.php | 2 +- classes/Handler_Public.php | 5 ++--- classes/PluginHost.php | 3 +-- classes/Pref_Filters.php | 3 --- classes/UserHelper.php | 2 +- update.php | 2 +- 9 files changed, 8 insertions(+), 16 deletions(-) (limited to 'classes/API.php') diff --git a/classes/API.php b/classes/API.php index d6d622c52..d44a7eaea 100644 --- a/classes/API.php +++ b/classes/API.php @@ -300,10 +300,9 @@ class API extends Handler { } function getArticle(): bool { - $article_ids = explode(',', clean($_REQUEST['article_id'] ?? '')); + $article_ids = array_filter(explode(',', clean($_REQUEST['article_id'] ?? ''))); $sanitize_content = self::_param_to_bool($_REQUEST['sanitize'] ?? true); - // @phpstan-ignore-next-line if (count($article_ids)) { $entries = ORM::for_table('ttrss_entries') ->table_alias('e') diff --git a/classes/Debug.php b/classes/Debug.php index 1cda12539..ba39b4d44 100644 --- a/classes/Debug.php +++ b/classes/Debug.php @@ -77,7 +77,7 @@ class Debug { */ public static function map_loglevel(int $level) : int { if (in_array($level, self::ALL_LOG_LEVELS)) { - /** @phpstan-ignore-next-line */ + /** @phpstan-ignore return.type (yes it is a Debug::LOG_* value) */ return $level; } else { user_error("Passed invalid debug log level: $level", E_USER_WARNING); diff --git a/classes/FeedItem_Atom.php b/classes/FeedItem_Atom.php index f6c96f959..b5eaca181 100644 --- a/classes/FeedItem_Atom.php +++ b/classes/FeedItem_Atom.php @@ -43,7 +43,6 @@ class FeedItem_Atom extends FeedItem_Common { $links = $this->elem->getElementsByTagName("link"); foreach ($links as $link) { - /** @phpstan-ignore-next-line */ if ($link->hasAttribute("href") && (!$link->hasAttribute("rel") || $link->getAttribute("rel") == "alternate" @@ -181,7 +180,6 @@ class FeedItem_Atom extends FeedItem_Common { $encs = []; foreach ($links as $link) { - /** @phpstan-ignore-next-line */ if ($link->hasAttribute("href") && $link->hasAttribute("rel")) { $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link); diff --git a/classes/Feeds.php b/classes/Feeds.php index 38c098724..22f34f719 100644 --- a/classes/Feeds.php +++ b/classes/Feeds.php @@ -1067,7 +1067,7 @@ class Feeds extends Handler_Protected { if (!$url) return ["code" => 2]; PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_PRE_SUBSCRIBE, - /** @phpstan-ignore-next-line */ + /** @phpstan-ignore closure.unusedUse, closure.unusedUse, closure.unusedUse */ function ($result) use (&$url, &$auth_login, &$auth_pass) { // arguments are updated inside the hook (if needed) }, diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index 208c8e40a..be4254783 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -53,9 +53,8 @@ class Handler_Public extends Handler { $handler = $tmppluginhost->get_feed_handler( PluginHost::feed_to_pfeed_id((int)$feed)); - if ($handler) { - // 'get_headlines' is implemented by the plugin. - // @phpstan-ignore-next-line + if ($handler && implements_interface($handler, 'IVirtualFeed')) { + /** @var Plugin&IVirtualFeed $handler */ $qfh_ret = $handler->get_headlines(PluginHost::feed_to_pfeed_id((int)$feed), $params); } else { user_error("Failed to find handler for plugin feed ID: $feed", E_USER_ERROR); diff --git a/classes/PluginHost.php b/classes/PluginHost.php index 88eb5d2a4..b54e48d4f 100644 --- a/classes/PluginHost.php +++ b/classes/PluginHost.php @@ -386,8 +386,7 @@ class PluginHost { * @param PluginHost::HOOK_* $type */ function del_hook(string $type, Plugin $sender): void { - /** @phpstan-ignore function.alreadyNarrowedType ($type might not exist) */ - if (is_array($this->hooks[$type])) { + if (array_key_exists($type, $this->hooks)) { foreach (array_keys($this->hooks[$type]) as $prio) { $key = array_search($sender, $this->hooks[$type][$prio]); diff --git a/classes/Pref_Filters.php b/classes/Pref_Filters.php index 6e609b76c..87237342a 100644 --- a/classes/Pref_Filters.php +++ b/classes/Pref_Filters.php @@ -115,9 +115,6 @@ class Pref_Filters extends Handler_Protected { $glue = $filter['match_any_rule'] ? " OR " : " AND "; $scope_qpart = join($glue, $scope_qparts); - /** @phpstan-ignore-next-line */ - if (!$scope_qpart) $scope_qpart = "true"; - $rv = array(); //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) { diff --git a/classes/UserHelper.php b/classes/UserHelper.php index 6c294a9a8..27e022624 100644 --- a/classes/UserHelper.php +++ b/classes/UserHelper.php @@ -50,7 +50,7 @@ class UserHelper { */ public static function map_access_level(int $level) : int { if (in_array($level, self::ACCESS_LEVELS)) { - /** @phpstan-ignore-next-line */ + /** @phpstan-ignore return.type (yes it is a UserHelper::ACCESS_LEVEL_* value) */ return $level; } else { user_error("Passed invalid user access level: $level", E_USER_WARNING); diff --git a/update.php b/update.php index c8fe69fdb..c7f274419 100755 --- a/update.php +++ b/update.php @@ -215,7 +215,7 @@ } if (isset($options["daemon"])) { - // @phpstan-ignore-next-line + // @phpstan-ignore while.alwaysTrue (single process daemon will always run) while (true) { $quiet = (isset($options["quiet"])) ? "--quiet" : ""; $log = isset($options['log']) ? '--log '.$options['log'] : ''; -- cgit v1.2.3-54-g00ecf From 5a200755b8afd8e719214f19b51a38dc8870fe09 Mon Sep 17 00:00:00 2001 From: wn_ Date: Tue, 12 Nov 2024 03:49:58 +0000 Subject: Move 'IVirtualFeed' checks into 'PluginHost::get_feed_handler()'. --- classes/API.php | 4 +--- classes/Feeds.php | 13 +++---------- classes/Handler_Public.php | 3 +-- classes/PluginHost.php | 13 +++++++++++-- 4 files changed, 16 insertions(+), 17 deletions(-) (limited to 'classes/API.php') diff --git a/classes/API.php b/classes/API.php index d44a7eaea..826d1f333 100644 --- a/classes/API.php +++ b/classes/API.php @@ -693,9 +693,7 @@ class API extends Handler { $handler = PluginHost::getInstance()->get_feed_handler($pfeed_id); - if ($handler && implements_interface($handler, 'IVirtualFeed')) { - /** @var Plugin&IVirtualFeed $handler */ - + if ($handler) { $params = array( "feed" => $feed_id, "limit" => $limit, diff --git a/classes/Feeds.php b/classes/Feeds.php index 22f34f719..b4df690a7 100644 --- a/classes/Feeds.php +++ b/classes/Feeds.php @@ -106,9 +106,7 @@ class Feeds extends Handler_Protected { $handler = PluginHost::getInstance()->get_feed_handler( PluginHost::feed_to_pfeed_id($feed)); - if ($handler && implements_interface($handler, 'IVirtualFeed')) { - /** @var Plugin&IVirtualFeed $handler */ - + if ($handler) { $options = array( "limit" => $limit, "view_mode" => $view_mode, @@ -936,15 +934,10 @@ class Feeds extends Handler_Protected { if ($is_cat) { return self::_get_cat_unread($n_feed, $owner_uid); - } else if(is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX && $feed > LABEL_BASE_INDEX) { // virtual Feed + } else if (is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX && $feed > LABEL_BASE_INDEX) { // virtual Feed $feed_id = PluginHost::feed_to_pfeed_id($feed); $handler = PluginHost::getInstance()->get_feed_handler($feed_id); - if (implements_interface($handler, 'IVirtualFeed')) { - /** @var Plugin&IVirtualFeed $handler */ - return $handler->get_unread($feed_id); - } else { - return 0; - } + return $handler ? $handler->get_unread($feed_id) : 0; } else if ($n_feed == Feeds::FEED_RECENTLY_READ) { return 0; // tags diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index be4254783..518dcad05 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -53,8 +53,7 @@ class Handler_Public extends Handler { $handler = $tmppluginhost->get_feed_handler( PluginHost::feed_to_pfeed_id((int)$feed)); - if ($handler && implements_interface($handler, 'IVirtualFeed')) { - /** @var Plugin&IVirtualFeed $handler */ + if ($handler) { $qfh_ret = $handler->get_headlines(PluginHost::feed_to_pfeed_id((int)$feed), $params); } else { user_error("Failed to find handler for plugin feed ID: $feed", E_USER_ERROR); diff --git a/classes/PluginHost.php b/classes/PluginHost.php index b54e48d4f..608355158 100644 --- a/classes/PluginHost.php +++ b/classes/PluginHost.php @@ -782,12 +782,21 @@ class PluginHost { return $this->feeds[$cat_id] ?? []; } - // convert feed_id (e.g. -129) to pfeed_id first + /** + * convert feed_id (e.g. -129) to pfeed_id first + * + * @return (Plugin&IVirtualFeed)|null + */ function get_feed_handler(int $pfeed_id): ?Plugin { foreach ($this->feeds as $cat) { foreach ($cat as $feed) { if ($feed['id'] == $pfeed_id) { - return $feed['sender']; + if (implements_interface($feed['sender'], 'IVirtualFeed')) { + /** @var Plugin&IVirtualFeed $feed['sender'] */ + return $feed['sender']; + } else { + return null; + } } } } -- cgit v1.2.3-54-g00ecf