diff options
| author | 2023-02-09 13:52:55 +0100 | |
|---|---|---|
| committer | 2023-02-09 13:52:55 +0100 | |
| commit | b9a62a6aaacf2763c45f503ed5602ba43bedfce0 (patch) | |
| tree | 1cdcd56232be2a8b9f384b98728e9b7a495c83d9 | |
| parent | 64d68a691c031a235631589e8af2f3dc6c7eddf3 (diff) | |
code improvement for phpstan and humans ;) (#5084)
* code improvement for phpstan and humans ;)
* code improvement for phpstan and humans ;)
* code improvement for phpstan and humans ;)
* code improvement for phpstan and humans ;)
* PHPSTAN level 6
* PHPStan level 9
* Avoid return mixed
---------
Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
| -rw-r--r-- | app/Models/Tag.php | 55 | ||||
| -rw-r--r-- | app/Models/TagDAO.php | 40 |
2 files changed, 69 insertions, 26 deletions
diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 589648e26..c1290d192 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -5,40 +5,61 @@ class FreshRSS_Tag extends Minz_Model { * @var int */ private $id = 0; + /** + * @var string + */ private $name; + /** + * @var array<string,mixed> + */ private $attributes = []; + /** + * @var int + */ private $nbEntries = -1; + /** + * @var int + */ private $nbUnread = -1; - public function __construct($name = '') { + public function __construct(string $name = '') { $this->_name($name); } - public function id() { + public function id(): int { return $this->id; } - public function _id($value) { + /** + * @param int|string $value + */ + public function _id($value): void { $this->id = (int)$value; } - public function name() { + public function name(): string { return $this->name; } - public function _name($value) { + public function _name(string $value): void { $this->name = trim($value); } - public function attributes($key = '') { + /** + * @return mixed|string|array<string,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 _attributes($key, $value) { + /** + * @param mixed|string|array<string,mixed>|null $value + */ + public function _attributes(string $key, $value = null): void { if ($key == '') { if (is_string($value)) { $value = json_decode($value, true); @@ -53,27 +74,33 @@ class FreshRSS_Tag extends Minz_Model { } } - public function nbEntries() { + public function nbEntries(): int { if ($this->nbEntries < 0) { $tagDAO = FreshRSS_Factory::createTagDao(); - $this->nbEntries = $tagDAO->countEntries($this->id()); + $this->nbEntries = $tagDAO->countEntries($this->id()) ?: 0; } return $this->nbEntries; } - public function _nbEntries($value) { + /** + * @param string|int $value + */ + public function _nbEntries($value): void { $this->nbEntries = (int)$value; } - public function nbUnread() { + public function nbUnread(): int { if ($this->nbUnread < 0) { $tagDAO = FreshRSS_Factory::createTagDao(); - $this->nbUnread = $tagDAO->countNotRead($this->id()); + $this->nbUnread = $tagDAO->countNotRead($this->id()) ?: 0; } return $this->nbUnread; } - public function _nbUnread($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 f232b2f9f..35123606b 100644 --- a/app/Models/TagDAO.php +++ b/app/Models/TagDAO.php @@ -267,12 +267,13 @@ SQL; return $newestItemUsec; } + /** @return int|false */ public function count() { $sql = 'SELECT COUNT(*) AS count FROM `_tag`'; $stm = $this->pdo->query($sql); if ($stm !== false) { $res = $stm->fetchAll(PDO::FETCH_ASSOC); - return $res[0]['count']; + return (int)$res[0]['count']; } else { $info = $this->pdo->errorInfo(); if ($this->autoUpdateDb($info)) { @@ -283,16 +284,27 @@ SQL; } } - public function countEntries($id) { + /** + * @return int|false + */ + public function countEntries(int $id) { $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` WHERE id_tag=?'; - $stm = $this->pdo->prepare($sql); $values = array($id); - $stm->execute($values); - $res = $stm->fetchAll(PDO::FETCH_ASSOC); - return $res[0]['count']; + if (($stm = $this->pdo->prepare($sql)) !== false && + $stm->execute($values) && + ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) { + return (int)$res[0]['count']; + } else { + $info = is_object($stm) ? $stm->errorInfo() : $this->pdo->errorInfo(); + Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info)); + return false; + } } - public function countNotRead($id = null) { + /** + * @return int|false + */ + public function countNotRead(?int $id = null) { $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` et ' . 'INNER JOIN `_entry` e ON et.id_entry=e.id ' . 'WHERE e.is_read=0'; @@ -303,11 +315,15 @@ SQL; $values = [$id]; } - $stm = $this->pdo->prepare($sql); - - $stm->execute($values); - $res = $stm->fetchAll(PDO::FETCH_ASSOC); - return $res[0]['count']; + if (($stm = $this->pdo->prepare($sql)) !== false && + $stm->execute($values) && + ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) { + return (int)$res[0]['count']; + } else { + $info = is_object($stm) ? $stm->errorInfo() : $this->pdo->errorInfo(); + Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info)); + return false; + } } public function tagEntry($id_tag, $id_entry, $checked = true) { |
