QueryBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silk\Term;
4
5
use Silk\Query\Builder as BaseBuilder;
6
use Silk\Exception\WP_ErrorException;
7
use Silk\Support\Collection;
8
9
/**
10
 * @property Model $model
11
 */
12
class QueryBuilder extends BaseBuilder
13
{
14
    /**
15
     * Query arguments
16
     * @var Collection
17
     */
18
    protected $query;
19
20
    /**
21
     * Taxonomy Identifier
22
     * @var string
23
     */
24
    protected $taxonomy;
25
26
    /**
27
     * QueryBuilder Constructor.
28
     *
29
     * @param array $args
30
     */
31
    public function __construct(array $args = [])
32
    {
33
        $this->query = new Collection($args);
34
    }
35
36
    /**
37
     * Create a new instance.
38
     *
39
     * @return static
40
     */
41
    public static function make()
42
    {
43
        return new static;
44
    }
45
46
    /**
47
     * Restrict the query to terms of the provided Taxonomy.
48
     *
49
     * @param  string $taxonomy
50
     *
51
     * @return $this
52
     */
53
    public function forTaxonomy($taxonomy)
54
    {
55
        $this->taxonomy = $taxonomy;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get all terms.
62
     *
63
     * @return $this
64
     */
65
    public function all()
66
    {
67
        return $this->includeEmpty()
68
            ->limit('all');
0 ignored issues
show
Bug introduced by
'all' of type string is incompatible with the type integer expected by parameter $max_results of Silk\Term\QueryBuilder::limit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            ->limit(/** @scrutinizer ignore-type */ 'all');
Loading history...
69
    }
70
71
    /**
72
     * Include terms that have no related objects in the results.
73
     *
74
     * @return $this
75
     */
76
    public function includeEmpty()
77
    {
78
        return $this->set('hide_empty', false);
79
    }
80
81
    /**
82
     * Limit the maximum number of results returned.
83
     *
84
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
85
     *
86
     * @return $this
87
     */
88
    public function limit($max_results)
89
    {
90
        return $this->set('number', intval($max_results));
91
    }
92
93
    /**
94
     * Execute the query and return the raw results.
95
     *
96
     * @throws WP_ErrorException
97
     *
98
     * @return array
99
     */
100
    protected function query()
101
    {
102
        if ($this->model) {
103
            $this->set('taxonomy', $this->model->taxonomy)
104
                 ->set('fields', 'all');
105
        } elseif ($this->taxonomy) {
106
            $this->set('taxonomy', $this->taxonomy);
107
        }
108
109
        if (is_wp_error($terms = get_terms($this->query->toArray()))) {
110
            throw new WP_ErrorException($terms);
0 ignored issues
show
Bug introduced by
It seems like $terms can also be of type array; however, parameter $error of Silk\Exception\WP_ErrorException::__construct() does only seem to accept WP_Error, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            throw new WP_ErrorException(/** @scrutinizer ignore-type */ $terms);
Loading history...
111
        }
112
113
        return $terms;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $terms also could return the type WP_Error which is incompatible with the documented return type array.
Loading history...
114
    }
115
116
    /**
117
     * Set an arbitrary query parameter.
118
     *
119
     * @param $parameter
120
     * @param $value
121
     *
122
     * @return $this
123
     */
124
    public function set($parameter, $value)
125
    {
126
        $this->query->put($parameter, $value);
127
128
        return $this;
129
    }
130
}
131