Skip to content

Commit 2dda3e0

Browse files
o-alquimistajaviereguiluz
authored andcommitted
Removed sanitizeSearchQuery() and moved its code into extractSearchTerms()
1 parent df8275b commit 2dda3e0

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/Repository/PostRepository.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public function findLatest(int $page = 1, Tag $tag = null): Paginator
5858
*/
5959
public function findBySearchQuery(string $rawQuery, int $limit = Post::NUM_ITEMS): array
6060
{
61-
$query = $this->sanitizeSearchQuery($rawQuery);
62-
$searchTerms = $this->extractSearchTerms($query);
61+
$searchTerms = $this->extractSearchTerms($rawQuery);
6362

6463
if (0 === \count($searchTerms)) {
6564
return [];
@@ -82,20 +81,16 @@ public function findBySearchQuery(string $rawQuery, int $limit = Post::NUM_ITEMS
8281
}
8382

8483
/**
85-
* Removes all non-alphanumeric characters except whitespaces.
86-
*/
87-
private function sanitizeSearchQuery(string $query): string
88-
{
89-
return trim(preg_replace('/[[:space:]]+/', ' ', $query));
90-
}
91-
92-
/**
93-
* Splits the search query into terms and removes the ones which are irrelevant.
84+
* Transforms the search string into an array of search terms.
9485
*/
9586
private function extractSearchTerms(string $searchQuery): array
9687
{
88+
$searchQuery = trim(preg_replace('/[[:space:]]+/', ' ', $searchQuery));
9789
$terms = array_unique(explode(' ', $searchQuery));
9890

91+
/*
92+
* Search terms with a very small length are left out.
93+
*/
9994
return array_filter($terms, function ($term) {
10095
return 2 <= mb_strlen($term);
10196
});

0 commit comments

Comments
 (0)