Fix types hint in UserQuery.php (#5045)

* Fix types hint in UserQuery.php
In particular, we had a mix of strings and integers for the query state
Pass PHPStan level 9

* Unneeded null check
This commit is contained in:
Alexandre Alapetite
2023-01-28 13:37:09 +01:00
committed by GitHub
parent d8744a9ec1
commit e617830e96
2 changed files with 46 additions and 47 deletions

View File

@@ -8,27 +8,35 @@
*/
class FreshRSS_UserQuery {
/** @var bool */
private $deprecated = false;
private $get;
private $get_name;
private $get_type;
private $name;
private $order;
/** @var string */
private $get = '';
/** @var string */
private $get_name = '';
/** @var string */
private $get_type = '';
/** @var string */
private $name = '';
/** @var string */
private $order = '';
/** @var FreshRSS_BooleanSearch */
private $search;
private $state;
private $url;
/** @var FreshRSS_FeedDAO */
/** @var int */
private $state = 0;
/** @var string */
private $url = '';
/** @var FreshRSS_FeedDAO|null */
private $feed_dao;
/** @var FreshRSS_CategoryDAO */
/** @var FreshRSS_CategoryDAO|null */
private $category_dao;
/** @var FreshRSS_TagDAO */
/** @var FreshRSS_TagDAO|null */
private $tag_dao;
/**
* @param array<string,string> $query
*/
public function __construct($query, FreshRSS_FeedDAO $feed_dao = null, FreshRSS_CategoryDAO $category_dao = null, FreshRSS_TagDAO $tag_dao = null) {
public function __construct(array $query, FreshRSS_FeedDAO $feed_dao = null, FreshRSS_CategoryDAO $category_dao = null, FreshRSS_TagDAO $tag_dao = null) {
$this->category_dao = $category_dao;
$this->feed_dao = $feed_dao;
$this->tag_dao = $tag_dao;
@@ -54,17 +62,17 @@ class FreshRSS_UserQuery {
}
// linked too deeply with the search object, need to use dependency injection
$this->search = new FreshRSS_BooleanSearch($query['search']);
if (isset($query['state'])) {
$this->state = $query['state'];
if (!empty($query['state'])) {
$this->state = intval($query['state']);
}
}
/**
* Convert the current object to an array.
*
* @return array<string,string>
* @return array<string,string|int>
*/
public function toArray() {
public function toArray(): array {
return array_filter(array(
'get' => $this->get,
'name' => $this->name,
@@ -76,12 +84,9 @@ class FreshRSS_UserQuery {
}
/**
* Parse the get parameter in the query string to extract its name and
* type
*
* @param string $get
* Parse the get parameter in the query string to extract its name and type
*/
private function parseGet($get) {
private function parseGet(string $get): void {
$this->get = $get;
if (preg_match('/(?P<type>[acfst])(_(?P<id>\d+))?/', $get, $matches)) {
$id = intval($matches['id'] ?? '0');
@@ -108,7 +113,7 @@ class FreshRSS_UserQuery {
/**
* Parse the query string when it is an "all" query
*/
private function parseAll() {
private function parseAll(): void {
$this->get_name = 'all';
$this->get_type = 'all';
}
@@ -118,7 +123,7 @@ class FreshRSS_UserQuery {
*
* @throws FreshRSS_DAO_Exception
*/
private function parseCategory(int $id) {
private function parseCategory(int $id): void {
if ($this->category_dao === null) {
throw new FreshRSS_DAO_Exception('Category DAO is not loaded in UserQuery');
}
@@ -136,7 +141,7 @@ class FreshRSS_UserQuery {
*
* @throws FreshRSS_DAO_Exception
*/
private function parseFeed(int $id) {
private function parseFeed(int $id): void {
if ($this->feed_dao === null) {
throw new FreshRSS_DAO_Exception('Feed DAO is not loaded in UserQuery');
}
@@ -154,7 +159,7 @@ class FreshRSS_UserQuery {
*
* @throws FreshRSS_DAO_Exception
*/
private function parseTag(int $id) {
private function parseTag(int $id): void {
if ($this->tag_dao == null) {
throw new FreshRSS_DAO_Exception('Tag DAO is not loaded in UserQuery');
}
@@ -170,7 +175,7 @@ class FreshRSS_UserQuery {
/**
* Parse the query string when it is a "favorite" query
*/
private function parseFavorite() {
private function parseFavorite(): void {
$this->get_name = 'favorite';
$this->get_type = 'favorite';
}
@@ -179,20 +184,16 @@ class FreshRSS_UserQuery {
* Check if the current user query is deprecated.
* It is deprecated if the category or the feed used in the query are
* not existing.
*
* @return boolean
*/
public function isDeprecated() {
public function isDeprecated(): bool {
return $this->deprecated;
}
/**
* Check if the user query has parameters.
* If the type is 'all', it is considered equal to no parameters
*
* @return boolean
*/
public function hasParameters() {
public function hasParameters(): bool {
if ($this->get_type === 'all') {
return false;
}
@@ -213,42 +214,40 @@ class FreshRSS_UserQuery {
/**
* Check if there is a search in the search object
*
* @return boolean
*/
public function hasSearch() {
return $this->search->getRawInput() != "";
public function hasSearch(): bool {
return $this->search->getRawInput() !== '';
}
public function getGet() {
public function getGet(): string {
return $this->get;
}
public function getGetName() {
public function getGetName(): string {
return $this->get_name;
}
public function getGetType() {
public function getGetType(): string {
return $this->get_type;
}
public function getName() {
public function getName(): string {
return $this->name;
}
public function getOrder() {
public function getOrder(): string {
return $this->order;
}
public function getSearch() {
public function getSearch(): FreshRSS_BooleanSearch {
return $this->search;
}
public function getState() {
public function getState(): int {
return $this->state;
}
public function getUrl() {
public function getUrl(): string {
return $this->url;
}