Passed
Push — master ( 968f43...466ed2 )
by Rougin
02:22
created

Query   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 302
Duplicated Lines 0 %

Test Coverage

Coverage 84.13%

Importance

Changes 0
Metric Value
wmc 26
eloc 34
dl 0
loc 302
ccs 53
cts 63
cp 0.8413
rs 10
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 5 1
A sql() 0 3 1
A orderBy() 0 3 1
A insertInto() 0 2 1
A from() 0 5 1
A orWhere() 0 3 1
A innerJoin() 0 5 1
A update() 0 2 1
A __toString() 0 3 1
A types() 0 3 1
A orHaving() 0 3 1
A bindings() 0 3 1
A __construct() 0 3 1
A andOrderBy() 0 3 1
A where() 0 3 1
A instance() 0 3 1
A rightJoin() 0 5 1
A andHaving() 0 3 1
A leftJoin() 0 5 1
A groupBy() 0 5 1
A having() 0 3 1
A deleteFrom() 0 2 1
A andWhere() 0 3 1
A builder() 0 5 1
A limit() 0 10 2
1
<?php
2
3
namespace Rougin\Windstorm\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Rougin\Windstorm\QueryInterface;
8
9
/**
10
 * Query
11
 *
12
 * @package Windstorm
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class Query implements QueryInterface
16
{
17
    /**
18
     * @var \Illuminate\Database\Eloquent\Builder
19
     */
20
    protected $builder;
21
22
    /**
23
     * Initializes the query instance.
24
     *
25
     * @param \Illuminate\Database\Eloquent\Model $model
26
     */
27 93
    public function __construct(Model $model)
28
    {
29 93
        $this->builder = $model->newQuery();
30 93
    }
31
32
    /**
33
     * Returns the safe and compiled SQL.
34
     *
35
     * @return string
36
     */
37
    public function __toString()
38
    {
39
        return $this->sql();
40
    }
41
42
    /**
43
     * Generates an AND HAVING query.
44
     *
45
     * @param  string $key
46
     * @return \Rougin\Windstorm\HavingInterface
47
     */
48 3
    public function andHaving($key)
49
    {
50 3
        return new Having($this, $this->builder, $key, 'and');
51
    }
52
53
    /**
54
     * Generates a multiple ORDER BY query.
55
     *
56
     * @param  string $key
57
     * @return \Rougin\Windstorm\OrderInterface
58
     */
59 3
    public function andOrderBy($key)
60
    {
61 3
        return new Order($this, $this->builder, $key);
62
    }
63
64
    /**
65
     * Generates an AND WHERE query.
66
     *
67
     * @param  string $key
68
     * @return \Rougin\Windstorm\WhereInterface
69
     */
70 3
    public function andWhere($key)
71
    {
72 3
        return new Where($this, $this->builder, $key, 'and');
73
    }
74
75
    /**
76
     * Returns the SQL bindings specified.
77
     *
78
     * @return array
79
     */
80
    public function bindings()
81
    {
82
        return $this->builder->getBindings();
83
    }
84
85
    /**
86
     * Sets the builder instance.
87
     *
88
     * @param  \Illuminate\Database\Query\Builder $builder
89
     * @return self
90
     */
91 69
    public function builder(Builder $builder)
92
    {
93 69
        $this->builder = $builder;
94
95 69
        return $this;
96
    }
97
98
    /**
99
     * Generates a DELETE FROM query.
100
     *
101
     * @param  string      $table
102
     * @param  string|null $alias
103
     * @return self
104
     */
105
    public function deleteFrom($table, $alias = null)
106
    {
107
    }
108
109
    /**
110
     * Generates a FROM query.
111
     *
112
     * @param  string      $table
113
     * @param  string|null $alias
114
     * @return self
115
     */
116 90
    public function from($table, $alias = null)
117
    {
118 90
        $this->builder = $this->builder->from($table);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->from($table) can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
119
120 90
        return $this;
121
    }
122
123
    /**
124
     * Generates a GROUP BY query.
125
     *
126
     * @param  array|string $fields
127
     * @return self
128
     */
129 3
    public function groupBy($fields)
130
    {
131 3
        $this->builder = $this->builder->groupBy($fields);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->groupBy($fields) can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
132
133 3
        return $this;
134
    }
135
136
    /**
137
     * Generates a HAVING query.
138
     *
139
     * @param  string $key
140
     * @return \Rougin\Windstorm\HavingInterface
141
     */
142 9
    public function having($key)
143
    {
144 9
        return new Having($this, $this->builder, $key);
145
    }
