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>
This commit is contained in:
Luc SANCHEZ
2023-02-09 13:52:55 +01:00
committed by GitHub
parent 64d68a691c
commit b9a62a6aaa
2 changed files with 69 additions and 26 deletions

View File

@@ -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;
}
}

View File

@@ -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) {