Passed
Push — master ( 1908fb...8df30e )
by Rougin
03:01
created

Query::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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