Skip to content

Commit 61c2359

Browse files
committed
Better consistency for search results
1 parent 26f2c06 commit 61c2359

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

app/Models/Search.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,8 @@ class FreshRSS_Search {
7171
/** @var array<string>|null */
7272
private $not_search;
7373

74-
/**
75-
* @param string|null $input
76-
*/
77-
public function __construct(?string $input) {
78-
$input = (string)$input;
79-
if ($input === '') {
80-
return;
81-
}
74+
public function __construct(string $input) {
75+
$input = self::cleanSearch($input);
8276
$this->raw_input = $input;
8377

8478
$input = $this->parseNotEntryIds($input);
@@ -443,6 +437,9 @@ private function parseIntitleSearch(string $input): string {
443437
$input = str_replace($matches[0], '', $input);
444438
}
445439
$this->intitle = self::removeEmptyValues($this->intitle);
440+
if (empty($this->intitle)) {
441+
$this->intitle = null;
442+
}
446443
return $input;
447444
}
448445

@@ -456,6 +453,9 @@ private function parseNotIntitleSearch(string $input): string {
456453
$input = str_replace($matches[0], '', $input);
457454
}
458455
$this->not_intitle = self::removeEmptyValues($this->not_intitle);
456+
if (empty($this->not_intitle)) {
457+
$this->not_intitle = null;
458+
}
459459
return $input;
460460
}
461461

@@ -474,6 +474,9 @@ private function parseAuthorSearch(string $input): string {
474474
$input = str_replace($matches[0], '', $input);
475475
}
476476
$this->author = self::removeEmptyValues($this->author);
477+
if (empty($this->author)) {
478+
$this->author = null;
479+
}
477480
return $input;
478481
}
479482

@@ -487,6 +490,9 @@ private function parseNotAuthorSearch(string $input): string {
487490
$input = str_replace($matches[0], '', $input);
488491
}
489492
$this->not_author = self::removeEmptyValues($this->not_author);
493+
if (empty($this->not_author)) {
494+
$this->not_author = null;
495+
}
490496
return $input;
491497
}
492498

@@ -498,17 +504,17 @@ private function parseInurlSearch(string $input): string {
498504
if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
499505
$this->inurl = $matches['search'];
500506
$input = str_replace($matches[0], '', $input);
507+
$this->inurl = self::removeEmptyValues($this->inurl);
501508
}
502-
$this->inurl = self::removeEmptyValues($this->inurl);
503509
return $input;
504510
}
505511

506512
private function parseNotInurlSearch(string $input): string {
507513
if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
508514
$this->not_inurl = $matches['search'];
509515
$input = str_replace($matches[0], '', $input);
516+
$this->not_inurl = self::removeEmptyValues($this->not_inurl);
510517
}
511-
$this->not_inurl = self::removeEmptyValues($this->not_inurl);
512518
return $input;
513519
}
514520

@@ -574,19 +580,19 @@ private function parseTagsSearch(string $input): string {
574580
if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
575581
$this->tags = $matches['search'];
576582
$input = str_replace($matches[0], '', $input);
583+
$this->tags = self::removeEmptyValues($this->tags);
584+
$this->tags = self::decodeSpaces($this->tags);
577585
}
578-
$this->tags = self::removeEmptyValues($this->tags);
579-
$this->tags = self::decodeSpaces($this->tags);
580586
return $input;
581587
}
582588

583589
private function parseNotTagsSearch(string $input): string {
584590
if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
585591
$this->not_tags = $matches['search'];
586592
$input = str_replace($matches[0], '', $input);
593+
$this->not_tags = self::removeEmptyValues($this->not_tags);
594+
$this->not_tags = self::decodeSpaces($this->not_tags);
587595
}
588-
$this->not_tags = self::removeEmptyValues($this->not_tags);
589-
$this->not_tags = self::decodeSpaces($this->not_tags);
590596
return $input;
591597
}
592598

