Passed
Push — master ( 9f58b4...83392d )
by Rougin
03:13
created

Query   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 393
ccs 92
cts 92
cp 1
rs 9.76
c 0
b 0
f 0
wmc 33

28 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 7 1
A sql() 0 21 4
A orderBy() 0 3 1
A insertInto() 0 5 1
A from() 0 7 1
A orWhere() 0 3 1
A innerJoin() 0 5 1
A update() 0 5 1
A __toString() 0 3 1
A orHaving() 0 3 1
A bindings() 0 8 2
A type() 0 3 1
A __construct() 0 3 1
A andOrderBy() 0 3 1
A where() 0 3 1
A instance() 0 3 1
A data() 0 5 1
A rightJoin() 0 5 1
A __clone() 0 3 1
A table() 0 3 1
A andHaving() 0 3 1
A leftJoin() 0 5 1
A groupBy() 0 5 1
A limit() 0 10 2
A having() 0 3 1
A deleteFrom() 0 7 1
A andWhere() 0 3 1
A builder() 0 5 1
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 array
19
     */
20
    protected $bindings = array();
21
22
    /**
23
     * @var \Illuminate\Database\Eloquent\Builder
24
     */
25
    protected $builder;
26
27
    /**
28
     * @var string
29
     */
30
    protected $sql = '';
31
32
    /**
33
     * @var string
34
     */
35
    protected $table = '';
36
37
    /**
38
     * @var integer
39
     */
40
    protected $type = self::TYPE_SELECT;
41
42
    /**
43
     * Clones the builder instance.
44
     *
45
     * @return void
46
     */
47 3
    public function __clone()
48
    {
49 3
        $this->builder = clone $this->builder;
50 3
    }
51
52
    /**
53
     * Initializes the query instance.
54
     *
55
     * @param \Illuminate\Database\Eloquent\Model $model
56
     */
57 126
    public function __construct(Model $model)
58
    {
59 126
        $this->builder = $model->newQuery();
60 126
    }
61
62
    /**
63
     * Returns the safe and compiled SQL.
64
     *
65
     * @return string
66
     */
67 3
    public function __toString()
68
    {
69 3
        return $this->sql();
70
    }
71
72
    /**
73
     * Generates an AND HAVING query.
74
     *
75
     * @param  string $key
76
     * @return \Rougin\Windstorm\HavingInterface
77
     */
78 3
    public function andHaving($key)
79
    {
80 3
        return new Having($this, $this->builder, $key, 'and');
81
    }
82
83
    /**
84
     * Generates a multiple ORDER BY query.
85
     *
86
     * @param  string $key
87
     * @return \Rougin\Windstorm\OrderInterface
88
     */
89 3
    public function andOrderBy($key)
90
    {
91 3
        return new Order($this, $this->builder, $key);
92
    }
93
94
    /**
95
     * Generates an AND WHERE query.
96
     *
97
     * @param  string $key
98
     * @return \Rougin\Windstorm\WhereInterface
99
     */
100 3
    public function andWhere($key)
101
    {
102 3
        return new Where($this, $this->builder, $key, 'and');
103
    }
104
105
    /**
106
     * Returns the SQL bindings specified.
107
     *
108
     * @return array
109
     */
110 12
    public function bindings()
111
    {
112 12
        if (empty($this->bindings))
113 8
        {
114 6
            return $this->builder->getBindings();
115
        }
116
117 6
        return $this->bindings;
118
    }
119
120
    /**
121
     * Sets the builder instance.
122
     *
123
     * @param  \Illuminate\Database\Query\Builder $builder
124
     * @return self
125
     */
126 90
    public function builder(Builder $builder)
127
    {
128 90
        $this->builder = $builder;
129
130 90
        return $this;
131
    }
132
133
    /**
134
     * Sets the data with new values.
135
     *
136
     * @param  array $data
137
     * @return self
138
     */
139 12
    public function data(array $data)
140
    {
141 12
        $this->bindings = $data;
142
143 12
        return $this;
144
    }
145
146
    /**
147
     * Generates a DELETE FROM query.
148
     *
149
     * @param  string      $table
150
     * @param  string|null $alias
151
     * @return self
152
     */
153 15
    public function deleteFrom($table, $alias = null)
154
    {
155 15
        $this->type = self::TYPE_DELETE;
156
157 15
        $this->table = (string) $table;
158
159 15
        return $this;
160
    }
161
162
    /**
163
     * Generates a FROM query.
164
     *
165
     * @param  string      $table
166
     * @param  string|null $alias
167
     * @return self
168
     */
169 96
    public function from($table, $alias = null)
170
    {
171 96
        $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...
172
173 96
        $this->table = (string) $table;
174
175 96
        return $this;
176
    }
177
178
    /**
179
     * Generates a GROUP BY query.
180
     *
181
     * @param  array|string $fields
182
     * @return self
183
     */
184 3
    public function groupBy($fields)
185
    {
186 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...
187
188 3
        return $this;
189
    }
190
191
    /**
192
     * Generates a HAVING query.
193
     *
194
     * @param  string $key
195
     * @return \Rougin\Windstorm\HavingInterface
196
     */
197 9
    public function having($key)
198
    {
199 9
        return new Having($this, $this->builder, $key);
200
    }
