Total Complexity | 46 |
Total Lines | 498 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BuildsWheres often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BuildsWheres, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait BuildsWheres |
||
21 | { |
||
22 | /** |
||
23 | * Add a "where fulltext" clause to the query. |
||
24 | * |
||
25 | * @param string|string[] $columns |
||
26 | * @param string $value |
||
27 | * @param array<mixed> $options |
||
28 | * @param string $boolean |
||
29 | * @return $this |
||
30 | * |
||
31 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
32 | */ |
||
33 | public function whereFullText($columns, $value, array $options = [], $boolean = 'and') |
||
34 | { |
||
35 | // NOTE: can be done by listing all fulltext calls and using it on the collection name from $builder->from |
||
36 | // distinctly merging results from multiple calls on the same collection. |
||
37 | // For a call on a joined collection it might need to be moved to the JoinClause |
||
38 | throw new LogicException('This database driver does not support the whereFullText method.'); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Prepare the value and operator for a where clause. |
||
43 | * |
||
44 | * @param DateTimeInterface|float|int|string|null $value |
||
45 | * @param string|null $operator |
||
46 | * @param bool $useDefault |
||
47 | * @return array<mixed> |
||
48 | * |
||
49 | * @throws \InvalidArgumentException |
||
50 | * |
||
51 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
52 | */ |
||
53 | public function prepareValueAndOperator($value, $operator, $useDefault = false) |
||
54 | { |
||
55 | if ($useDefault) { |
||
56 | return [$operator, '==']; |
||
57 | } elseif (!is_null($operator) && $this->invalidOperatorAndValue($operator, $value)) { |
||
|
|||
58 | throw new InvalidArgumentException('Illegal operator and value combination.'); |
||
59 | } |
||
60 | |||
61 | return [$value, $operator]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Add an array of where clauses to the query. |
||
66 | * |
||
67 | * @param array<mixed> $column |
||
68 | * @param string $boolean |
||
69 | * @param string $method |
||
70 | * @return $this |
||
71 | */ |
||
72 | protected function addArrayOfWheres($column, $boolean, $method = 'where') |
||
73 | { |
||
74 | $column = associativeFlatten($column); |
||
75 | |||
76 | return $this->whereNested(function ($query) use ($column, $method, $boolean) { |
||
77 | foreach ($column as $key => $value) { |
||
78 | $query->{$method}($key, '==', $value, $boolean); |
||
79 | } |
||
80 | }, $boolean); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param mixed $operator |
||
85 | * @param mixed $value |
||
86 | * @return array<mixed> |
||
87 | */ |
||
88 | public function validateOperator(mixed $operator, mixed $value): array |
||
89 | { |
||
90 | // If the given operator is not found in the list of valid operators we will |
||
91 | // assume that the developer is just short-cutting the '==' operators and |
||
92 | // we will set the operators to '==' and set the values appropriately. |
||
93 | if ($this->invalidOperator($operator)) { |
||
94 | [$value, $operator] = [$operator, '==']; |
||
95 | } |
||
96 | return [$value, $operator]; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param mixed $operator |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getType(mixed $operator): string |
||
104 | { |
||
105 | $type = 'Basic'; |
||
106 | |||
107 | if ($this->isBitwiseOperator($operator)) { |
||
108 | $type = 'Bitwise'; |
||
109 | } |
||
110 | |||
111 | return $type; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Add a date based (year, month, day, time) statement to the query. |
||
116 | * |
||
117 | * @param string $type |
||
118 | * @param string $column |
||
119 | * @param string $operator |
||
120 | * @param mixed $value |
||
121 | * @param string $boolean |
||
122 | * @return IlluminateQueryBuilder |
||
123 | */ |
||
124 | protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and') |
||
125 | { |
||
126 | $value = $this->bindValue($value); |
||
127 | |||
128 | $this->wheres[] = compact('column', 'type', 'boolean', 'operator', 'value'); |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Add another query builder as a nested where to the query builder. |
||
136 | * |
||
137 | * @param \Illuminate\Database\Query\Builder $query |
||
138 | * @param string $boolean |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function addNestedWhereQuery($query, $boolean = 'and') |
||
142 | { |
||
143 | if (count($query->wheres)) { |
||
144 | $type = 'Nested'; |
||
145 | |||
146 | $this->wheres[] = compact('type', 'query', 'boolean'); |
||
147 | $this->importBindings($query); |
||
148 | } |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add an exists clause to the query. |
||
155 | * |
||
156 | * @param \Illuminate\Database\Query\Builder $query |
||
157 | * @param string $boolean |
||
158 | * @param bool $not |
||
159 | * @return $this |
||
160 | * |
||
161 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
162 | */ |
||
163 | public function addWhereExistsQuery(IlluminateQueryBuilder $query, $boolean = 'and', $not = false) |
||
164 | { |
||
165 | $type = $not ? 'NotExists' : 'Exists'; |
||
166 | |||
167 | [$subquery] = $this->parseSub($query); |
||
168 | |||
169 | $this->wheres[] = compact('type', 'subquery', 'boolean'); |
||
170 | |||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Merge an array of where clauses and bindings. |
||
176 | * |
||
177 | * @param array<mixed> $wheres |
||
178 | * @param array<mixed> $bindings |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function mergeWheres($wheres, $bindings) |
||
182 | { |
||
183 | $this->wheres = array_merge($this->wheres, (array) $wheres); |
||
184 | |||
185 | $this->bindings['where'] = array_merge($this->bindings['where'], (array) $bindings); |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Add a basic where clause to the query. |
||
192 | * |
||
193 | * @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|Expression|string|array<mixed> $column |
||
194 | * @param mixed $operator |
||
195 | * @param mixed $value |
||
196 | * @param string $boolean |
||
197 | * @return IlluminateQueryBuilder |
||
198 | * |
||
199 | * @SuppressWarnings("PHPMD.CyclomaticComplexity") |
||
200 | * @SuppressWarnings("PHPMD.NPathComplexity") |
||
201 | */ |
||
202 | public function where($column, $operator = null, $value = null, $boolean = 'and') |
||
203 | { |
||
204 | if ($column instanceof ConditionExpression) { |
||
205 | $type = 'Expression'; |
||
206 | |||
207 | $this->wheres[] = compact('type', 'column', 'boolean'); |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | // If the column is an array, we will assume it is an array of key-value pairs |
||
213 | // and can add them each as a where clause. We will maintain the boolean we |
||
214 | // received when the method was called and pass it into the nested where. |
||
215 | if (is_array($column) && !array_is_list($column)) { |
||
216 | return $this->addArrayOfWheres($column, $boolean); |
||
217 | } |
||
218 | |||
219 | // Here we will make some assumptions about the operator. If only 2 values are |
||
220 | // passed to the method, we will assume that the operator is an equals sign |
||
221 | // and keep going. Otherwise, we'll require the operator to be passed in. |
||
222 | [$value, $operator] = $this->prepareValueAndOperator( |
||
223 | $value, |
||
224 | $operator, |
||
225 | func_num_args() === 2, |
||
226 | ); |
||
227 | |||
228 | // If the column is actually a Closure instance, we will assume the developer |
||
229 | // wants to begin a nested where statement which is wrapped in parentheses. |
||
230 | // We will add that Closure to the query and return back out immediately. |
||
231 | if ($column instanceof Closure && is_null($operator)) { |
||
232 | return $this->whereNested($column, $boolean); |
||
233 | } |
||
234 | |||
235 | // If the column is a Closure instance and there is an operator value, we will |
||
236 | // assume the developer wants to run a subquery and then compare the result |
||
237 | // of that subquery with the given value that was provided to the method. |
||
238 | if ($this->isQueryable($column) && !is_null($operator)) { |
||
239 | /** @phpstan-ignore-next-line */ |
||
240 | [$subquery] = $this->createSub($column, true); |
||
241 | |||
242 | return $this->where(new Expression($subquery), $operator, $value, $boolean); |
||
243 | } |
||
244 | |||
245 | list($value, $operator) = $this->validateOperator($operator, $value); |
||
246 | |||
247 | // If the value is a Closure, it means the developer is performing an entire |
||
248 | // sub-select within the query and we will need to compile the sub-select |
||
249 | // within the where clause to get the appropriate query record results. |
||
250 | if ($this->isQueryable($value)) { |
||
251 | /** @phpstan-ignore-next-line */ |
||
252 | return $this->whereSub($column, $operator, $value, $boolean); |
||
253 | } |
||
254 | |||
255 | // If the value is "null", we will just assume the developer wants to add a |
||
256 | // where null clause to the query. So, we will allow a short-cut here to |
||
257 | // that method for convenience so the developer doesn't have to check. |
||
258 | if (is_null($value)) { |
||
259 | /** @phpstan-ignore-next-line */ |
||
260 | return $this->whereNull($column, $boolean, $operator !== '=='); |
||
261 | } |
||
262 | |||
263 | $type = $this->getType($operator); |
||
264 | |||
265 | $value = $this->bindValue($value); |
||
266 | |||
267 | // Now that we are working with just a simple query we can put the elements |
||
268 | // in our array and add the query binding to our array of bindings that |
||
269 | // will be bound to each SQL statements when it is finally executed. |
||
270 | $this->wheres[] = compact( |
||
271 | 'type', |
||
272 | 'column', |
||
273 | 'operator', |
||
274 | 'value', |
||
275 | 'boolean', |
||
276 | ); |
||
277 | |||
278 | return $this; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Add a where between statement to the query. |
||
283 | * |
||
284 | * @param string|Expression $column |
||
285 | * @param iterable<mixed> $values |
||
286 | * @param string $boolean |
||
287 | * @param bool $not |
||
288 | * @return IlluminateQueryBuilder |
||
289 | * |
||
290 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
291 | */ |
||
292 | public function whereBetween($column, iterable $values, $boolean = 'and', $not = false) |
||
293 | { |
||
294 | $type = 'between'; |
||
295 | |||
296 | if ($values instanceof CarbonPeriod) { |
||
297 | $values = $values->toArray(); |
||
298 | } |
||
299 | |||
300 | $values = array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2); |
||
301 | $values[0] = $this->bindValue($values[0]); |
||
302 | $values[1] = $this->bindValue($values[1]); |
||
303 | |||
304 | $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not'); |
||
305 | |||
306 | return $this; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Add a "where" clause comparing two columns to the query. |
||
311 | * |
||
312 | * @param Expression|string|array<mixed> $first |
||
313 | * @param string|null $operator |
||
314 | * @param Expression|string|null $second |
||
315 | * @param string|null $boolean |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') |
||
319 | { |
||
320 | // If the column is an array, we will assume it is an array of key-value pairs |
||
321 | // and can add them each as a where clause. We will maintain the boolean we |
||
322 | // received when the method was called and pass it into the nested where. |
||
323 | if (is_array($first) && !array_is_list($first) && $boolean !== null) { |
||
324 | return $this->addArrayOfWheres($first, $boolean, 'whereColumn'); |
||
325 | } |
||
326 | |||
327 | // If the given operator is not found in the list of valid operators we will |
||
328 | // assume that the developer is just short-cutting the '=' operators and |
||
329 | // we will set the operators to '=' and set the values appropriately. |
||
330 | if ($operator !== null && $this->invalidOperator($operator)) { |
||
331 | [$second, $operator] = [$operator, '=']; |
||
332 | } |
||
333 | |||
334 | // Finally, we will add this where clause into this array of clauses that we |
||
335 | // are building for the query. All of them will be compiled via a grammar |
||
336 | // once the query is about to be executed and run against the database. |
||
337 | $type = 'Column'; |
||
338 | |||
339 | $this->wheres[] = compact( |
||
340 | 'type', |
||
341 | 'first', |
||
342 | 'operator', |
||
343 | 'second', |
||
344 | 'boolean', |
||
345 | ); |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | |||
351 | /** |
||
352 | * Add a "where in" clause to the query. |
||
353 | * |
||
354 | * @param string $column |
||
355 | * @param mixed $values |
||
356 | * @param string $boolean |
||
357 | * @param bool $not |
||
358 | * @return IlluminateQueryBuilder |
||
359 | * |
||
360 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
361 | */ |
||
362 | public function whereIn($column, $values, $boolean = 'and', $not = false) |
||
363 | { |
||
364 | $type = $not ? 'NotIn' : 'In'; |
||
365 | |||
366 | // If the value is a query builder instance we will assume the developer wants to |
||
367 | // look for any values that exist within this given query. So, we will add the |
||
368 | // query accordingly so that this query is properly executed when it is run. |
||
369 | if ($this->isQueryable($values)) { |
||
370 | [$query, $bindings] = $this->createSub($values); |
||
371 | |||
372 | $values = [new Expression($query)]; |
||
373 | |||
374 | $this->addBinding($bindings, 'where'); |
||
375 | } |
||
376 | |||
377 | // Next, if the value is Arrayable we need to cast it to its raw array form so we |
||
378 | // have the underlying array value instead of an Arrayable object which is not |
||
379 | // able to be added as a binding, etc. We will then add to the wheres array. |
||
380 | if ($values instanceof Arrayable) { |
||
381 | $values = $values->toArray(); |
||
382 | } |
||
383 | |||
384 | $values = $this->bindValue($this->cleanBindings($values)); |
||
385 | |||
386 | $this->wheres[] = compact('type', 'column', 'values', 'boolean'); |
||
387 | |||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Add a "where JSON contains" clause to the query. |
||
393 | * |
||
394 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
395 | * |
||
396 | * @param string $column |
||
397 | * @param mixed $value |
||
398 | * @param string $boolean |
||
399 | * @param bool $not |
||
400 | * @return IlluminateQueryBuilder |
||
401 | */ |
||
402 | public function whereJsonContains($column, $value, $boolean = 'and', $not = false) |
||
403 | { |
||
404 | $type = 'JsonContains'; |
||
405 | |||
406 | $value = $this->bindValue($value); |
||
407 | |||
408 | $this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not'); |
||
409 | |||
410 | return $this; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Add a "where JSON length" clause to the query. |
||
415 | * |
||
416 | * @param string $column |
||
417 | * @param mixed $operator |
||
418 | * @param mixed $value |
||
419 | * @param string $boolean |
||
420 | * @return IlluminateQueryBuilder |
||
421 | */ |
||
422 | public function whereJsonLength($column, $operator, $value = null, $boolean = 'and') |
||
423 | { |
||
424 | $type = 'JsonLength'; |
||
425 | |||
426 | [$value, $operator] = $this->prepareValueAndOperator( |
||
427 | $value, |
||
428 | $operator, |
||
429 | func_num_args() === 2, |
||
430 | ); |
||
431 | |||
432 | $value = $this->bindValue((int) $this->flattenValue($value)); |
||
433 | |||
434 | $this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean'); |
||
435 | |||
436 | return $this; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Add a "where like" clause to the query. |
||
441 | * |
||
442 | * @param \Illuminate\Contracts\Database\Query\Expression|string $column |
||
443 | * @param string $value |
||
444 | * @param bool $caseSensitive |
||
445 | * @param string $boolean |
||
446 | * @param bool $not |
||
447 | * @return $this |
||
448 | * |
||
449 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
450 | */ |
||
451 | public function whereLike($column, $value, $caseSensitive = false, $boolean = 'and', $not = false) |
||
452 | { |
||
453 | $type = 'Like'; |
||
454 | |||
455 | $value = $this->bindValue($value); |
||
456 | |||
457 | $this->wheres[] = compact('type', 'column', 'value', 'caseSensitive', 'boolean', 'not'); |
||
458 | |||
459 | return $this; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Add a "where null" clause to the query. |
||
464 | * |
||
465 | * @param string|array<mixed>|\Illuminate\Contracts\Database\Query\Expression $columns |
||
466 | * @param string $boolean |
||
467 | * @param bool $not |
||
468 | * @return $this |
||
469 | * |
||
470 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||
471 | */ |
||
472 | public function whereNull($columns, $boolean = 'and', $not = false) |
||
473 | { |
||
474 | $type = $not ? 'NotNull' : 'Null'; |
||
475 | |||
476 | foreach (Arr::wrap($columns) as $column) { |
||
477 | $this->wheres[] = compact('type', 'column', 'boolean'); |
||
478 | } |
||
479 | |||
480 | return $this; |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Add a full sub-select to the query. |
||
485 | * |
||
486 | * @param \Illuminate\Contracts\Database\Query\Expression|string $column |
||
487 | * @param string $operator |
||
488 | * @param mixed $callback |
||
489 | * @param string $boolean |
||
490 | * @return $this |
||
491 | */ |
||
492 | protected function whereSub($column, $operator, $callback, $boolean) |
||
518 | } |
||
519 | } |
||
520 |