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