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