201
202
    /**
203
     * Generates an INNER JOIN query.
204
     *
205
     * @param  string $table
206
     * @param  string $local
207
     * @param  string $foreign
208
     * @return self
209
     */
210 3
    public function innerJoin($table, $local, $foreign)
211
    {
212 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...
213
214 3
        return $this;
215
    }
216
217
    /**
218
     * Generates an INSERT INTO query.
219
     *
220
     * @param  string $table
221
     * @return \Rougin\Windstorm\InsertInterface
222
     */
223 6
    public function insertInto($table)
224
    {
225 6
        $this->type = (integer) self::TYPE_INSERT;
226
227 6
        return new Insert($this, $this->builder);
228
    }
229
230
    /**
231
     * Returns the model instance.
232
     *
233
     * @return \Illuminate\Database\Eloquent\Builder
234
     */
235 15
    public function instance()
236
    {
237 15
        return $this->builder;
238
    }
239
240
    /**
241
     * Generates a LEFT JOIN query.
242
     *
243
     * @param  string $table
244
     * @param  string $local
245
     * @param  string $foreign
246
     * @return self
247
     */
248 3
    public function leftJoin($table, $local, $foreign)
249
    {
250 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...
251
252 3
        return $this;
253
    }
254
255
    /**
256
     * Performs a LIMIT query.
257
     *
258
     * @param  integer      $limit
259
     * @param  integer|null $offset
260
     * @return self
261
     */
262 6
    public function limit($limit, $offset = null)
263
    {
264 6
        $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...
265
266 2
        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...
267 4
        {
268 3
            $this->builder = $this->builder->offset($offset);
269 2
        }
270
271 6
        return $this;
272
    }
273
274
    /**
275
     * Generates an OR HAVING query.
276
     *
277
     * @param  string $key
278
     * @return \Rougin\Windstorm\HavingInterface
279
     */
280 3
    public function orHaving($key)
281
    {
282 3
        return new Having($this, $this->builder, $key, 'or');
283
    }
284
285
    /**
286
     * Generates an OR WHERE query.
287
     *
288
     * @param  string $key
289
     * @return \Rougin\Windstorm\WhereInterface
290
     */
291 3
    public function orWhere($key)
292
    {
293 3
        return new Where($this, $this->builder, $key, 'or');
294
    }
295
296
    /**
297
     * Generates an ORDER BY query.
298
     *
299
     * @param  string $key
300
     * @return \Rougin\Windstorm\OrderInterface
301
     */
302 15
    public function orderBy($key)
303
    {
304 15
        return new Order($this, $this->builder, $key);
305
    }
306
307
    /**
308
     * Generates a RIGHT JOIN query.
309
     *
310
     * @param  string $table
311
     * @param  string $local
312
     * @param  string $foreign
313
     * @return self
314
     */
315 3
    public function rightJoin($table, $local, $foreign)
316
    {
317 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...
318
319 3
        return $this;
320
    }
321
322
    /**
323
     * Generates a SELECT query.
324
     *
325
     * @param  array|string $fields
326
     * @return self
327
     */
328 99
    public function select($fields)
329
    {
330 99
        $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...
331
332 99
        $this->type = self::TYPE_SELECT;
333
334 99
        return $this;
335
    }
336
337
    /**
338
     * Returns the safe and compiled SQL.
339
     *
340
     * @return string
341
     */
342 102
    public function sql()
343
    {
344 102
        $query = $this->builder->getQuery();
345
346 102
        switch ($this->type)
347
        {
348 102
            case QueryInterface::TYPE_INSERT:
349 3
                $grammar = $query->getGrammar();
350
351 3
                return $grammar->compileInsert($query, $this->bindings);
352 99
            case QueryInterface::TYPE_UPDATE:
353 3
                $grammar = $query->getGrammar();
354
355 3
                return $grammar->compileUpdate($query, $this->bindings);
356 96
            case QueryInterface::TYPE_DELETE:
357 6
                $grammar = $query->getGrammar();
358
359 6
                return $grammar->compileDelete($query);
360 60
        }
361
362 90
        return (string) $this->builder->toSql();
363
    }
364
365
    /**
366
     * Returns the table name from the query.
367
     *
368
     * @return string
369
     */
370 3
    public function table()
371
    {
372 3
        return $this->table;
373
    }
374
375
    /**
376
     * Returns the type of the query.
377
     *
378
     * @return integer
379
     */
380 15
    public function type()
381
    {
382 15
        return $this->type;
383
    }
384
385
    /**
386
     * Generates an UPDATE query.
387
     *
388
     * @param  string      $table
389
     * @param  string|null $alias
390
     * @return \Rougin\Windstorm\UpdateInterface
391
     */
392 6
    public function update($table, $alias = null)
393
    {
394 6
        $this->type = self::TYPE_UPDATE;
395
396 6
        return new Update($this, $this->builder);
397
    }
398
399
    /**
400
     * Generates a WHERE query.
401
     *
402
     * @param  string $key
403
     * @return \Rougin\Windstorm\WhereInterface
404
     */
405 66
    public function where($key)
406
    {
407 66
        return new Where($this, $this->builder, $key, 'and');
408
    }
409
}
410