| Conditions | 23 |
| Paths | 2512 |
| Total Lines | 77 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function build(InConditionInterface $expression, array &$params = []): string |
||
| 46 | { |
||
| 47 | $column = $expression->getColumn(); |
||
| 48 | $operator = strtoupper($expression->getOperator()); |
||
| 49 | $values = $expression->getValues(); |
||
| 50 | |||
| 51 | if ($column === []) { |
||
| 52 | /** no columns to test against */ |
||
| 53 | return $operator === 'IN' ? '0=1' : ''; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($column instanceof ExpressionInterface) { |
||
| 57 | $column = (string) $column; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($values instanceof QueryInterface) { |
||
| 61 | return $this->buildSubqueryInCondition($operator, $column, $values, $params); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (!is_array($values) && !is_iterable($values)) { |
||
| 65 | /** ensure values is an array */ |
||
| 66 | $values = (array) $values; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (is_array($column)) { |
||
| 70 | if (count($column) > 1) { |
||
| 71 | return $this->buildCompositeInCondition($operator, $column, $values, $params); |
||
|
|
|||
| 72 | } |
||
| 73 | |||
| 74 | /** @var mixed */ |
||
| 75 | $column = reset($column); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($column instanceof Iterator) { |
||
| 79 | if (iterator_count($column) > 1) { |
||
| 80 | return $this->buildCompositeInCondition($operator, $column, $values, $params); |
||
| 81 | } |
||
| 82 | |||
| 83 | $column->rewind(); |
||
| 84 | /** @var mixed */ |
||
| 85 | $column = $column->current(); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (is_array($values)) { |
||
| 89 | $rawValues = $values; |
||
| 90 | } else { |
||
| 91 | $rawValues = $this->getRawValuesFromTraversableObject($values); |
||
| 92 | } |
||
| 93 | |||
| 94 | $nullCondition = null; |
||
| 95 | $nullConditionOperator = null; |
||
| 96 | if (is_string($column) && in_array(null, $rawValues, true)) { |
||
| 97 | $nullCondition = $this->getNullCondition($operator, $column); |
||
| 98 | $nullConditionOperator = $operator === 'IN' ? 'OR' : 'AND'; |
||
| 99 | } |
||
| 100 | |||
| 101 | $sqlValues = $this->buildValues($expression, $values, $params); |
||
| 102 | |||
| 103 | if (empty($sqlValues)) { |
||
| 104 | return $nullCondition ?? ($operator === 'IN' ? '0=1' : ''); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (is_string($column) && !str_contains($column, '(')) { |
||
| 108 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (count($sqlValues) > 1) { |
||
| 112 | $sql = "$column $operator (" . implode(', ', $sqlValues) . ')'; |
||
| 113 | } else { |
||
| 114 | $operator = $operator === 'IN' ? '=' : '<>'; |
||
| 115 | $sql = (string) $column . $operator . reset($sqlValues); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** @var int|string|null $nullCondition */ |
||
| 119 | return $nullCondition !== null && $nullConditionOperator !== null |
||
| 120 | ? sprintf('%s %s %s', $sql, $nullConditionOperator, $nullCondition) |
||
| 121 | : $sql; |
||
| 122 | } |
||
| 281 |