aboutsummaryrefslogtreecommitdiff
path: root/app/Models/Search.php
diff options
context:
space:
mode:
authorGravatar Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> 2023-05-30 23:03:14 +0200
committerGravatar GitHub <noreply@github.com> 2023-05-30 23:03:14 +0200
commit1d2bb50f2e0721102a3739ce5b13ff77a772fe15 (patch)
tree31716dc3b6d4b87c81a9d9dde3b86321aba38bdf /app/Models/Search.php
parentadb5db9d971fb425c2949191605071c1389c30f6 (diff)
phpstan-9 for Share.php (#5431)
* phpstan 9 for Search.php phpstan 9 for Share.php * phpstan-9 for Search.php * Better consistency for search results --------- Co-authored-by: Luc <sanchezluc+freshrss@gmail.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'app/Models/Search.php')
-rw-r--r--app/Models/Search.php64
1 files changed, 38 insertions, 26 deletions
diff --git a/app/Models/Search.php b/app/Models/Search.php
index d2ba23eda..9622e18ab 100644
--- a/app/Models/Search.php
+++ b/app/Models/Search.php
@@ -71,13 +71,8 @@ class FreshRSS_Search {
/** @var array<string>|null */
private $not_search;
- /**
- * @param string|null $input
- */
- public function __construct($input) {
- if ($input == '') {
- return;
- }
+ public function __construct(string $input) {
+ $input = self::cleanSearch($input);
$this->raw_input = $input;
$input = $this->parseNotEntryIds($input);
@@ -238,7 +233,9 @@ class FreshRSS_Search {
* @return array<string>
*/
private static function removeEmptyValues(?array $anArray): array {
- return empty($anArray) ? [] : array_filter($anArray, static function(string $value) { return $value !== ''; });
+ return empty($anArray) ? [] : array_filter($anArray, static function(string $value) {
+ return $value !== '';
+ });
}
/**
@@ -299,7 +296,7 @@ class FreshRSS_Search {
foreach ($ids_lists as $ids_list) {
$feed_ids = explode(',', $ids_list);
$feed_ids = self::removeEmptyValues($feed_ids);
- /** @var array<int> */
+ /** @var array<int> $feed_ids */
$feed_ids = array_map('intval', $feed_ids);
if (!empty($feed_ids)) {
$this->feed_ids = array_merge($this->feed_ids, $feed_ids);
@@ -317,7 +314,7 @@ class FreshRSS_Search {
foreach ($ids_lists as $ids_list) {
$feed_ids = explode(',', $ids_list);
$feed_ids = self::removeEmptyValues($feed_ids);
- /** @var array<int> */
+ /** @var array<int> $feed_ids */
$feed_ids = array_map('intval', $feed_ids);
if (!empty($feed_ids)) {
$this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
@@ -342,7 +339,7 @@ class FreshRSS_Search {
}
$label_ids = explode(',', $ids_list);
$label_ids = self::removeEmptyValues($label_ids);
- /** @var array<int> */
+ /** @var array<int> $label_ids */
$label_ids = array_map('intval', $label_ids);
if (!empty($label_ids)) {
$this->label_ids = array_merge($this->label_ids, $label_ids);
@@ -364,7 +361,7 @@ class FreshRSS_Search {
}
$label_ids = explode(',', $ids_list);
$label_ids = self::removeEmptyValues($label_ids);
- /** @var array<int> */
+ /** @var array<int> $label_ids */
$label_ids = array_map('intval', $label_ids);
if (!empty($label_ids)) {
$this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
@@ -436,10 +433,13 @@ class FreshRSS_Search {
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->intitle = array_merge($this->intitle ? $this->intitle : array(), $matches['search']);
+ $this->intitle = array_merge($this->intitle ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->intitle = self::removeEmptyValues($this->intitle);
+ if (empty($this->intitle)) {
+ $this->intitle = null;
+ }
return $input;
}
@@ -449,10 +449,13 @@ class FreshRSS_Search {
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->not_intitle = array_merge($this->not_intitle ? $this->not_intitle : array(), $matches['search']);
+ $this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->not_intitle = self::removeEmptyValues($this->not_intitle);
+ if (empty($this->not_intitle)) {
+ $this->not_intitle = null;
+ }
return $input;
}
@@ -467,10 +470,13 @@ class FreshRSS_Search {
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
+ $this->author = array_merge($this->author ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->author = self::removeEmptyValues($this->author);
+ if (empty($this->author)) {
+ $this->author = null;
+ }
return $input;
}
@@ -480,10 +486,13 @@ class FreshRSS_Search {
$input = str_replace($matches[0], '', $input);
}
if (preg_match_all('/(?<=\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
- $this->not_author = array_merge($this->not_author ? $this->not_author : array(), $matches['search']);
+ $this->not_author = array_merge($this->not_author ?: [], $matches['search']);
$input = str_replace($matches[0], '', $input);
}
$this->not_author = self::removeEmptyValues($this->not_author);
+ if (empty($this->not_author)) {
+ $this->not_author = null;
+ }
return $input;
}
@@ -495,8 +504,8 @@ class FreshRSS_Search {
if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
$this->inurl = $matches['search'];
$input = str_replace($matches[0], '', $input);
+ $this->inurl = self::removeEmptyValues($this->inurl);
}
- $this->inurl = self::removeEmptyValues($this->inurl);
return $input;
}
@@ -504,8 +513,8 @@ class FreshRSS_Search {
if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
$this->not_inurl = $matches['search'];
$input = str_replace($matches[0], '', $input);
+ $this->not_inurl = self::removeEmptyValues($this->not_inurl);
}
- $this->not_inurl = self::removeEmptyValues($this->not_inurl);
return $input;
}
@@ -571,9 +580,9 @@ class FreshRSS_Search {
if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
$this->tags = $matches['search'];
$input = str_replace($matches[0], '', $input);
+ $this->tags = self::removeEmptyValues($this->tags);
+ $this->tags = self::decodeSpaces($this->tags);
}
- $this->tags = self::removeEmptyValues($this->tags);
- $this->tags = self::decodeSpaces($this->tags);
return $input;
}
@@ -581,9 +590,9 @@ class FreshRSS_Search {
if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
$this->not_tags = $matches['search'];
$input = str_replace($matches[0], '', $input);
+ $this->not_tags = self::removeEmptyValues($this->not_tags);
+ $this->not_tags = self::decodeSpaces($this->not_tags);
}
- $this->not_tags = self::removeEmptyValues($this->not_tags);
- $this->not_tags = self::decodeSpaces($this->not_tags);
return $input;
}
@@ -594,7 +603,7 @@ class FreshRSS_Search {
*/
private function parseQuotedSearch(string $input): string {
$input = self::cleanSearch($input);
- if ($input == '') {
+ if ($input === '') {
return '';
}
if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
@@ -611,7 +620,7 @@ class FreshRSS_Search {
*/
private function parseSearch(string $input): string {
$input = self::cleanSearch($input);
- if ($input == '') {
+ if ($input === '') {
return '';
}
if (is_array($this->search)) {
@@ -624,7 +633,7 @@ class FreshRSS_Search {
private function parseNotSearch(string $input): string {
$input = self::cleanSearch($input);
- if ($input == '') {
+ if ($input === '') {
return '';
}
if (preg_match_all('/(?<=\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
@@ -632,7 +641,7 @@ class FreshRSS_Search {
$input = str_replace($matches[0], '', $input);
}
$input = self::cleanSearch($input);
- if ($input == '') {
+ if ($input === '') {
return '';
}
if (preg_match_all('/(?<=\s|^)[!-](?P<search>[^\s]+)/', $input, $matches)) {
@@ -648,6 +657,9 @@ class FreshRSS_Search {
*/
private static function cleanSearch(string $input): string {
$input = preg_replace('/\s+/', ' ', $input);
+ if (!is_string($input)) {
+ return '';
+ }
return trim($input);
}
}