| Total Complexity | 50 |
| Total Lines | 231 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like InConditionBuilder 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 InConditionBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class InConditionBuilder implements ExpressionBuilderInterface |
||
| 37 | { |
||
| 38 | public function __construct(private QueryBuilderInterface $queryBuilder) |
||
| 39 | { |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 44 | */ |
||
| 45 | public function build(InConditionInterface $expression, array &$params = []): string |
||
| 46 | { |
||
| 47 | $column = $expression->getColumn(); |
||
| 48 | $nullConditionOperator = ''; |
||
| 49 | $operator = strtoupper($expression->getOperator()); |
||
| 50 | $values = $expression->getValues(); |
||
| 51 | |||
| 52 | if ($column === []) { |
||
| 53 | /** no columns to test against */ |
||
| 54 | return $operator === 'IN' ? '0=1' : ''; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($values instanceof QueryInterface) { |
||
| 58 | return $this->buildSubqueryInCondition($operator, $column, $values, $params); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!is_array($values) && !is_iterable($values)) { |
||
| 62 | /** ensure values is an array */ |
||
| 63 | $values = (array) $values; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (is_array($column)) { |
||
| 67 | if (count($column) > 1) { |
||
| 68 | return $this->buildCompositeInCondition($operator, $column, $values, $params); |
||
|
|
|||
| 69 | } |
||
| 70 | |||
| 71 | /** @var mixed */ |
||
| 72 | $column = reset($column); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($column instanceof Iterator) { |
||
| 76 | if (iterator_count($column) > 1) { |
||
| 77 | return $this->buildCompositeInCondition($operator, $column, $values, $params); |
||
| 78 | } |
||
| 79 | |||
| 80 | $column->rewind(); |
||
| 81 | /** @var mixed */ |
||
| 82 | $column = $column->current(); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (is_array($values)) { |
||
| 86 | $rawValues = $values; |
||
| 87 | } else { |
||
| 88 | $rawValues = $this->getRawValuesFromTraversableObject($values); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (is_string($column) && in_array(null, $rawValues, true)) { |
||
| 92 | $nullCondition = $this->getNullCondition($operator, $column); |
||
| 93 | $nullConditionOperator = $operator === 'IN' ? 'OR' : 'AND'; |
||
| 94 | } |
||
| 95 | |||
| 96 | $sqlValues = $this->buildValues($expression, $values, $params); |
||
| 97 | |||
| 98 | if (empty($sqlValues)) { |
||
| 99 | return empty($nullCondition) ? ($operator === 'IN' ? '0=1' : '') : (string) $nullCondition; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (is_string($column) && !str_contains($column, '(')) { |
||
| 103 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (count($sqlValues) > 1) { |
||
| 107 | $sql = "$column $operator (" . implode(', ', $sqlValues) . ')'; |
||
| 108 | } else { |
||
| 109 | $operator = $operator === 'IN' ? '=' : '<>'; |
||
| 110 | $sql = (string) $column . $operator . reset($sqlValues); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** @var int|string|null $nullCondition */ |
||
| 114 | return isset($nullCondition) ? sprintf('%s %s %s', $sql, $nullConditionOperator, $nullCondition) : $sql; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Builds $values to be used in {@see InCondition}. |
||
| 119 | * |
||
| 120 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 121 | * |
||
| 122 | * @psalm-return string[] |
||
| 123 | * |
||
| 124 | * @psalm-suppress MixedArrayTypeCoercion |
||
| 125 | * @psalm-suppress MixedArrayOffset |
||
| 126 | */ |
||
| 127 | protected function buildValues(InConditionInterface $condition, array|Traversable $values, array &$params = []): array |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Builds SQL for IN condition. |
||
| 166 | * |
||
| 167 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 168 | */ |
||
| 169 | protected function buildSubqueryInCondition( |
||
| 170 | string $operator, |
||
| 171 | iterable|string|Iterator $columns, |
||
| 172 | ExpressionInterface $values, |
||
| 173 | array &$params = [] |
||
| 174 | ): string { |
||
| 175 | $query = ''; |
||
| 176 | $sql = $this->queryBuilder->buildExpression($values, $params); |
||
| 177 | |||
| 178 | if (is_array($columns)) { |
||
| 179 | /** @psalm-var string[] $columns */ |
||
| 180 | foreach ($columns as $i => $col) { |
||
| 181 | if (!str_contains($col, '(')) { |
||
| 182 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | $query = '(' . implode(', ', $columns) . ") $operator $sql"; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (is_string($columns) && !str_contains($columns, '(')) { |
||
| 190 | $columns = $this->queryBuilder->quoter()->quoteColumnName($columns); |
||
| 191 | $query = "$columns $operator $sql"; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $query; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Builds SQL for IN condition. |
||
| 199 | */ |
||
| 200 | protected function buildCompositeInCondition( |
||
| 201 | ?string $operator, |
||
| 202 | array|Traversable $columns, |
||
| 203 | iterable|Iterator $values, |
||
| 204 | array &$params = [] |
||
| 205 | ): string { |
||
| 206 | $vss = []; |
||
| 207 | |||
| 208 | /** @psalm-var string[][] $values */ |
||
| 209 | foreach ($values as $value) { |
||
| 210 | $vs = []; |
||
| 211 | /** @psalm-var string[] $columns */ |
||
| 212 | foreach ($columns as $column) { |
||
| 213 | if (isset($value[$column])) { |
||
| 214 | $vs[] = $this->queryBuilder->bindParam($value[$column], $params); |
||
| 215 | } else { |
||
| 216 | $vs[] = 'NULL'; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | $vss[] = '(' . implode(', ', $vs) . ')'; |
||
| 220 | } |
||
| 221 | |||
| 222 | if (empty($vss)) { |
||
| 223 | return $operator === 'IN' ? '0=1' : ''; |
||
| 224 | } |
||
| 225 | |||
| 226 | $sqlColumns = []; |
||
| 227 | |||
| 228 | /** @psalm-var string[] $columns */ |
||
| 229 | foreach ($columns as $column) { |
||
| 230 | $sqlColumns[] = !str_contains($column, '(') |
||
| 231 | ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column; |
||
| 232 | } |
||
| 233 | |||
| 234 | return '(' . implode(', ', $sqlColumns) . ") $operator (" . implode(', ', $vss) . ')'; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Builds is null/is not null condition for column based on operator. |
||
| 239 | */ |
||
| 240 | protected function getNullCondition(string $operator, string $column): string |
||
| 249 | } |
||
| 250 | |||
| 251 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 252 | { |
||
| 269 |