From 1d9d4e3e3c8dd020ab4d333436264eaa3ef201cd Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 9 Jan 2023 12:59:30 +0100 Subject: Update dev dependencies (#4993) Related to https://github.com/FreshRSS/FreshRSS/pull/4991 Required a few changes in code to pass the tests --- app/Models/UserQuery.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'app/Models/UserQuery.php') diff --git a/app/Models/UserQuery.php b/app/Models/UserQuery.php index 964324bf7..4c7e2a8f7 100644 --- a/app/Models/UserQuery.php +++ b/app/Models/UserQuery.php @@ -18,16 +18,17 @@ class FreshRSS_UserQuery { private $search; private $state; private $url; + /** @var FreshRSS_FeedDAO */ private $feed_dao; + /** @var FreshRSS_CategoryDAO */ private $category_dao; + /** @var FreshRSS_TagDAO */ private $tag_dao; /** * @param array $query - * @param FreshRSS_Searchable $feed_dao - * @param FreshRSS_Searchable $category_dao */ - public function __construct($query, FreshRSS_Searchable $feed_dao = null, FreshRSS_Searchable $category_dao = null, FreshRSS_Searchable $tag_dao = null) { + public function __construct($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; @@ -83,21 +84,22 @@ class FreshRSS_UserQuery { private function parseGet($get) { $this->get = $get; if (preg_match('/(?P[acfst])(_(?P\d+))?/', $get, $matches)) { + $id = intval($matches['id'] ?? '0'); switch ($matches['type']) { case 'a': $this->parseAll(); break; case 'c': - $this->parseCategory($matches['id']); + $this->parseCategory($id); break; case 'f': - $this->parseFeed($matches['id']); + $this->parseFeed($id); break; case 's': $this->parseFavorite(); break; case 't': - $this->parseTag($matches['id']); + $this->parseTag($id); break; } } @@ -114,11 +116,10 @@ class FreshRSS_UserQuery { /** * Parse the query string when it is a "category" query * - * @param integer $id * @throws FreshRSS_DAO_Exception */ - private function parseCategory($id) { - if (is_null($this->category_dao)) { + private function parseCategory(int $id) { + if ($this->category_dao === null) { throw new FreshRSS_DAO_Exception('Category DAO is not loaded in UserQuery'); } $category = $this->category_dao->searchById($id); @@ -133,11 +134,10 @@ class FreshRSS_UserQuery { /** * Parse the query string when it is a "feed" query * - * @param integer $id * @throws FreshRSS_DAO_Exception */ - private function parseFeed($id) { - if (is_null($this->feed_dao)) { + private function parseFeed(int $id) { + if ($this->feed_dao === null) { throw new FreshRSS_DAO_Exception('Feed DAO is not loaded in UserQuery'); } $feed = $this->feed_dao->searchById($id); @@ -152,10 +152,9 @@ class FreshRSS_UserQuery { /** * Parse the query string when it is a "tag" query * - * @param integer $id * @throws FreshRSS_DAO_Exception */ - private function parseTag($id) { + private function parseTag(int $id) { if ($this->tag_dao == null) { throw new FreshRSS_DAO_Exception('Tag DAO is not loaded in UserQuery'); } -- cgit v1.2.3 From e617830e966862006e7f64eb83a733deb4549e29 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 28 Jan 2023 13:37:09 +0100 Subject: 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 --- app/Models/UserQuery.php | 85 +++++++++++++++++++------------------- tests/app/Models/UserQueryTest.php | 8 ++-- 2 files changed, 46 insertions(+), 47 deletions(-) (limited to 'app/Models/UserQuery.php') diff --git a/app/Models/UserQuery.php b/app/Models/UserQuery.php index 4c7e2a8f7..278074362 100644 --- a/app/Models/UserQuery.php +++ b/app/Models/UserQuery.php @@ -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 $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 + * @return array */ - 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[acfst])(_(?P\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; } diff --git a/tests/app/Models/UserQueryTest.php b/tests/app/Models/UserQueryTest.php index d56c4b743..7b1e88907 100644 --- a/tests/app/Models/UserQueryTest.php +++ b/tests/app/Models/UserQueryTest.php @@ -74,8 +74,8 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { public function test__construct_whenUnknownQuery_doesStoreParameters() { $query = array('get' => 'q'); $user_query = new FreshRSS_UserQuery($query); - $this->assertNull($user_query->getGetName()); - $this->assertNull($user_query->getGetType()); + $this->assertEmpty($user_query->getGetName()); + $this->assertEmpty($user_query->getGetType()); } public function test__construct_whenName_storesName() { @@ -93,7 +93,7 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { } public function test__construct_whenState_storesState() { - $state = 'some state'; + $state = FreshRSS_Entry::STATE_ALL; $query = array('state' => $state); $user_query = new FreshRSS_UserQuery($query); $this->assertEquals($state, $user_query->getState()); @@ -118,7 +118,7 @@ class UserQueryTest extends PHPUnit\Framework\TestCase { 'name' => 'some name', 'order' => 'some order', 'search' => 'some search', - 'state' => 'some state', + 'state' => FreshRSS_Entry::STATE_ALL, 'url' => 'some url', ); $user_query = new FreshRSS_UserQuery($query); -- cgit v1.2.3