| 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 |
||
| 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|null $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 |
||
| 241 | { |
||
| 242 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 243 | |||
| 244 | if ($operator === 'IN') { |
||
| 245 | return sprintf('%s IS NULL', $column); |
||
| 246 | } |
||
| 247 | |||
| 248 | return sprintf('%s IS NOT NULL', $column); |
||
| 249 | } |
||
| 250 | |||
| 251 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 267 | } |
||
| 268 | } |
||
| 269 |