Completed
Push — master ( 3d50cd...ebe6bd )
by Seth
01:45
created

LibGuidesSearch   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 21.82 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 12
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 0 26 3
A relevance() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace smtech\StMarksSearch\LibApps\LibGuides;
4
5
use smtech\StMarksSearch\Relevance;
6
use smtech\StMarksSearch\SearchResult;
7
use smtech\StMarksSearch\SearchSource;
8
use smtech\StMarksSearch\LibApps\AbstractLibAppsSearchDomain;
9
10
/**
11
 * LibGuides search domain
12
 *
13
 * @author Seth Battis <[email protected]>
14
 */
15
class LibGuidesSearch extends AbstractLibAppsSearchDomain
16
{
17
    /**
18
     * Search for `$query` within the LibGuides
19
     *
20
     * @param string $query
21
     * @return SearchResult[] Ordered by descending relevance
22
     */
23
    public function search($query)
24
    {
25
        $source = new SearchSource($this);
26
        $results = [];
27
        $response = $this->getApi()->get(
28
            '/guides',
29
            [
30
                'search_terms' => $query,
31
                'search_match' => 2 // contains
32
            ]
33
        );
34
        if (is_array($response)) {
35
            foreach ($response as $result) {
36
                $results[] = new SearchResult(
37
                    $result['url'],
38
                    $this->relevance($result, $query),
39
                    $result['name'],
40
                    $result['description'],
41
                    $source
42
                );
43
            }
44
        }
45
46
        $this->sortByRelevance($results);
47
        return $results;
48
    }
49
50
    /**
51
     * Calculate relevance score for a particular search result
52
     *
53
     * @param mixed[] $guide
54
     * @param string $query
55
     * @return Relevance
56
     */
57 View Code Duplication
    protected function relevance($guide, $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $relevance = new Relevance();
60
61
        $relevance->add(Relevance::stringProportion($guide['name'], $query), 'title match');
62
63
        if (($count = substr_count(strtolower($guide['description']), strtolower($query))) > 0) {
64
            $relevance->add($count * 0.01, "$count occurrences in description");
65
        }
66
67
        return $relevance;
68
    }
69
}
70