A free, self-hostable news aggregator…
PHPstan level 6 for Category.php (#5298)
Luc SANCHEZa19b56064d74
diff --git a/app/Models/Category.php b/app/Models/Category.phpindex b23e8da0a..737544481 100644--- a/app/Models/Category.php+++ b/app/Models/Category.php@@ -4,39 +4,41 @@ class FreshRSS_Category extends Minz_Model {/*** Normal- * @var int*/- const KIND_NORMAL = 0;+ public const KIND_NORMAL = 0;/*** Category tracking a third-party Dynamic OPML- * @var int*/- const KIND_DYNAMIC_OPML = 2;+ public const KIND_DYNAMIC_OPML = 2;- const TTL_DEFAULT = 0;-- /**- * @var int- */+ /** @var int */private $id = 0;/** @var int */private $kind = 0;+ /** @var string */private $name;+ /** @var int */private $nbFeeds = -1;+ /** @var int */private $nbNotRead = -1;/** @var array<FreshRSS_Feed>|null */- private $feeds = null;+ private $feeds;+ /** @var bool|int */private $hasFeedsWithError = false;+ /** @var array<string,mixed> */private $attributes = [];/** @var int */private $lastUpdate = 0;/** @var bool */private $error = false;- public function __construct(string $name = '', $feeds = null) {+ /**+ * @param array<FreshRSS_Feed>|null $feeds+ */+ public function __construct(string $name = '', array $feeds = null) {$this->_name($name);- if (isset($feeds)) {+ if ($feeds !== null) {$this->_feeds($feeds);$this->nbFeeds = 0;$this->nbNotRead = 0;@@ -61,13 +63,15 @@ class FreshRSS_Category extends Minz_Model {public function lastUpdate(): int {return $this->lastUpdate;}- public function _lastUpdate(int $value) {+ public function _lastUpdate(int $value): void {$this->lastUpdate = $value;}public function inError(): bool {return $this->error;}- public function _error($value) {++ /** @param bool|int $value */+ public function _error($value): void {$this->error = (bool)$value;}public function isDefault(): bool {@@ -81,6 +85,11 @@ class FreshRSS_Category extends Minz_Model {return $this->nbFeeds;}++ /**+ * @throws Minz_ConfigurationNamespaceException+ * @throws Minz_PDOConnectionException+ */public function nbNotRead(): int {if ($this->nbNotRead < 0) {$catDAO = FreshRSS_Factory::createCategoryDao();@@ -109,34 +118,39 @@ class FreshRSS_Category extends Minz_Model {return $this->feeds;}- public function hasFeedsWithError() {- return $this->hasFeedsWithError;+ public function hasFeedsWithError(): bool {+ return (bool)($this->hasFeedsWithError);}- public function attributes($key = '') {- if ($key == '') {+ /**+ * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)+ * @return array<string,mixed>|mixed|null+ */+ public function attributes(string $key = '') {+ if ($key === '') {return $this->attributes;} else {- return isset($this->attributes[$key]) ? $this->attributes[$key] : null;+ return $this->attributes[$key] ?? null;}}- public function _id($id) {- $this->id = intval($id);- if ($id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {+ public function _id(int $id): void {+ $this->id = $id;+ if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {$this->_name(_t('gen.short.default_category'));}}- public function _kind(int $kind) {+ public function _kind(int $kind): void {$this->kind = $kind;}- public function _name($value) {+ public function _name(string $value): void {$this->name = mb_strcut(trim($value), 0, 255, 'UTF-8');}+/** @param array<FreshRSS_Feed>|FreshRSS_Feed $values */- public function _feeds($values) {+ public function _feeds($values): void {if (!is_array($values)) {$values = array($values);}@@ -147,9 +161,8 @@ class FreshRSS_Category extends Minz_Model {/*** To manually add feeds to this category (not committing to database).- * @param FreshRSS_Feed $feed*/- public function addFeed($feed) {+ public function addFeed(FreshRSS_Feed $feed): void {if ($this->feeds === null) {$this->feeds = [];}@@ -158,8 +171,9 @@ class FreshRSS_Category extends Minz_Model {$this->sortFeeds();}- public function _attributes($key, $value) {- if ('' == $key) {+ /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */+ public function _attributes(string $key, $value): void {+ if ('' === $key) {if (is_string($value)) {$value = json_decode($value, true);}@@ -173,6 +187,10 @@ class FreshRSS_Category extends Minz_Model {}}+ /**+ * @param array<string> $attributes+ * @throws FreshRSS_Context_Exception+ */public static function cacheFilename(string $url, array $attributes): string {$simplePie = customSimplePie($attributes);$filename = $simplePie->get_cache_filename($url);@@ -247,8 +265,8 @@ class FreshRSS_Category extends Minz_Model {return $ok;}- private function sortFeeds() {- usort($this->feeds, static function ($a, $b) {+ private function sortFeeds(): void {+ usort($this->feeds, static function (FreshRSS_Feed $a, FreshRSS_Feed $b) {return strnatcasecmp($a->name(), $b->name());});}diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.phpindex a67567f33..b103afb4e 100644--- a/app/Models/CategoryDAO.php+++ b/app/Models/CategoryDAO.php@@ -258,14 +258,14 @@ SQL;}}- public function listSortedCategories($prePopulateFeeds = true, $details = false) {+ public function listSortedCategories(bool $prePopulateFeeds = true, bool $details = false) {$categories = $this->listCategories($prePopulateFeeds, $details);if (!is_array($categories)) {return $categories;}- uasort($categories, static function ($a, $b) {+ uasort($categories, static function (FreshRSS_Category $a, FreshRSS_Category $b) {$aPosition = $a->attributes('position');$bPosition = $b->attributes('position');if ($aPosition === $bPosition) {@@ -495,7 +495,7 @@ SQL;$cat->_id($dao['id']);$cat->_kind($dao['kind']);$cat->_lastUpdate($dao['lastUpdate'] ?? 0);- $cat->_error($dao['error'] ?? false);+ $cat->_error($dao['error'] ?? 0);$cat->_attributes('', isset($dao['attributes']) ? $dao['attributes'] : '');$list[$key] = $cat;}diff --git a/app/Models/Entry.php b/app/Models/Entry.phpindex be31387c3..791853aca 100644--- a/app/Models/Entry.php+++ b/app/Models/Entry.php@@ -363,17 +363,17 @@ HTML;/*** @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)- * @return array<string,mixed>|mixed+ * @return array<string,mixed>|mixed|null*/public function attributes(string $key = '') {- if ($key == '') {+ if ($key === '') {return $this->attributes;} else {- return isset($this->attributes[$key]) ? $this->attributes[$key] : null;+ return $this->attributes[$key] ?? null;}}- /** @param string|array<mixed>|bool|int|null $value */+ /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */public function _attributes(string $key, $value): void {if ($key == '') {if (is_string($value)) {diff --git a/app/Models/Feed.php b/app/Models/Feed.phpindex 0b16bc796..447445d46 100644--- a/app/Models/Feed.php+++ b/app/Models/Feed.php@@ -199,12 +199,15 @@ class FreshRSS_Feed extends Minz_Model {return $this->ttl;}- /** @return mixed attribute (if $key is not blank) or array of attributes, not HTML-encoded */- public function attributes($key = '') {- if ($key == '') {+ /**+ * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)+ * @return array<string,mixed>|mixed|null+ */+ public function attributes(string $key = '') {+ if ($key === '') {return $this->attributes;} else {- return isset($this->attributes[$key]) ? $this->attributes[$key] : null;+ return $this->attributes[$key] ?? null;}}@@ -330,7 +333,7 @@ class FreshRSS_Feed extends Minz_Model {$this->mute = $value < self::TTL_DEFAULT;}- /** @param mixed $value Value, not HTML-encoded */+ /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */public function _attributes(string $key, $value) {if ($key == '') {if (is_string($value)) {diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.phpindex 2a123e0db..b3e2648d9 100644--- a/app/Models/FeedDAO.php+++ b/app/Models/FeedDAO.php@@ -395,7 +395,7 @@ SQL;$feeds = self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));- usort($feeds, function ($a, $b) {+ usort($feeds, static function (FreshRSS_Feed $a, FreshRSS_Feed $b) {return strnatcasecmp($a->name(), $b->name());});diff --git a/app/Models/Tag.php b/app/Models/Tag.phpindex c1290d192..d88f0c1c2 100644--- a/app/Models/Tag.php+++ b/app/Models/Tag.php@@ -46,19 +46,18 @@ class FreshRSS_Tag extends Minz_Model {}/**- * @return mixed|string|array<string,mixed>|null+ * @phpstan-return ($key is non-empty-string ? mixed : array<string,mixed>)+ * @return array<string,mixed>|mixed|null*/public function attributes(string $key = '') {- if ($key == '') {+ if ($key === '') {return $this->attributes;} else {return $this->attributes[$key] ?? null;}}- /**- * @param mixed|string|array<string,mixed>|null $value- */+ /** @param string|array<mixed>|bool|int|null $value Value, not HTML-encoded */public function _attributes(string $key, $value = null): void {if ($key == '') {if (is_string($value)) {diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.phpindex 3f12d586c..b3bb6e34f 100644--- a/cli/i18n/I18nData.php+++ b/cli/i18n/I18nData.php@@ -92,7 +92,7 @@ class I18nData {* @return array<string>*/private function getNonReferenceLanguages(): array {- return array_filter(array_keys($this->data), static function ($value) {+ return array_filter(array_keys($this->data), static function (string $value) {return static::REFERENCE_LANGUAGE !== $value;});}@@ -144,7 +144,7 @@ class I18nData {$keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);$parent = $this->getParentKey($key);- return array_values(array_filter($keys, static function ($element) use ($parent) {+ return array_values(array_filter($keys, static function (string $element) use ($parent) {return false !== strpos($element, $parent);}));}diff --git a/tests/phpstan-next.txt b/tests/phpstan-next.txtindex 7a7a412db..510f9b61f 100644--- a/tests/phpstan-next.txt+++ b/tests/phpstan-next.txt@@ -5,7 +5,6 @@# find . -type d -name 'vendor' -prune -o -name '*.php' -exec sh -c 'vendor/bin/phpstan analyse --level 6 --memory-limit 512M {} >/dev/null 2>/dev/null || echo {}' \;./app/install.php-./app/Models/Category.php./app/Models/CategoryDAO.php./app/Models/Feed.php./app/Models/FeedDAO.php