aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGravatar Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> 2023-04-15 01:19:22 +0200
committerGravatar GitHub <noreply@github.com> 2023-04-15 01:19:22 +0200
commita19b56064d74ae871abd52903778e0d5c35af896 (patch)
treefdc0f2cadc8171c466de34d1d30adab01a738c71 /app
parentb8662f88995df2cbae655d4efe8f414eb3872e5e (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')
-rw-r--r--app/Models/Category.php80
-rw-r--r--app/Models/CategoryDAO.php6
-rw-r--r--app/Models/Entry.php8
-rw-r--r--app/Models/Feed.php13
-rw-r--r--app/Models/FeedDAO.php2
-rw-r--r--app/Models/Tag.php9
6 files changed, 69 insertions, 49 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());
});
}
diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php
index 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.php
index 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.php
index 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.php
index 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.php
index 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)) {