diff options
| author | 2023-04-17 08:30:21 +0200 | |
|---|---|---|
| committer | 2023-04-17 08:30:21 +0200 | |
| commit | f3760f138dcbaf7a2190336a0378cf1b2190c9f5 (patch) | |
| tree | 6fac8fbf9efd7aa74a8e3970ab70ccf85287b2cd /app/Models | |
| parent | 41fa4e746df8c2e2399ed753b4994ca85cb21358 (diff) | |
Complete PHPStan Level 6 (#5305)
* Complete PHPStan Level 6
Fix https://github.com/FreshRSS/FreshRSS/issues/4112
And initiate PHPStan Level 7
* PHPStan Level 6 for tests
* Use phpstan/phpstan-phpunit
* Update to PHPStan version 1.10
* Fix mixed bug
* Fix mixed return bug
* Fix paginator bug
* Fix FreshRSS_UserConfiguration
* A couple more Minz_Configuration bug fixes
* A few trivial PHPStan Level 7 fixes
* A few more simple PHPStan Level 7
* More files passing PHPStan Level 7
Add interface to replace removed class from https://github.com/FreshRSS/FreshRSS/pull/5251
* A few more PHPStan Level 7 preparations
* A few last details
Diffstat (limited to 'app/Models')
| -rw-r--r-- | app/Models/Auth.php | 6 | ||||
| -rw-r--r-- | app/Models/BooleanSearch.php | 2 | ||||
| -rw-r--r-- | app/Models/Category.php | 2 | ||||
| -rw-r--r-- | app/Models/CategoryDAO.php | 2 | ||||
| -rw-r--r-- | app/Models/Context.php | 9 | ||||
| -rw-r--r-- | app/Models/DatabaseDAO.php | 2 | ||||
| -rw-r--r-- | app/Models/DatabaseDAOSQLite.php | 9 | ||||
| -rw-r--r-- | app/Models/Entry.php | 11 | ||||
| -rw-r--r-- | app/Models/EntryDAO.php | 40 | ||||
| -rw-r--r-- | app/Models/EntryDAOSQLite.php | 36 | ||||
| -rw-r--r-- | app/Models/Factory.php | 12 | ||||
| -rw-r--r-- | app/Models/Feed.php | 109 | ||||
| -rw-r--r-- | app/Models/FeedDAO.php | 8 | ||||
| -rw-r--r-- | app/Models/Search.php | 6 | ||||
| -rw-r--r-- | app/Models/Share.php | 4 | ||||
| -rw-r--r-- | app/Models/StatsDAO.php | 10 | ||||
| -rw-r--r-- | app/Models/StatsDAOPGSQL.php | 5 | ||||
| -rw-r--r-- | app/Models/StatsDAOSQLite.php | 3 | ||||
| -rw-r--r-- | app/Models/Tag.php | 2 | ||||
| -rw-r--r-- | app/Models/TagDAO.php | 4 | ||||
| -rw-r--r-- | app/Models/TagDAOSQLite.php | 2 | ||||
| -rw-r--r-- | app/Models/UserQuery.php | 2 | ||||
| -rw-r--r-- | app/Models/View.php | 4 |
23 files changed, 151 insertions, 139 deletions
diff --git a/app/Models/Auth.php b/app/Models/Auth.php index 8fd06b24d..e868e1b4f 100644 --- a/app/Models/Auth.php +++ b/app/Models/Auth.php @@ -135,9 +135,9 @@ class FreshRSS_Auth { * Returns if current user has access to the given scope. * * @param string $scope general (default) or admin - * @return boolean true if user has corresponding access, false else. + * @return bool true if user has corresponding access, false else. */ - public static function hasAccess($scope = 'general'): bool { + public static function hasAccess(string $scope = 'general'): bool { if (FreshRSS_Context::$user_conf == null) { return false; } @@ -154,7 +154,7 @@ class FreshRSS_Auth { default: $ok = false; } - return $ok; + return (bool)$ok; } /** diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php index 89e6e3ee2..f4979b22b 100644 --- a/app/Models/BooleanSearch.php +++ b/app/Models/BooleanSearch.php @@ -230,7 +230,7 @@ class FreshRSS_BooleanSearch { if ($input == '') { return; } - $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE); + $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE) ?: []; $segment = ''; $ns = count($splits); diff --git a/app/Models/Category.php b/app/Models/Category.php index 737544481..4e28b1741 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -262,7 +262,7 @@ class FreshRSS_Category extends Minz_Model { $catDAO = FreshRSS_Factory::createCategoryDao(); $catDAO->updateLastUpdate($this->id(), !$ok); - return $ok; + return (bool)$ok; } private function sortFeeds(): void { diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php index 64b14ffb7..29593ce5f 100644 --- a/app/Models/CategoryDAO.php +++ b/app/Models/CategoryDAO.php @@ -477,7 +477,7 @@ SQL; $cat->_id($previousLine['c_id']); $cat->_kind($previousLine['c_kind']); $cat->_lastUpdate($previousLine['c_last_update'] ?? 0); - $cat->_error($previousLine['c_error'] ?? false); + $cat->_error($previousLine['c_error'] ?? 0); $cat->_attributes('', $previousLine['c_attributes']); $list[$previousLine['c_id']] = $cat; } diff --git a/app/Models/Context.php b/app/Models/Context.php index b2631291a..03006cbbf 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -110,10 +110,9 @@ 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) { + public static function initUser(string $username = '', bool $userMustExist = true): ?FreshRSS_UserConfiguration { FreshRSS_Context::$user_conf = null; if (!isset($_SESSION)) { Minz_Session::init('FreshRSS'); @@ -145,7 +144,7 @@ final class FreshRSS_Context { Minz_Session::unlock(); if (FreshRSS_Context::$user_conf == null) { - return false; + return null; } FreshRSS_Context::$search = new FreshRSS_BooleanSearch(''); @@ -249,8 +248,8 @@ final class FreshRSS_Context { /** * Return the current get as a string or an array. * - * If $array is true, the first item of the returned value is 'f' or 'c' and - * the second is the id. + * If $array is true, the first item of the returned value is 'f' or 'c' or 't' and the second is the id. + * @phpstan-return ($asArray is true ? array{'c'|'f'|'t',bool|int} : string) * @return string|array{string,bool|int} */ public static function currentGet(bool $asArray = false) { diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php index 28dd36cd9..aeac6f435 100644 --- a/app/Models/DatabaseDAO.php +++ b/app/Models/DatabaseDAO.php @@ -79,7 +79,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { $ok &= in_array($c['name'], $schema); } - return $ok; + return (bool)$ok; } public function categoryIsCorrect(): bool { diff --git a/app/Models/DatabaseDAOSQLite.php b/app/Models/DatabaseDAOSQLite.php index 3fab1134d..0909613a7 100644 --- a/app/Models/DatabaseDAOSQLite.php +++ b/app/Models/DatabaseDAOSQLite.php @@ -8,7 +8,10 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO { public function tablesAreCorrect(): bool { $sql = 'SELECT name FROM sqlite_master WHERE type="table"'; $stm = $this->pdo->query($sql); - $res = $stm->fetchAll(PDO::FETCH_ASSOC); + $res = $stm ? $stm->fetchAll(PDO::FETCH_ASSOC) : false; + if ($res === false) { + return false; + } $tables = array( $this->pdo->prefix() . 'category' => false, @@ -29,7 +32,7 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO { public function getSchema(string $table): array { $sql = 'PRAGMA table_info(' . $table . ')'; $stm = $this->pdo->query($sql); - return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC)); + return $stm ? $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC) ?: []) : []; } public function entryIsCorrect(): bool { @@ -62,7 +65,7 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO { public function size(bool $all = false): int { $sum = 0; if ($all) { - foreach (glob(DATA_PATH . '/users/*/db.sqlite') as $filename) { + foreach (glob(DATA_PATH . '/users/*/db.sqlite') ?: [] as $filename) { $sum += @filesize($filename); } } else { diff --git a/app/Models/Entry.php b/app/Models/Entry.php index d208b07ec..20f17d1c7 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -106,7 +106,7 @@ class FreshRSS_Entry extends Minz_Model { return $this->authors(true); } /** - * @phpstan return ($asString ? string : array<string>) + * @phpstan-return ($asString is true ? string : array<string>) * @return string|array<string> */ public function authors(bool $asString = false) { @@ -285,7 +285,7 @@ HTML; /** * @return array<string,string>|null */ - public function thumbnail(bool $searchEnclosures = true) { + public function thumbnail(bool $searchEnclosures = true): ?array { $thumbnail = $this->attributes('thumbnail'); if (!empty($thumbnail['url'])) { return $thumbnail; @@ -352,7 +352,10 @@ HTML; return $this->feedId; } - /** @return string|array<string> */ + /** + * @phpstan-return ($asString is true ? string : array<string>) + * @return string|array<string> + */ public function tags(bool $asString = false) { if ($asString) { return $this->tags == null ? '' : '#' . implode(' #', $this->tags); @@ -609,7 +612,7 @@ HTML; } } } - return $ok; + return (bool)$ok; } /** @param array<string,int> $titlesAsRead */ diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index 9f24beb7c..1661bfd13 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -308,7 +308,7 @@ SQL; * there is an other way to do that. * * @param string|array<string> $ids - * @return false|integer + * @return int|false */ public function markFavorite($ids, bool $is_favorite = true) { if (!is_array($ids)) { @@ -348,12 +348,8 @@ SQL; * feeds from one category or on all feeds. * * @todo It can use the query builder refactoring to build that query - * - * @param false|integer $catId category ID - * @param false|integer $feedId feed ID - * @return boolean */ - protected function updateCacheUnreads($catId = false, $feedId = false) { + protected function updateCacheUnreads(?int $catId = null, ?int $feedId = null): bool { $sql = 'UPDATE `_feed` f ' . 'LEFT OUTER JOIN (' . 'SELECT e.id_feed, ' @@ -365,13 +361,13 @@ SQL; . 'SET f.`cache_nbUnreads`=COALESCE(x.nbUnreads, 0)'; $hasWhere = false; $values = array(); - if ($feedId !== false) { + if ($feedId != null) { $sql .= ' WHERE'; $hasWhere = true; $sql .= ' f.id=?'; $values[] = $feedId; } - if ($catId !== false) { + if ($catId != null) { $sql .= $hasWhere ? ' AND' : ' WHERE'; $hasWhere = true; $sql .= ' f.category=?'; @@ -397,8 +393,8 @@ SQL; * same if it is an array or not. * * @param string|array<string> $ids - * @param boolean $is_read - * @return integer|false affected rows + * @param bool $is_read + * @return int|false affected rows */ public function markRead($ids, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -431,7 +427,7 @@ SQL; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) { return false; } return $affected; @@ -469,7 +465,7 @@ SQL; * separated. * * @param string $idMax fail safe article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadEntries(string $idMax = '0', bool $onlyFavorites = false, int $priorityMin = 0, ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { @@ -498,7 +494,7 @@ SQL; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) { return false; } return $affected; @@ -511,9 +507,9 @@ SQL; * * If $idMax equals 0, a deprecated debug message is logged * - * @param integer $id category ID + * @param int $id category ID * @param string $idMax fail safe article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadCat(int $id, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -536,7 +532,7 @@ SQL; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads($id, null))) { return false; } return $affected; @@ -549,9 +545,9 @@ SQL; * * If $idMax equals 0, a deprecated debug message is logged * - * @param integer $id_feed feed ID + * @param int $id_feed feed ID * @param string $idMax fail safe article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadFeed(int $id_feed, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -597,9 +593,9 @@ SQL; /** * Mark all the articles in a tag as read. - * @param integer $id tag ID, or empty for targeting any tag + * @param int $id tag ID, or empty for targeting any tag * @param string $idMax max article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadTag(int $id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { @@ -630,7 +626,7 @@ SQL; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) { return false; } return $affected; @@ -758,7 +754,7 @@ SQL; } /** @return array{0:array<int|string>,1:string} */ - public static function sqlBooleanSearch(string $alias, FreshRSS_BooleanSearch $filters, int $level = 0) { + public static function sqlBooleanSearch(string $alias, FreshRSS_BooleanSearch $filters, int $level = 0): array { $search = ''; $values = []; diff --git a/app/Models/EntryDAOSQLite.php b/app/Models/EntryDAOSQLite.php index e509097f2..c86791372 100644 --- a/app/Models/EntryDAOSQLite.php +++ b/app/Models/EntryDAOSQLite.php @@ -25,7 +25,7 @@ class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO { /** @param array<string> $errorInfo */ protected function autoUpdateDb(array $errorInfo): bool { if ($tableInfo = $this->pdo->query("PRAGMA table_info('entry')")) { - $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1); + $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1) ?: []; foreach (['attributes'] as $column) { if (!in_array($column, $columns)) { return $this->addColumn($column); @@ -34,14 +34,14 @@ class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO { } if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) { $showCreate = $tableInfo->fetchColumn(); - if (stripos($showCreate, 'tag') === false) { + if (is_string($showCreate) && stripos($showCreate, 'tag') === false) { $tagDAO = FreshRSS_Factory::createTagDao(); return $tagDAO->createTagTable(); //v1.12.0 } } if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='entrytmp'")) { $showCreate = $tableInfo->fetchColumn(); - if (stripos($showCreate, 'entrytmp') === false) { + if (is_string($showCreate) && stripos($showCreate, 'entrytmp') === false) { return $this->createEntryTempTable(); //v1.7.0 } } @@ -78,20 +78,20 @@ DROP TABLE IF EXISTS `tmp`; return $result; } - protected function updateCacheUnreads($catId = false, $feedId = false) { + protected function updateCacheUnreads(?int $catId = null, ?int $feedId = null): bool { $sql = 'UPDATE `_feed` ' . 'SET `cache_nbUnreads`=(' . 'SELECT COUNT(*) AS nbUnreads FROM `_entry` e ' . 'WHERE e.id_feed=`_feed`.id AND e.is_read=0)'; $hasWhere = false; $values = array(); - if ($feedId !== false) { + if ($feedId != null) { $sql .= ' WHERE'; $hasWhere = true; $sql .= ' id=?'; $values[] = $feedId; } - if ($catId !== false) { + if ($catId != null) { $sql .= $hasWhere ? ' AND' : ' WHERE'; $hasWhere = true; $sql .= ' category=?'; @@ -117,8 +117,8 @@ DROP TABLE IF EXISTS `tmp`; * same if it is an array or not. * * @param string|array<string> $ids - * @param boolean $is_read - * @return integer|false affected rows + * @param bool $is_read + * @return int|false affected rows */ public function markRead($ids, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -176,9 +176,9 @@ DROP TABLE IF EXISTS `tmp`; * separated. * * @param string $idMax fail safe article ID - * @param boolean $onlyFavorites - * @param integer $priorityMin - * @return integer|false affected rows + * @param bool $onlyFavorites + * @param int $priorityMin + * @return int|false affected rows */ public function markReadEntries(string $idMax = '0', bool $onlyFavorites = false, int $priorityMin = 0, ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { @@ -205,7 +205,7 @@ DROP TABLE IF EXISTS `tmp`; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) { return false; } return $affected; @@ -218,9 +218,9 @@ DROP TABLE IF EXISTS `tmp`; * * If $idMax equals 0, a deprecated debug message is logged * - * @param integer $id category ID + * @param int $id category ID * @param string $idMax fail safe article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadCat(int $id, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -244,7 +244,7 @@ DROP TABLE IF EXISTS `tmp`; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads($id, null))) { return false; } return $affected; @@ -252,9 +252,9 @@ DROP TABLE IF EXISTS `tmp`; /** * Mark all the articles in a tag as read. - * @param integer $id tag ID, or empty for targeting any tag + * @param int $id tag ID, or empty for targeting any tag * @param string $idMax max article ID - * @return integer|false affected rows + * @return int|false affected rows */ public function markReadTag($id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true) { FreshRSS_UserDAO::touch(); @@ -283,7 +283,7 @@ DROP TABLE IF EXISTS `tmp`; return false; } $affected = $stm->rowCount(); - if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) { + if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) { return false; } return $affected; diff --git a/app/Models/Factory.php b/app/Models/Factory.php index 80c611db5..d06c3c63d 100644 --- a/app/Models/Factory.php +++ b/app/Models/Factory.php @@ -13,7 +13,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createCategoryDao(?string $username = null): FreshRSS_CategoryDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_CategoryDAOSQLite($username); default: @@ -25,7 +25,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createFeedDao(?string $username = null): FreshRSS_FeedDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_FeedDAOSQLite($username); default: @@ -37,7 +37,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createEntryDao(?string $username = null): FreshRSS_EntryDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_EntryDAOSQLite($username); case 'pgsql': @@ -51,7 +51,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createTagDao(?string $username = null): FreshRSS_TagDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_TagDAOSQLite($username); case 'pgsql': @@ -65,7 +65,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createStatsDAO(?string $username = null): FreshRSS_StatsDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_StatsDAOSQLite($username); case 'pgsql': @@ -79,7 +79,7 @@ class FreshRSS_Factory { * @throws Minz_ConfigurationNamespaceException|Minz_PDOConnectionException */ public static function createDatabaseDAO(?string $username = null): FreshRSS_DatabaseDAO { - switch (FreshRSS_Context::$system_conf->db['type']) { + switch (FreshRSS_Context::$system_conf->db['type'] ?? '') { case 'sqlite': return new FreshRSS_DatabaseDAOSQLite($username); case 'pgsql': diff --git a/app/Models/Feed.php b/app/Models/Feed.php index 447445d46..ecf875723 100644 --- a/app/Models/Feed.php +++ b/app/Models/Feed.php @@ -71,6 +71,7 @@ class FreshRSS_Feed extends Minz_Model { private $error = false; /** @var int */ private $ttl = self::TTL_DEFAULT; + /** @var array<string,mixed> */ private $attributes = []; /** @var bool */ private $mute = false; @@ -93,10 +94,7 @@ class FreshRSS_Feed extends Minz_Model { } } - /** - * @return FreshRSS_Feed - */ - public static function example() { + public static function example(): FreshRSS_Feed { $f = new FreshRSS_Feed('http://example.net/', false); $f->faviconPrepare(); return $f; @@ -142,12 +140,16 @@ class FreshRSS_Feed extends Minz_Model { return $this->categoryId; } - public function entries() { + /** + * @return array<FreshRSS_Entry>|null + * @deprecated + */ + public function entries(): ?array { Minz_Log::warning(__method__ . ' is deprecated since FreshRSS 1.16.1!'); $simplePie = $this->load(false, true); return $simplePie == null ? [] : iterator_to_array($this->loadEntries($simplePie)); } - public function name($raw = false): string { + public function name(bool $raw = false): string { return $raw || $this->name != '' ? $this->name : preg_replace('%^https?://(www[.])?%i', '', $this->url); } /** @return string HTML-encoded URL of the Web site of the feed */ @@ -167,7 +169,11 @@ class FreshRSS_Feed extends Minz_Model { public function pathEntries(): string { return $this->pathEntries; } - public function httpAuth($raw = true) { + /** + * @phpstan-return ($raw is true ? string : array{'username':string,'password':string}) + * @return array{'username':string,'password':string}|string + */ + public function httpAuth(bool $raw = true) { if ($raw) { return $this->httpAuth; } else { @@ -223,7 +229,7 @@ class FreshRSS_Feed extends Minz_Model { return $this->nbEntries; } - public function nbNotRead($includePending = false): int { + public function nbNotRead(bool $includePending = false): int { if ($this->nbNotRead < 0) { $feedDAO = FreshRSS_Factory::createFeedDao(); $this->nbNotRead = $feedDAO->countNotRead($this->id()); @@ -231,7 +237,7 @@ class FreshRSS_Feed extends Minz_Model { return $this->nbNotRead + ($includePending ? $this->nbPendingNotRead : 0); } - public function faviconPrepare() { + public function faviconPrepare(): void { require_once(LIB_PATH . '/favicons.php'); $url = $this->website; if ($url == '') { @@ -253,7 +259,7 @@ class FreshRSS_Feed extends Minz_Model { } } } - public static function faviconDelete($hash) { + public static function faviconDelete(string $hash): void { $path = DATA_PATH . '/favicons/' . $hash; @unlink($path . '.ico'); @unlink($path . '.txt'); @@ -262,10 +268,11 @@ class FreshRSS_Feed extends Minz_Model { return Minz_Url::display('/f.php?' . $this->hash()); } - public function _id($value) { - $this->id = intval($value); + public function _id(int $value): void { + $this->id = $value; } - public function _url(string $value, bool $validate = true) { + + public function _url(string $value, bool $validate = true): void { $this->hash = ''; $url = $value; if ($validate) { @@ -276,26 +283,26 @@ class FreshRSS_Feed extends Minz_Model { } $this->url = $url; } - public function _kind(int $value) { + + public function _kind(int $value): void { $this->kind = $value; } - /** @param FreshRSS_Category|null $cat */ - public function _category($cat) { + public function _category(?FreshRSS_Category $cat): void { $this->category = $cat; $this->categoryId = $this->category == null ? 0 : $this->category->id(); } /** @param int|string $id */ - public function _categoryId($id) { + public function _categoryId($id): void { $this->category = null; $this->categoryId = intval($id); } - public function _name(string $value) { + public function _name(string $value): void { $this->name = $value == '' ? '' : trim($value); } - public function _website(string $value, bool $validate = true) { + public function _website(string $value, bool $validate = true): void { if ($validate) { $value = checkUrl($value); } @@ -304,37 +311,37 @@ class FreshRSS_Feed extends Minz_Model { } $this->website = $value; } - public function _description(string $value) { + public function _description(string $value): void { $this->description = $value == '' ? '' : $value; } - public function _lastUpdate($value) { - $this->lastUpdate = intval($value); + public function _lastUpdate(int $value): void { + $this->lastUpdate = $value; } - public function _priority($value) { - $this->priority = intval($value); + public function _priority(int $value): void { + $this->priority = $value; } /** @param string $value HTML-encoded CSS selector */ - public function _pathEntries(string $value) { + public function _pathEntries(string $value): void { $this->pathEntries = $value; } - public function _httpAuth(string $value) { + public function _httpAuth(string $value): void { $this->httpAuth = $value; } - public function _error($value) { + /** @param bool|int $value */ + public function _error($value): void { $this->error = (bool)$value; } - public function _mute(bool $value) { + public function _mute(bool $value): void { $this->mute = $value; } - public function _ttl($value) { - $value = intval($value); + public function _ttl(int $value): void { $value = min($value, 100000000); $this->ttl = abs($value); $this->mute = $value < self::TTL_DEFAULT; } /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */ - public function _attributes(string $key, $value) { + public function _attributes(string $key, $value): void { if ($key == '') { if (is_string($value)) { $value = json_decode($value, true); @@ -349,17 +356,14 @@ class FreshRSS_Feed extends Minz_Model { } } - public function _nbNotRead($value) { - $this->nbNotRead = intval($value); + public function _nbNotRead(int $value): void { + $this->nbNotRead = $value; } - public function _nbEntries($value) { - $this->nbEntries = intval($value); + public function _nbEntries(int $value): void { + $this->nbEntries = $value; } - /** - * @return SimplePie|null - */ - public function load(bool $loadDetails = false, bool $noCache = false) { + public function load(bool $loadDetails = false, bool $noCache = false): ?SimplePie { if ($this->url != '') { // @phpstan-ignore-next-line if (CACHE_PATH === false) { @@ -440,7 +444,7 @@ class FreshRSS_Feed extends Minz_Model { /** * @return array<string> */ - public function loadGuids(SimplePie $simplePie) { + public function loadGuids(SimplePie $simplePie): array { $hasUniqueGuids = true; $testGuids = []; $guids = []; @@ -474,6 +478,9 @@ class FreshRSS_Feed extends Minz_Model { return $guids; } + /** + * @return iterable<FreshRSS_Entry> + */ public function loadEntries(SimplePie $simplePie) { $hasBadGuids = $this->attributes('hasBadGuids'); @@ -591,10 +598,7 @@ class FreshRSS_Feed extends Minz_Model { } } - /** - * @return SimplePie|null - */ - public function loadHtmlXpath() { + public function loadHtmlXpath(): ?SimplePie { if ($this->url == '') { return null; } @@ -708,7 +712,7 @@ class FreshRSS_Feed extends Minz_Model { if ($item['title'] != '' || $item['content'] != '' || $item['link'] != '') { // HTML-encoding/escaping of the relevant fields (all except 'content') foreach (['author', 'categories', 'guid', 'link', 'thumbnail', 'timestamp', 'title'] as $key) { - if (!empty($item[$key])) { + if (!empty($item[$key]) && is_string($item[$key])) { $item[$key] = Minz_Helper::htmlspecialchars_utf8($item[$key]); } } @@ -731,7 +735,7 @@ class FreshRSS_Feed extends Minz_Model { /** * To keep track of some new potentially unread articles since last commit+fetch from database */ - public function incPendingUnread(int $n = 1) { + public function incPendingUnread(int $n = 1): void { $this->nbPendingNotRead += $n; } @@ -770,6 +774,7 @@ class FreshRSS_Feed extends Minz_Model { /** * Remember to call updateCachedValue($id_feed) or updateCachedValues() just after + * @return int|false */ public function cleanOldEntries() { $archiving = $this->attributes('archiving'); @@ -785,7 +790,6 @@ class FreshRSS_Feed extends Minz_Model { $entryDAO = FreshRSS_Factory::createEntryDao(); $nb = $entryDAO->cleanOldEntries($this->id(), $archiving); if ($nb > 0) { - $needFeedCacheRefresh = true; Minz_Log::debug($nb . ' entries cleaned in feed [' . $this->url(false) . '] with: ' . json_encode($archiving)); } return $nb; @@ -793,6 +797,7 @@ class FreshRSS_Feed extends Minz_Model { return false; } + /** @param array<string,mixed> $attributes */ public static function cacheFilename(string $url, array $attributes, int $kind = FreshRSS_Feed::KIND_RSS): string { $simplePie = customSimplePie($attributes); $filename = $simplePie->get_cache_filename($url); @@ -851,12 +856,12 @@ class FreshRSS_Feed extends Minz_Model { } /** - * @param array<FreshRSS_FilterAction> $filterActions + * @param array<FreshRSS_FilterAction>|null $filterActions */ - private function _filterActions($filterActions) { + private function _filterActions(?array $filterActions): void { $this->filterActions = $filterActions; if (is_array($this->filterActions) && !empty($this->filterActions)) { - $this->_attributes('filters', array_map(function ($af) { + $this->_attributes('filters', array_map(static function (?FreshRSS_FilterAction $af) { return $af == null ? null : $af->toJSON(); }, $this->filterActions)); } else { @@ -885,10 +890,10 @@ class FreshRSS_Feed extends Minz_Model { /** * @param array<string> $filters */ - public function _filtersAction(string $action, $filters) { + public function _filtersAction(string $action, array $filters): void { $action = trim($action); if ($action == '' || !is_array($filters)) { - return false; + return; } $filters = array_unique(array_map('trim', $filters)); $filterActions = $this->filterActions(); diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php index 9138f3d59..837fef7f1 100644 --- a/app/Models/FeedDAO.php +++ b/app/Models/FeedDAO.php @@ -268,7 +268,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo { * @param bool|null $muted to include only muted feeds * @return int|false */ - public function deleteFeedByCategory(int $id, $muted = null) { + public function deleteFeedByCategory(int $id, ?bool $muted = null) { $sql = 'DELETE FROM `_feed` WHERE category=?'; if ($muted) { $sql .= ' AND ttl < 0'; @@ -338,7 +338,7 @@ SQL; } /** @return array<string,string> */ - public function listFeedsNewestItemUsec(?int $id_feed = null) { + public function listFeedsNewestItemUsec(?int $id_feed = null): array { $sql = 'SELECT id_feed, MAX(id) as newest_item_us FROM `_entry` '; if ($id_feed === null) { $sql .= 'GROUP BY id_feed'; @@ -358,7 +358,7 @@ SQL; * Use $defaultCacheDuration == -1 to return all feeds, without filtering them by TTL. * @return array<FreshRSS_Feed> */ - public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0) { + public function listFeedsOrderUpdate(int $defaultCacheDuration = 3600, int $limit = 0): array { $this->updateTTL(); $sql = 'SELECT id, url, kind, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, ttl, attributes ' . 'FROM `_feed` ' @@ -398,7 +398,7 @@ SQL; * @param bool|null $muted to include only muted feeds * @return array<FreshRSS_Feed> */ - public function listByCategory(int $cat, $muted = null): array { + public function listByCategory(int $cat, ?bool $muted = null): array { $sql = 'SELECT * FROM `_feed` WHERE category=?'; if ($muted) { $sql .= ' AND ttl < 0'; diff --git a/app/Models/Search.php b/app/Models/Search.php index 14d77cb6f..f5b061512 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -234,11 +234,11 @@ class FreshRSS_Search { } /** - * @param array<string> $anArray + * @param array<string>|null $anArray * @return array<string> */ - private static function removeEmptyValues($anArray): array { - return empty($anArray) ? [] : array_filter($anArray, function($value) { return $value !== ''; }); + private static function removeEmptyValues(?array $anArray): array { + return empty($anArray) ? [] : array_filter($anArray, static function(string $value) { return $value !== ''; }); } /** diff --git a/app/Models/Share.php b/app/Models/Share.php index 7bd6de9cf..b01d285f3 100644 --- a/app/Models/Share.php +++ b/app/Models/Share.php @@ -49,7 +49,7 @@ class FreshRSS_Share { self::register($share_options); } - uasort(self::$list_sharing, function ($a, $b) { + uasort(self::$list_sharing, static function (FreshRSS_Share $a, FreshRSS_Share $b) { return strcasecmp($a->name(), $b->name()); }); } @@ -303,7 +303,7 @@ class FreshRSS_Share { * @param array<string> $transform an array containing a list of functions to apply. * @return string the transformed data. */ - private static function transform(string $data, $transform): string { + private static function transform(string $data, array $transform): string { if (!is_array($transform) || empty($transform)) { return $data; } diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php index 30d7aa2ef..a0a4ec498 100644 --- a/app/Models/StatsDAO.php +++ b/app/Models/StatsDAO.php @@ -13,7 +13,7 @@ class FreshRSS_StatsDAO extends Minz_ModelPdo { * * @return array{'main_stream':array{'total':int,'count_unreads':int,'count_reads':int,'count_favorites':int},'all_feeds':array{'total':int,'count_unreads':int,'count_reads':int,'count_favorites':int}} */ - public function calculateEntryRepartition() { + public function calculateEntryRepartition(): array { return array( 'main_stream' => $this->calculateEntryRepartitionPerFeed(null, true), 'all_feeds' => $this->calculateEntryRepartitionPerFeed(null, false), @@ -57,7 +57,7 @@ SQL; * Calculates entry count per day on a 30 days period. * @return array<int,int> */ - public function calculateEntryCount() { + public function calculateEntryCount(): array { $count = $this->initEntryCountArray(); $midnight = mktime(0, 0, 0); $oldest = $midnight - (self::ENTRY_COUNT_PERIOD * 86400); @@ -87,7 +87,7 @@ SQL; * Initialize an array for the entry count. * @return array<int,int> */ - protected function initEntryCountArray() { + protected function initEntryCountArray(): array { return $this->initStatsArray(-self::ENTRY_COUNT_PERIOD, -1); } @@ -348,8 +348,8 @@ SQL; * @param array<string> $data * @return array<string> */ - private function convertToTranslatedJson(array $data = array()) { - $translated = array_map(function($a) { + private function convertToTranslatedJson(array $data = array()): array { + $translated = array_map(static function (string $a) { return _t('gen.date.' . $a); }, $data); diff --git a/app/Models/StatsDAOPGSQL.php b/app/Models/StatsDAOPGSQL.php index 9548027ce..52a99d2f4 100644 --- a/app/Models/StatsDAOPGSQL.php +++ b/app/Models/StatsDAOPGSQL.php @@ -5,7 +5,7 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO { /** * Calculates the number of article per hour of the day per feed * - * @param integer $feed id + * @param int $feed id * @return array<int,int> */ public function calculateEntryRepartitionPerFeedPerHour(?int $feed = null): array { @@ -48,6 +48,9 @@ ORDER BY period ASC SQL; $stm = $this->pdo->query($sql); + if ($stm === false) { + return []; + } $res = $stm->fetchAll(PDO::FETCH_NAMED); switch ($period) { diff --git a/app/Models/StatsDAOSQLite.php b/app/Models/StatsDAOSQLite.php index 632fa17e2..9f292aae6 100644 --- a/app/Models/StatsDAOSQLite.php +++ b/app/Models/StatsDAOSQLite.php @@ -25,6 +25,9 @@ ORDER BY period ASC SQL; $stm = $this->pdo->query($sql); + if ($stm === false) { + return []; + } $res = $stm->fetchAll(PDO::FETCH_NAMED); switch ($period) { diff --git a/app/Models/Tag.php b/app/Models/Tag.php index d88f0c1c2..4ab28a286 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -97,7 +97,7 @@ class FreshRSS_Tag extends Minz_Model { } /** - * @param string|int$value + * @param string|int $value */ public function _nbUnread($value): void { $this->nbUnread = (int)$value; diff --git a/app/Models/TagDAO.php b/app/Models/TagDAO.php index c27a69f11..0f491e706 100644 --- a/app/Models/TagDAO.php +++ b/app/Models/TagDAO.php @@ -245,7 +245,7 @@ SQL; } /** @return array<string,string> */ - public function listTagsNewestItemUsec(?int $id_tag = null) { + public function listTagsNewestItemUsec(?int $id_tag = null): array { $sql = 'SELECT t.id AS id_tag, MAX(e.id) AS newest_item_us ' . 'FROM `_tag` t ' . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id ' @@ -440,7 +440,7 @@ SQL; * @param array<array<string,string|int>>|array<string,string|int> $listDAO * @return array<FreshRSS_Tag> */ - private static function daoToTag(array $listDAO) { + private static function daoToTag(array $listDAO): array { $list = array(); if (!is_array($listDAO)) { $listDAO = array($listDAO); diff --git a/app/Models/TagDAOSQLite.php b/app/Models/TagDAOSQLite.php index 910455546..50683cf84 100644 --- a/app/Models/TagDAOSQLite.php +++ b/app/Models/TagDAOSQLite.php @@ -10,7 +10,7 @@ class FreshRSS_TagDAOSQLite extends FreshRSS_TagDAO { protected function autoUpdateDb(array $errorInfo): bool { if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) { $showCreate = $tableInfo->fetchColumn(); - if (stripos($showCreate, 'tag') === false) { + if (is_string($showCreate) && stripos($showCreate, 'tag') === false) { return $this->createTagTable(); //v1.12.0 } } diff --git a/app/Models/UserQuery.php b/app/Models/UserQuery.php index 278074362..f0d809378 100644 --- a/app/Models/UserQuery.php +++ b/app/Models/UserQuery.php @@ -34,7 +34,7 @@ class FreshRSS_UserQuery { private $tag_dao; /** - * @param array<string,string> $query + * @param array<string,string|int> $query */ public function __construct(array $query, FreshRSS_FeedDAO $feed_dao = null, FreshRSS_CategoryDAO $category_dao = null, FreshRSS_TagDAO $tag_dao = null) { $this->category_dao = $category_dao; diff --git a/app/Models/View.php b/app/Models/View.php index e908bdfad..87302a4e1 100644 --- a/app/Models/View.php +++ b/app/Models/View.php @@ -57,7 +57,7 @@ class FreshRSS_View extends Minz_View { public $show_email_field; /** @var string */ public $username; - /** @var array<array{'last_user_activity':int, 'language':string,'enabled':bool,'is_admin':bool, 'enabled':bool, 'article_count':int, 'database_size':int, 'last_user_activity', 'mail_login':string, 'feed_count':int, 'is_default':bool}> */ + /** @var array<array{'last_user_activity':int,'language':string,'enabled':bool,'is_admin':bool,'enabled':bool,'article_count':int,'database_size':int,'last_user_activity','mail_login':string,'feed_count':int,'is_default':bool}> */ public $users; // Updates @@ -73,7 +73,7 @@ class FreshRSS_View extends Minz_View { public $status_database; // Archiving - /** @var int|false */ + /** @var int */ public $nb_total; /** @var int */ public $size_total; |