@@ -651,8 +657,8 @@ private function parseNotSearch(string $input): string {
651657
*/
652658
private static function cleanSearch(string $input): string {
653659
$input = preg_replace('/\s+/', ' ', $input);
654-
if ($input === null) {
655-
$input = '';
660+
if (!is_string($input)) {
661+
return '';
656662
}
657663
return trim($input);
658664
}

app/Models/Share.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ public static function load(string $filename): void {
5050
}
5151

5252
uasort(self::$list_sharing, static function (FreshRSS_Share $a, FreshRSS_Share $b) {
53-
if ($a->name() === null || $b->name() === null) {
54-
return 0;
55-
}
56-
return strcasecmp($a->name(), $b->name());
53+
return strcasecmp($a->name() ?? '', $b->name() ?? '');
5754
});
5855
}
5956

@@ -329,7 +326,7 @@ private static function transform(string $data, array $transform): string {
329326
}
330327

331328
foreach ($transform as $action) {
332-
$data = $action($data);
329+
$data = call_user_func($action, $data);
333330
}
334331

335332
return $data;

tests/app/Models/SearchTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SearchTest extends PHPUnit\Framework\TestCase {
77
/**
88
* @dataProvider provideEmptyInput
99
*/
10-
public function test__construct_whenInputIsEmpty_getsOnlyNullValues(?string $input): void {
10+
public function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
1111
$search = new FreshRSS_Search($input);
1212
self::assertEquals('', $search->getRawInput());
1313
self::assertNull($search->getIntitle());
@@ -23,13 +23,13 @@ public function test__construct_whenInputIsEmpty_getsOnlyNullValues(?string $inp
2323
/**
2424
* Return an array of values for the search object.
2525
* Here is the description of the values
26-
* @return array{array{''},array{null}}
26+
* @return array{array{''},array{' '}}
2727
*/
2828
public function provideEmptyInput(): array {
29-
return array(
30-
array(''),
31-
array(null),
32-
);
29+
return [
30+
[''],
31+
[' '],
32+
];
3333
}
3434

3535
/**
@@ -58,7 +58,7 @@ public function provideIntitleSearch(): array {
5858
array('word1 intitle:"word2 word3"', array('word2 word3'), array('word1')),
5959
array("word1 intitle:'word2 word3'", array('word2 word3'), array('word1')),
6060
array('intitle:word1 intitle:word2', array('word1', 'word2'), null),
61-
array('intitle: word1 word2', array(), array('word1', 'word2')),
61+
array('intitle: word1 word2', null, array('word1', 'word2')),
6262
array('intitle:123', array('123'), null),
6363
array('intitle:"word1 word2" word3"', array('word1 word2'), array('word3"')),
6464
array("intitle:'word1 word2' word3'", array('word1 word2'), array("word3'")),
@@ -95,7 +95,7 @@ public function provideAuthorSearch(): array {
9595
array('word1 author:"word2 word3"', array('word2 word3'), array('word1')),
9696
array("word1 author:'word2 word3'", array('word2 word3'), array('word1')),
9797
array('author:word1 author:word2', array('word1', 'word2'), null),
98-
array('author: word1 word2', array(), array('word1', 'word2')),
98+
array('author: word1 word2', null, array('word1', 'word2')),
9999
array('author:123', array('123'), null),
100100
array('author:"word1 word2" word3"', array('word1 word2'), array('word3"')),
101101
array("author:'word1 word2' word3'", array('word1 word2'), array("word3'")),
@@ -196,7 +196,7 @@ public function test__construct_whenInputContainsTags_setsTagsValue(string $inpu
196196
public function provideTagsSearch(): array {
197197
return array(
198198
array('#word1', array('word1'), null),
199-
array('# word1', array(), array('#', 'word1')),
199+
array('# word1', null, array('#', 'word1')),
200200
array('#123', array('123'), null),
201201
array('#word1 word2', array('word1'), array('word2')),
202202
array('#"word1 word2"', array('"word1'), array('word2"')),

0 commit comments

Comments
 (0)