Completed
Push — develop ( c52d7e...4c8faa )
by Evan
04:55
created

QueryBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 8.28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 12
loc 145
rs 10
wmc 12
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 4 1
A forTaxonomy() 0 6 1
A all() 0 5 1
A includeEmpty() 0 6 1
A limit() 0 6 1
A results() 0 12 3
A collectModels() 12 12 1
A fetchTerms() 0 8 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 Silk\Term;
4
5
use WP_Term;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use Silk\Query\Builder as BaseBuilder;
7
use Silk\Exception\WP_ErrorException;
8
use Illuminate\Support\Collection;
9
10
class QueryBuilder extends BaseBuilder
11
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
12
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
13
     * The term model
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
14
     * @var Model
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Invalid class name "Model"
Loading history...
15
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
16
    protected $model;
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
17
18
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
19
     * Collection of arguments
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
20
     * @var Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
21
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
22
    protected $args;
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
23
24
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
25
     * Taxonomy Identifier
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
26
     * @var string
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
27
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
28
    protected $taxonomy;
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
29
30
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
31
     * TermQueryBuilder Constructor.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
32
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
33
     * @param array $args
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
34
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
35
    public function __construct(array $args = [])
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
36
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
37
        $this->args = Collection::make($args);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
38
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
39
40
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
41
     * Create a new instance.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
42
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
43
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
44
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
45
    public static function make()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
46
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
47
        return new static;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Calling class constructors must always include parentheses
Loading history...
48
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
49
50
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
51
     * Restrict the query to terms of the provided Taxonomy.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
52
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
53
     * @param  string $taxonomy
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
54
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
55
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
56
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
57
    public function forTaxonomy($taxonomy)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
58
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
59
        $this->taxonomy = $taxonomy;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
60
61
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
62
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
64
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
65
     * Get all terms.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
66
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
67
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
68
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
69
    public function all()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
70
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
71
        return $this->includeEmpty()
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
72
            ->limit('all');
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
73
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
74
75
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
76
     * Include terms that have no related objects in the results.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
77
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
78
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
79
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
80
    public function includeEmpty()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
81
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
82
        $this->args->put('hide_empty', false);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
83
84
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
85
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
86
87
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
88
     * Limit the maximum number of results returned.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
89
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
90
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
91
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
92
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
93
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
94
    public function limit($max_results)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
95
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
96
        $this->args->put('number', intval($max_results));
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
intval() found, should be int cast.
Loading history...
97
98
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
99
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
100
101
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
102
     * Get the query results.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
103
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
104
     * @throws WP_ErrorException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_ErrorException => \Silk\Exception\WP_ErrorException
Loading history...
105
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
106
     * @return Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
107
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
108
    public function results()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
109
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
110
        if ($this->model) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
111
            return $this->collectModels();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
112
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
113
114
        if ($this->taxonomy) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
115
            $this->args->put('taxonomy', $this->taxonomy);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
116
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
117
118
        return Collection::make($this->fetchTerms());
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
119
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
120
121
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
122
     * Get the results as a collection of models.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
123
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
124
     * @return Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
125
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
126 View Code Duplication
    protected function collectModels()
1 ignored issue
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...
introduced by
4 spaces found, expected 1 tabs
Loading history...
127
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
128
        $this->args->put('taxonomy', $this->model->taxonomy);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
129
        $this->args->put('fields', 'all');
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
130
131
        $modelClass = get_class($this->model);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
132
133
        return Collection::make($this->fetchTerms())
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
134
            ->map(function (WP_Term $term) use ($modelClass) {
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
135
                return new $modelClass($term);
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
136
            });
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
137
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
138
139
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
140
     * Perform the term query and return the results.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
141
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
142
     * @throws WP_ErrorException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_ErrorException => \Silk\Exception\WP_ErrorException
Loading history...
143
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
144
     * @return array
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
145
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
146
    protected function fetchTerms()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
147
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
148
        if (is_wp_error($terms = get_terms($this->args->toArray()))) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
149
            throw new WP_ErrorException($terms);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
150
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
151
152
        return $terms;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
153
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
154
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
155