| Total Complexity | 54 |
| Total Lines | 271 |
| 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(protected QueryBuilderInterface $queryBuilder) |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Build SQL for {@see InCondition}. |
||
| 44 | * |
||
| 45 | * @throws Exception |
||
| 46 | * @throws InvalidArgumentException |
||
| 47 | * @throws InvalidConfigException |
||
| 48 | * @throws NotSupportedException |
||
| 49 | */ |
||
| 50 | public function build(InConditionInterface $expression, array &$params = []): string |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Builds `$values` to use in {@see InCondition}. |
||
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | * @throws InvalidArgumentException |
||
| 134 | * @throws InvalidConfigException |
||
| 135 | * @throws NotSupportedException |
||
| 136 | * |
||
| 137 | * @psalm-return string[] |
||
| 138 | * |
||
| 139 | * @psalm-suppress MixedArrayTypeCoercion |
||
| 140 | * @psalm-suppress MixedArrayOffset |
||
| 141 | */ |
||
| 142 | protected function buildValues(InConditionInterface $condition, iterable $values, array &$params = []): array |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Build SQL for composite `IN` condition. |
||
| 184 | * |
||
| 185 | * @throws Exception |
||
| 186 | * @throws InvalidArgumentException |
||
| 187 | * @throws InvalidConfigException |
||
| 188 | * @throws NotSupportedException |
||
| 189 | */ |
||
| 190 | protected function buildSubqueryInCondition( |
||
| 191 | string $operator, |
||
| 192 | iterable|string|Iterator $columns, |
||
| 193 | ExpressionInterface $values, |
||
| 194 | array &$params = [] |
||
| 195 | ): string { |
||
| 196 | $query = ''; |
||
| 197 | $sql = $this->queryBuilder->buildExpression($values, $params); |
||
| 198 | |||
| 199 | if (is_array($columns)) { |
||
| 200 | /** @psalm-var string[] $columns */ |
||
| 201 | foreach ($columns as $i => $col) { |
||
| 202 | if ($col instanceof ExpressionInterface) { |
||
| 203 | $columns[$i] = $this->queryBuilder->buildExpression($col); |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | if (!str_contains($col, '(')) { |
||
| 208 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | $query = '(' . implode(', ', $columns) . ") $operator $sql"; |
||
| 213 | } |
||
| 214 | |||
| 215 | if (is_string($columns) && !str_contains($columns, '(')) { |
||
| 216 | $columns = $this->queryBuilder->quoter()->quoteColumnName($columns); |
||
| 217 | $query = "$columns $operator $sql"; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $query; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Builds an SQL statement for checking the existence of rows with the specified composite column values. |
||
| 225 | * |
||
| 226 | * @throws Exception |
||
| 227 | * @throws InvalidConfigException |
||
| 228 | * @throws InvalidArgumentException |
||
| 229 | * @throws NotSupportedException |
||
| 230 | */ |
||
| 231 | protected function buildCompositeInCondition( |
||
| 232 | string|null $operator, |
||
| 233 | iterable $columns, |
||
| 234 | iterable|Iterator $values, |
||
| 235 | array &$params = [] |
||
| 236 | ): string { |
||
| 237 | $vss = []; |
||
| 238 | |||
| 239 | /** @psalm-var string[][] $values */ |
||
| 240 | foreach ($values as $value) { |
||
| 241 | $vs = []; |
||
| 242 | /** @psalm-var string[] $columns */ |
||
| 243 | foreach ($columns as $column) { |
||
| 244 | if ($column instanceof ExpressionInterface) { |
||
| 245 | $column = $this->queryBuilder->buildExpression($column); |
||
| 246 | } |
||
| 247 | |||
| 248 | if (isset($value[$column])) { |
||
| 249 | $vs[] = $this->queryBuilder->bindParam($value[$column], $params); |
||
| 250 | } else { |
||
| 251 | $vs[] = 'NULL'; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | $vss[] = '(' . implode(', ', $vs) . ')'; |
||
| 255 | } |
||
| 256 | |||
| 257 | if (empty($vss)) { |
||
| 258 | return $operator === 'IN' ? '0=1' : ''; |
||
| 259 | } |
||
| 260 | |||
| 261 | $sqlColumns = []; |
||
| 262 | |||
| 263 | /** @psalm-var string[] $columns */ |
||
| 264 | foreach ($columns as $column) { |
||
| 265 | if ($column instanceof ExpressionInterface) { |
||
| 266 | $sqlColumns[] = $this->queryBuilder->buildExpression($column); |
||
| 267 | continue; |
||
| 268 | } |
||
| 269 | |||
| 270 | $sqlColumns[] = !str_contains($column, '(') |
||
| 271 | ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column; |
||
| 272 | } |
||
| 273 | |||
| 274 | return '(' . implode(', ', $sqlColumns) . ") $operator (" . implode(', ', $vss) . ')'; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * The Builds are `null/is` not `null` condition for column based on the operator. |
||
| 279 | */ |
||
| 280 | protected function getNullCondition(string $operator, string $column): string |
||
| 281 | { |
||
| 282 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 283 | |||
| 284 | if ($operator === 'IN') { |
||
| 285 | return sprintf('%s IS NULL', $column); |
||
| 286 | } |
||
| 287 | |||
| 288 | return sprintf('%s IS NOT NULL', $column); |
||
| 289 | } |
||
| 290 | |||
| 291 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 307 | } |
||
| 308 | } |
||
| 309 |