diff options
| author | 2023-04-15 01:19:22 +0200 | |
|---|---|---|
| committer | 2023-04-15 01:19:22 +0200 | |
| commit | a19b56064d74ae871abd52903778e0d5c35af896 (patch) | |
| tree | fdc0f2cadc8171c466de34d1d30adab01a738c71 /app/Models/Category.php | |
| parent | b8662f88995df2cbae655d4efe8f414eb3872e5e (diff) | |
PHPstan level 6 for Category.php (#5298)
* PHPstan level 6 for Category.php
* Fix a few things
* Minor fixes
* A few more fixes
---------
Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'app/Models/Category.php')
| -rw-r--r-- | app/Models/Category.php | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/app/Models/Category.php b/app/Models/Category.php index 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()); }); } |
