diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-02 17:50:59 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 13:50:11 +0200 |
commit | efb7d21b52eb033530e80e5e49d175e6e3b031f4 (patch) | |
tree | 4f34052788a08be1a30cb88c3339ae14e0b7c4da /application/bookmark/BookmarkFilter.php | |
parent | 29c31b7ec6ca48ba37b7eb6da650931fd0cb7164 (diff) | |
download | Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.gz Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.zst Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.zip |
Add strict types for bookmarks management
Parameters typing and using strict types overall increase the codebase
quality by enforcing the a given parameter will have the expected type.
It also removes the need to unnecessary unit tests checking methods
behavior with invalid input.
Diffstat (limited to 'application/bookmark/BookmarkFilter.php')
-rw-r--r-- | application/bookmark/BookmarkFilter.php | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/application/bookmark/BookmarkFilter.php b/application/bookmark/BookmarkFilter.php index 6636bbfe..4232f114 100644 --- a/application/bookmark/BookmarkFilter.php +++ b/application/bookmark/BookmarkFilter.php | |||
@@ -1,5 +1,7 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | declare(strict_types=1); | ||
4 | |||
3 | namespace Shaarli\Bookmark; | 5 | namespace Shaarli\Bookmark; |
4 | 6 | ||
5 | use Exception; | 7 | use Exception; |
@@ -77,8 +79,13 @@ class BookmarkFilter | |||
77 | * | 79 | * |
78 | * @throws BookmarkNotFoundException | 80 | * @throws BookmarkNotFoundException |
79 | */ | 81 | */ |
80 | public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false) | 82 | public function filter( |
81 | { | 83 | string $type, |
84 | $request, | ||
85 | bool $casesensitive = false, | ||
86 | string $visibility = 'all', | ||
87 | bool $untaggedonly = false | ||
88 | ) { | ||
82 | if (!in_array($visibility, ['all', 'public', 'private'])) { | 89 | if (!in_array($visibility, ['all', 'public', 'private'])) { |
83 | $visibility = 'all'; | 90 | $visibility = 'all'; |
84 | } | 91 | } |
@@ -128,7 +135,7 @@ class BookmarkFilter | |||
128 | * | 135 | * |
129 | * @return Bookmark[] filtered bookmarks. | 136 | * @return Bookmark[] filtered bookmarks. |
130 | */ | 137 | */ |
131 | private function noFilter($visibility = 'all') | 138 | private function noFilter(string $visibility = 'all') |
132 | { | 139 | { |
133 | if ($visibility === 'all') { | 140 | if ($visibility === 'all') { |
134 | return $this->bookmarks; | 141 | return $this->bookmarks; |
@@ -151,11 +158,11 @@ class BookmarkFilter | |||
151 | * | 158 | * |
152 | * @param string $smallHash permalink hash. | 159 | * @param string $smallHash permalink hash. |
153 | * | 160 | * |
154 | * @return array $filtered array containing permalink data. | 161 | * @return Bookmark[] $filtered array containing permalink data. |
155 | * | 162 | * |
156 | * @throws \Shaarli\Bookmark\Exception\BookmarkNotFoundException if the smallhash doesn't match any link. | 163 | * @throws BookmarkNotFoundException if the smallhash doesn't match any link. |
157 | */ | 164 | */ |
158 | private function filterSmallHash($smallHash) | 165 | private function filterSmallHash(string $smallHash) |
159 | { | 166 | { |
160 | foreach ($this->bookmarks as $key => $l) { | 167 | foreach ($this->bookmarks as $key => $l) { |
161 | if ($smallHash == $l->getShortUrl()) { | 168 | if ($smallHash == $l->getShortUrl()) { |
@@ -186,9 +193,9 @@ class BookmarkFilter | |||
186 | * @param string $searchterms search query. | 193 | * @param string $searchterms search query. |
187 | * @param string $visibility Optional: return only all/private/public bookmarks. | 194 | * @param string $visibility Optional: return only all/private/public bookmarks. |
188 | * | 195 | * |
189 | * @return array search results. | 196 | * @return Bookmark[] search results. |
190 | */ | 197 | */ |
191 | private function filterFulltext($searchterms, $visibility = 'all') | 198 | private function filterFulltext(string $searchterms, string $visibility = 'all') |
192 | { | 199 | { |
193 | if (empty($searchterms)) { | 200 | if (empty($searchterms)) { |
194 | return $this->noFilter($visibility); | 201 | return $this->noFilter($visibility); |
@@ -268,7 +275,7 @@ class BookmarkFilter | |||
268 | * | 275 | * |
269 | * @return string generated regex fragment | 276 | * @return string generated regex fragment |
270 | */ | 277 | */ |
271 | private static function tag2regex($tag) | 278 | private static function tag2regex(string $tag): string |
272 | { | 279 | { |
273 | $len = strlen($tag); | 280 | $len = strlen($tag); |
274 | if (!$len || $tag === "-" || $tag === "*") { | 281 | if (!$len || $tag === "-" || $tag === "*") { |
@@ -314,13 +321,13 @@ class BookmarkFilter | |||
314 | * You can specify one or more tags, separated by space or a comma, e.g. | 321 | * You can specify one or more tags, separated by space or a comma, e.g. |
315 | * print_r($mydb->filterTags('linux programming')); | 322 | * print_r($mydb->filterTags('linux programming')); |
316 | * | 323 | * |
317 | * @param string $tags list of tags separated by commas or blank spaces. | 324 | * @param string|array $tags list of tags, separated by commas or blank spaces if passed as string. |
318 | * @param bool $casesensitive ignore case if false. | 325 | * @param bool $casesensitive ignore case if false. |
319 | * @param string $visibility Optional: return only all/private/public bookmarks. | 326 | * @param string $visibility Optional: return only all/private/public bookmarks. |
320 | * | 327 | * |
321 | * @return array filtered bookmarks. | 328 | * @return Bookmark[] filtered bookmarks. |
322 | */ | 329 | */ |
323 | public function filterTags($tags, $casesensitive = false, $visibility = 'all') | 330 | public function filterTags($tags, bool $casesensitive = false, string $visibility = 'all') |
324 | { | 331 | { |
325 | // get single tags (we may get passed an array, even though the docs say different) | 332 | // get single tags (we may get passed an array, even though the docs say different) |
326 | $inputTags = $tags; | 333 | $inputTags = $tags; |
@@ -396,9 +403,9 @@ class BookmarkFilter | |||
396 | * | 403 | * |
397 | * @param string $visibility return only all/private/public bookmarks. | 404 | * @param string $visibility return only all/private/public bookmarks. |
398 | * | 405 | * |
399 | * @return array filtered bookmarks. | 406 | * @return Bookmark[] filtered bookmarks. |
400 | */ | 407 | */ |
401 | public function filterUntagged($visibility) | 408 | public function filterUntagged(string $visibility) |
402 | { | 409 | { |
403 | $filtered = []; | 410 | $filtered = []; |
404 | foreach ($this->bookmarks as $key => $link) { | 411 | foreach ($this->bookmarks as $key => $link) { |
@@ -427,11 +434,11 @@ class BookmarkFilter | |||
427 | * @param string $day day to filter. | 434 | * @param string $day day to filter. |
428 | * @param string $visibility return only all/private/public bookmarks. | 435 | * @param string $visibility return only all/private/public bookmarks. |
429 | 436 | ||
430 | * @return array all link matching given day. | 437 | * @return Bookmark[] all link matching given day. |
431 | * | 438 | * |
432 | * @throws Exception if date format is invalid. | 439 | * @throws Exception if date format is invalid. |
433 | */ | 440 | */ |
434 | public function filterDay($day, $visibility) | 441 | public function filterDay(string $day, string $visibility) |
435 | { | 442 | { |
436 | if (!checkDateFormat('Ymd', $day)) { | 443 | if (!checkDateFormat('Ymd', $day)) { |
437 | throw new Exception('Invalid date format'); | 444 | throw new Exception('Invalid date format'); |
@@ -460,9 +467,9 @@ class BookmarkFilter | |||
460 | * @param string $tags string containing a list of tags. | 467 | * @param string $tags string containing a list of tags. |
461 | * @param bool $casesensitive will convert everything to lowercase if false. | 468 | * @param bool $casesensitive will convert everything to lowercase if false. |
462 | * | 469 | * |
463 | * @return array filtered tags string. | 470 | * @return string[] filtered tags string. |
464 | */ | 471 | */ |
465 | public static function tagsStrToArray($tags, $casesensitive) | 472 | public static function tagsStrToArray(string $tags, bool $casesensitive): array |
466 | { | 473 | { |
467 | // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | 474 | // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) |
468 | $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8'); | 475 | $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8'); |