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
|
|||
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
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 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;
}
![]() |
|||
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 | * @param string|null $alias |
||
209 | * @return self |
||
210 | */ |
||
211 | 3 | public function innerJoin($table, $local, $foreign, $alias = null) |
|
212 | { |
||
213 | 3 | $table = $alias ? "$table as $alias" : $table; |
|
214 | |||
215 | 3 | $this->builder = $this->builder->join($table, $local, '=', $foreign); |
|
0 ignored issues
–
show
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 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;
}
![]() |
|||
216 | |||
217 | 3 | return $this; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Generates an INSERT INTO query. |
||
222 | * |
||
223 | * @param string $table |
||
224 | * @return \Rougin\Windstorm\InsertInterface |
||
225 | */ |
||
226 | 6 | public function insertInto($table) |
|
227 | { |
||
228 | 6 | $this->type = (integer) self::TYPE_INSERT; |
|
229 | |||
230 | 6 | return new Insert($this, $this->builder); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Returns the model instance. |
||
235 | * |
||
236 | * @return \Illuminate\Database\Eloquent\Builder |
||
237 | */ |
||
238 | 15 | public function instance() |
|
239 | { |
||
240 | 15 | return $this->builder; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Generates a LEFT JOIN query. |
||
245 | * |
||
246 | * @param string $table |
||
247 | * @param string $local |
||
248 | * @param string $foreign |
||
249 | * @param string|null $alias |
||
250 | * @return self |
||
251 | */ |
||
252 | 3 | public function leftJoin($table, $local, $foreign, $alias = null) |
|
253 | { |
||
254 | 3 | $table = $alias ? "$table as $alias" : $table; |
|
255 | |||
256 | 3 | $this->builder = $this->builder->join($table, $local, '=', $foreign, 'left'); |
|
0 ignored issues
–
show
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 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;
}
![]() |
|||
257 | |||
258 | 3 | return $this; |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * Performs a LIMIT query. |
||
263 | * |
||
264 | * @param integer $limit |
||
265 | * @param integer|null $offset |
||
266 | * @return self |
||
267 | */ |
||
268 | 3 | public function limit($limit, $offset = null) |
|
269 | { |
||
270 | 3 | $this->builder = $this->builder->limit($limit); |
|
0 ignored issues
–
show
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 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;
}
![]() |
|||
271 | |||
272 | 3 | if ($offset !== null) |
|
273 | 2 | { |
|
274 | 3 | $this->builder = $this->builder->offset($offset); |
|
275 | 2 | } |
|
276 | |||
277 | 3 | return $this; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * Generates an OR HAVING query. |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @return \Rougin\Windstorm\HavingInterface |
||
285 | */ |
||
286 | 3 | public function orHaving($key) |
|
287 | { |
||
288 | 3 | return new Having($this, $this->builder, $key, 'or'); |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Generates an OR WHERE query. |
||
293 | * |
||
294 | * @param string $key |
||
295 | * @return \Rougin\Windstorm\WhereInterface |
||
296 | */ |
||
297 | 3 | public function orWhere($key) |
|
298 | { |
||
299 | 3 | return new Where($this, $this->builder, $key, 'or'); |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Generates an ORDER BY query. |
||
304 | * |
||
305 | * @param string $key |
||
306 | * @return \Rougin\Windstorm\OrderInterface |
||
307 | */ |
||
308 | 15 | public function orderBy($key) |
|
309 | { |
||
310 | 15 | return new Order($this, $this->builder, $key); |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * Generates a RIGHT JOIN query. |
||
315 | * |
||
316 | * @param string $table |
||
317 | * @param string $local |
||
318 | * @param string $foreign |
||
319 | * @param string|null $alias |
||
320 | * @return self |
||
321 | */ |
||
322 | 3 | public function rightJoin($table, $local, $foreign, $alias = null) |
|
323 | { |
||
324 | 3 | $table = $alias ? "$table as $alias" : $table; |
|
325 | |||
326 | 3 | $this->builder = $this->builder->join($table, $local, '=', $foreign, 'right'); |
|
0 ignored issues
–
show
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 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;
}
![]() |
|||
327 | |||
328 | 3 | return $this; |
|
329 | } |
||
330 | |||
331 | /** |
||
332 | * Generates a SELECT query. |
||
333 | * |
||
334 | * @param array|string $fields |
||
335 | * @return self |
||
336 | */ |
||
337 | 99 | public function select($fields) |
|
338 | { |
||
339 | 99 | $this->builder = $this->builder->select($fields); |
|
0 ignored issues
–
show
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 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;
}
![]() |
|||
340 | |||
341 | 99 | $this->type = self::TYPE_SELECT; |
|
342 | |||
343 | 99 | return $this; |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * Returns the safe and compiled SQL. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 102 | public function sql() |
|
352 | { |
||
353 | 102 | $query = $this->builder->getQuery(); |
|
354 | |||
355 | 102 | switch ($this->type) |
|
356 | { |
||
357 | 102 | case QueryInterface::TYPE_INSERT: |
|
358 | 3 | $grammar = $query->getGrammar(); |
|
359 | |||
360 | 3 | return $grammar->compileInsert($query, $this->bindings); |
|
361 | 99 | case QueryInterface::TYPE_UPDATE: |
|
362 | 3 | $grammar = $query->getGrammar(); |
|
363 | |||
364 | 3 | return $grammar->compileUpdate($query, $this->bindings); |
|
365 | 96 | case QueryInterface::TYPE_DELETE: |
|
366 | 6 | $grammar = $query->getGrammar(); |
|
367 | |||
368 | 6 | return $grammar->compileDelete($query); |
|
369 | 60 | } |
|
370 | |||
371 | 90 | return (string) $this->builder->toSql(); |
|
372 | } |
||
373 | |||
374 | /** |
||
375 | * Returns the table name from the query. |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | 3 | public function table() |
|
380 | { |
||
381 | 3 | return $this->table; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * Returns the type of the query. |
||
386 | * |
||
387 | * @return integer |
||
388 | */ |
||
389 | 15 | public function type() |
|
390 | { |
||
391 | 15 | return $this->type; |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * Generates an UPDATE query. |
||
396 | * |
||
397 | * @param string $table |
||
398 | * @param string|null $alias |
||
399 | * @return \Rougin\Windstorm\UpdateInterface |
||
400 | */ |
||
401 | 6 | public function update($table, $alias = null) |
|
402 | { |
||
403 | 6 | $this->type = self::TYPE_UPDATE; |
|
404 | |||
405 | 6 | return new Update($this, $this->builder); |
|
406 | } |
||
407 | |||
408 | /** |
||
409 | * Generates a WHERE query. |
||
410 | * |
||
411 | * @param string $key |
||
412 | * @return \Rougin\Windstorm\WhereInterface |
||
413 | */ |
||
414 | 66 | public function where($key) |
|
415 | { |
||
416 | 66 | return new Where($this, $this->builder, $key, 'and'); |
|
417 | } |
||
418 | } |
||
419 |
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 theid
property of an instance of theAccount
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.