Completed
Push — master ( 734eba...03e6ad )
by
unknown
02:56
created

NoResultsSearchWidget   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 44
loc 44
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B loadSearchHistory() 36 36 1

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 Charcoal\Admin\Widget\Search;
4
5
use \PDO;
6
7
use \Charcoal\Search\SearchLog;
8
9
/**
10
 * Search widget to show the latest searched-for terms that produced no results.
11
 */
12 View Code Duplication
class NoResultsSearchWidget extends AbstractSearchHistoryWidget
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Load the top searches, from the source, that produced no results.
16
     *
17
     * @return SearchLog[]
18
     */
19
    public function loadSearchHistory()
20
    {
21
        $proto  = $this->modelFactory()->get(SearchLog::class);
22
        $source = $proto->source();
23
        $table  = $source->table();
24
25
        $q = '
26
        SELECT
27
            `keyword`,
28
            COUNT(`keyword`) as num_searches,
29
            SUM(`num_results`) as num_results
30
        FROM
31
            `'.$table.'`
32
        WHERE
33
            `num_results` = 0
34
        AND
35
            `ts` > :start
36
        AND
37
            `ts` <= :end
38
        GROUP BY
39
            `keyword`
40
        ORDER BY
41
            num_searches DESC
42
        LIMIT
43
            20
44
        ';
45
46
        $sth = $source->dbQuery($q, [
47
            'start' => $this->startDate()->format('Y-m-d H:i:s'),
48
            'end'   => $this->endDate()->format('Y-m-d H:i:s')
49
        ]);
50
51
        $results = $sth->fetchAll(PDO::FETCH_ASSOC);
52
53
        return $results;
54
    }
55
}
56