146
147
    /**
148
     * Generates an INNER JOIN query.
149
     *
150
     * @param  string $table
151
     * @param  string $local
152
     * @param  string $foreign
153
     * @return self
154
     */
155 3
    public function innerJoin($table, $local, $foreign)
156
    {
157 3
        $this->builder = $this->builder->join($table, $local, '=', $foreign);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->join($ta... $local, '=', $foreign) can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
158
159 3
        return $this;
160
    }
161
162
    /**
163
     * Generates an INSERT INTO query.
164
     *
165
     * @param  string $table
166
     * @return \Rougin\Windstorm\InsertInterface
167
     */
168
    public function insertInto($table)
169
    {
170
    }
171
172
    /**
173
     * Returns the model instance.
174
     *
175
     * @return \Illuminate\Database\Eloquent\Builder
176
     */
177 3
    public function instance()
178
    {
179 3
        return $this->builder;
180
    }
181
182
    /**
183
     * Generates a LEFT JOIN query.
184
     *
185
     * @param  string $table
186
     * @param  string $local
187
     * @param  string $foreign
188
     * @return self
189
     */
190 3
    public function leftJoin($table, $local, $foreign)
191
    {
192 3
        $this->builder = $this->builder->join($table, $local, '=', $foreign, 'left');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->join($ta... '=', $foreign, 'left') can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
193
194 3
        return $this;
195
    }
196
197
    /**
198
     * Performs a LIMIT query.
199
     *
200
     * @param  integer      $limit
201
     * @param  integer|null $offset
202
     * @return self
203
     */
204 3
    public function limit($limit, $offset = null)
205
    {
206 3
        $this->builder = $this->builder->limit($limit);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->limit($limit) can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
207
208 1
        if ($offset)
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
209 2
        {
210 3
            $this->builder = $this->builder->offset($offset);
211 2
        }
212
213 3
        return $this;
214
    }
215
216
    /**
217
     * Generates an OR HAVING query.
218
     *
219
     * @param  string $key
220
     * @return \Rougin\Windstorm\HavingInterface
221
     */
222 3
    public function orHaving($key)
223
    {
224 3
        return new Having($this, $this->builder, $key, 'or');
225
    }
226
227
    /**
228
     * Generates an OR WHERE query.
229
     *
230
     * @param  string $key
231
     * @return \Rougin\Windstorm\WhereInterface
232
     */
233 3
    public function orWhere($key)
234
    {
235 3
        return new Where($this, $this->builder, $key, 'or');
236
    }
237
238
    /**
239
     * Generates an ORDER BY query.
240
     *
241
     * @param  string $key
242
     * @return \Rougin\Windstorm\OrderInterface
243
     */
244 12
    public function orderBy($key)
245
    {
246 12
        return new Order($this, $this->builder, $key);
247
    }
248
249
    /**
250
     * Generates a RIGHT JOIN query.
251
     *
252
     * @param  string $table
253
     * @param  string $local
254
     * @param  string $foreign
255
     * @return self
256
     */
257 3
    public function rightJoin($table, $local, $foreign)
258
    {
259 3
        $this->builder = $this->builder->join($table, $local, '=', $foreign, 'right');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->join($ta...'=', $foreign, 'right') can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
260
261 3
        return $this;
262
    }
263
264
    /**
265
     * Generates a SELECT query.
266
     *
267
     * @param  array|string $fields
268
     * @return self
269
     */
270 90
    public function select($fields)
271
    {
272 90
        $this->builder = $this->builder->select($fields);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->select($fields) can also be of type Illuminate\Database\Query\Builder. However, the property $builder is declared as type Illuminate\Database\Eloquent\Builder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
273
274 90
        return $this;
275
    }
276
277
    /**
278
     * Returns the safe and compiled SQL.
279
     *
280
     * @return string
281
     */
282 87
    public function sql()
283
    {
284 87
        return $this->builder->toSql();
285
    }
286
287
    /**
288
     * Returns the data types of the bindings.
289
     *
290
     * @return array
291
     */
292 3
    public function types()
293
    {
294 3
        return array();
295
    }
296
297
    /**
298
     * Generates an UPDATE query.
299
     *
300
     * @param  string      $table
301
     * @param  string|null $alias
302
     * @return \Rougin\Windstorm\UpdateInterface
303
     */
304
    public function update($table, $alias = null)
305
    {
306
    }
307
308
    /**
309
     * Generates a WHERE query.
310
     *
311
     * @param  string $key
312
     * @return \Rougin\Windstorm\WhereInterface
313
     */
314 48
    public function where($key)
315
    {
316 48
        return new Where($this, $this->builder, $key, 'and');
317
    }
318
}
319