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

QueryBuilder::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
3
namespace Silk\Post;
4
5
use WP_Query;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use Illuminate\Support\Collection;
7
use Silk\Query\Builder as BaseBuilder;
8
9
class QueryBuilder extends BaseBuilder
10
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
11
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
12
     * WP_Query instance
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
13
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
14
     * @var WP_Query
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_Query => \WP_Query
Loading history...
15
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
16
    protected $query;
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
     * Post Model instance
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
20
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
21
     * @var Model
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Invalid class name "Model"
Loading history...
22
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
23
    protected $model;
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
24
25
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
26
     * Builder constructor.
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
     * @param WP_Query $query
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_Query => \WP_Query
Loading history...
29
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
30
    public function __construct(WP_Query $query)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
31
    {
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...
32
        $this->query = $query;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
33
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
34
35
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
36
     * Create a new instance.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
37
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
38
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
39
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
40
    public static function make()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
41
    {
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...
42
        return new static(new WP_Query);
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...
43
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
44
45
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
46
     * Limit the number of returned results
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
47
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
48
     * @param integer $limit  The maximum number of results to return
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
integer => int
Loading history...
49
     *                        use -1 for no limit
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
50
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
51
     * @return $this
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
    public function limit($limit)
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...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
55
        $this->query->set('posts_per_page', (int) $limit);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
56
57
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
58
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
59
60
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
61
     * Return an unlimited number of results.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
62
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
64
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
65
    public function all()
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...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
67
        return $this->limit(-1);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
68
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
69
70
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
71
     * Set the order for the query
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
72
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
73
     * @param  string $order
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
74
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
75
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
76
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
77
    public function order($order)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
78
    {
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...
79
        $this->query->set('order', strtoupper($order));
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
80
81
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
82
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
83
84
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
85
     * Query by post status
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
86
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
87
     * @param  string|array $status  the post status or stati to match
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
88
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
89
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
90
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
91
    public function whereStatus($status)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
92
    {
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...
93
        $this->query->set('post_status', $status);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
94
95
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
96
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
97
98
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
99
     * Query by slug
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
100
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
101
     * @param  string $slug  the post slug to query by
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
102
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
103
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
104
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
105
    public function whereSlug($slug)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
106
    {
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...
107
        $this->query->set('name', $slug);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
108
109
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
110
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
111
112
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
113
     * Get the results as a collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
114
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
115
     * @return Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
116
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
117
    public function results()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
118
    {
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...
119
        if ($this->model) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
120
            return $this->collectModels();
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
121
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
122
123
        return Collection::make($this->query->get_posts());
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
124
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
125
126
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
127
     * Get the results as a collection of post model instances
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
128
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
129
     * @return Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
130
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
131 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...
132
    {
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...
133
        $this->query->set('post_type', $this->model->post_type);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
134
        $this->query->set('fields', ''); // as WP_Post objects
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
135
        $modelClass = get_class($this->model);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
136
137
        return Collection::make($this->query->get_posts())
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
138
            ->map(function ($post) use ($modelClass) {
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
139
                return new $modelClass($post);
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
140
            });
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
141
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
142
143
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
144
     * Set a query variable on the query
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
     * @param string $var   Query variable key
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
147
     * @param mixed  $value Query value for key
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
148
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
149
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
150
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
151
    public function set($var, $value)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
152
    {
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...
153
        $this->query->set($var, $value);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
154
155
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
156
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
157
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
158