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