| Total Complexity | 52 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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) |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 44 | */ |
||
| 45 | public function build(InConditionInterface $expression, array &$params = []): string |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Builds $values to be used in {@see InCondition}. |
||
| 126 | * |
||
| 127 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 128 | * |
||
| 129 | * @psalm-return string[] |
||
| 130 | * |
||
| 131 | * @psalm-suppress MixedArrayTypeCoercion |
||
| 132 | * @psalm-suppress MixedArrayOffset |
||
| 133 | */ |
||
| 134 | protected function buildValues(InConditionInterface $condition, array|Traversable $values, array &$params = []): array |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Builds SQL for IN condition. |
||
| 173 | * |
||
| 174 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 175 | */ |
||
| 176 | protected function buildSubqueryInCondition( |
||
| 177 | string $operator, |
||
| 178 | iterable|string|Iterator $columns, |
||
| 179 | ExpressionInterface $values, |
||
| 180 | array &$params = [] |
||
| 181 | ): string { |
||
| 182 | $query = ''; |
||
| 183 | $sql = $this->queryBuilder->buildExpression($values, $params); |
||
| 184 | |||
| 185 | if (is_array($columns)) { |
||
| 186 | /** @psalm-var string[] $columns */ |
||
| 187 | foreach ($columns as $i => $col) { |
||
| 188 | if ($col instanceof ExpressionInterface) { |
||
| 189 | $columns[$i] = (string) $col; |
||
| 190 | continue; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!str_contains($col, '(')) { |
||
| 194 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $query = '(' . implode(', ', $columns) . ") $operator $sql"; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (is_string($columns) && !str_contains($columns, '(')) { |
||
| 202 | $columns = $this->queryBuilder->quoter()->quoteColumnName($columns); |
||
| 203 | $query = "$columns $operator $sql"; |
||
| 204 | } |
||
| 205 | |||
| 206 | return $query; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Builds SQL for IN condition. |
||
| 211 | */ |
||
| 212 | protected function buildCompositeInCondition( |
||
| 213 | string|null $operator, |
||
| 214 | array|Traversable $columns, |
||
| 215 | iterable|Iterator $values, |
||
| 216 | array &$params = [] |
||
| 217 | ): string { |
||
| 218 | $vss = []; |
||
| 219 | |||
| 220 | /** @psalm-var string[][] $values */ |
||
| 221 | foreach ($values as $value) { |
||
| 222 | $vs = []; |
||
| 223 | /** @psalm-var string[] $columns */ |
||
| 224 | foreach ($columns as $column) { |
||
| 225 | if (isset($value[$column])) { |
||
| 226 | $vs[] = $this->queryBuilder->bindParam($value[$column], $params); |
||
| 227 | } else { |
||
| 228 | $vs[] = 'NULL'; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | $vss[] = '(' . implode(', ', $vs) . ')'; |
||
| 232 | } |
||
| 233 | |||
| 234 | if (empty($vss)) { |
||
| 235 | return $operator === 'IN' ? '0=1' : ''; |
||
| 236 | } |
||
| 237 | |||
| 238 | $sqlColumns = []; |
||
| 239 | |||
| 240 | /** @psalm-var string[] $columns */ |
||
| 241 | foreach ($columns as $column) { |
||
| 242 | $sqlColumns[] = !str_contains($column, '(') |
||
| 243 | ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column; |
||
| 244 | } |
||
| 245 | |||
| 246 | return '(' . implode(', ', $sqlColumns) . ") $operator (" . implode(', ', $vss) . ')'; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Builds is null/is not null condition for column based on operator. |
||
| 251 | */ |
||
| 252 | protected function getNullCondition(string $operator, string $column): string |
||
| 253 | { |
||
| 254 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 255 | |||
| 256 | if ($operator === 'IN') { |
||
| 257 | return sprintf('%s IS NULL', $column); |
||
| 258 | } |
||
| 259 | |||
| 260 | return sprintf('%s IS NOT NULL', $column); |
||
| 261 | } |
||
| 262 | |||
| 263 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 279 | } |
||
| 280 | } |
||
| 281 |