Completed
Push — master ( 0765b1...3d50cd )
by Seth
03:43 queued 01:43
created

LibGuidesSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 23 2
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 Exception;
6
use smtech\StMarksSearch\Relevance;
7
use smtech\StMarksSearch\SearchResult;
8
use smtech\StMarksSearch\SearchSource;
9
use smtech\StMarksSearch\LibApps\AbstractLibAppsSearchDomain;
10
11
class LibGuidesSearch extends AbstractLibAppsSearchDomain
12
{
13
    public function search($query)
14
    {
15
        $source = new SearchSource($this);
16
        $results = [];
17
        foreach ($this->getApi()->get(
18
            '/guides',
19
            [
20
                'search_terms' => $query,
21
                'search_match' => 2
22
            ]
23
        ) as $result) {
24
            $results[] = new SearchResult(
25
                $result['url'],
26
                $this->relevance($result, $query),
27
                $result['name'],
28
                $result['description'],
29
                $source
30
            );
31
        }
32
33
        $this->sortByRelevance($results);
34
        return $results;
35
    }
36
37 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...
38
    {
39
        $relevance = new Relevance();
40
41
        $relevance->add(Relevance::stringProportion($guide['name'], $query), 'title match');
42
43
        if (($count = substr_count(strtolower($guide['description']), strtolower($query))) > 0) {
44
            $relevance->add($count * 0.01, "$count occurrences in description");
45
        }
46
47
        return $relevance;
48
    }
49
}
50