| Total Complexity | 54 |
| Total Lines | 256 |
| 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 |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Builds $values to be used in {@see InCondition}. |
||
| 127 | * |
||
| 128 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 129 | * |
||
| 130 | * @psalm-return string[] |
||
| 131 | * |
||
| 132 | * @psalm-suppress MixedArrayTypeCoercion |
||
| 133 | * @psalm-suppress MixedArrayOffset |
||
| 134 | */ |
||
| 135 | protected function buildValues(InConditionInterface $condition, array|Traversable $values, array &$params = []): array |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Builds SQL for IN condition. |
||
| 174 | * |
||
| 175 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 176 | */ |
||
| 177 | protected function buildSubqueryInCondition( |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Builds SQL for IN condition. |
||
| 213 | */ |
||
| 214 | protected function buildCompositeInCondition( |
||
| 215 | string|null $operator, |
||
| 216 | array|Traversable $columns, |
||
| 217 | iterable|Iterator $values, |
||
| 218 | array &$params = [] |
||
| 219 | ): string { |
||
| 220 | $vss = []; |
||
| 221 | |||
| 222 | /** @psalm-var string[][] $values */ |
||
| 223 | foreach ($values as $value) { |
||
| 224 | $vs = []; |
||
| 225 | /** @psalm-var string[] $columns */ |
||
| 226 | foreach ($columns as $column) { |
||
| 227 | if ($column instanceof ExpressionInterface) { |
||
| 228 | /** @psalm-suppress InvalidCast */ |
||
| 229 | $column = (string) $column; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (isset($value[$column])) { |
||
| 233 | $vs[] = $this->queryBuilder->bindParam($value[$column], $params); |
||
| 234 | } else { |
||
| 235 | $vs[] = 'NULL'; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | $vss[] = '(' . implode(', ', $vs) . ')'; |
||
| 239 | } |
||
| 240 | |||
| 241 | if (empty($vss)) { |
||
| 242 | return $operator === 'IN' ? '0=1' : ''; |
||
| 243 | } |
||
| 244 | |||
| 245 | $sqlColumns = []; |
||
| 246 | |||
| 247 | /** @psalm-var string[] $columns */ |
||
| 248 | foreach ($columns as $column) { |
||
| 249 | if ($column instanceof ExpressionInterface) { |
||
| 250 | /** @psalm-suppress InvalidCast */ |
||
| 251 | $sqlColumns[] = (string) $column; |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | $sqlColumns[] = !str_contains($column, '(') |
||
| 256 | ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column; |
||
| 257 | } |
||
| 258 | |||
| 259 | return '(' . implode(', ', $sqlColumns) . ") $operator (" . implode(', ', $vss) . ')'; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Builds is null/is not null condition for column based on operator. |
||
| 264 | */ |
||
| 265 | protected function getNullCondition(string $operator, string $column): string |
||
| 266 | { |
||
| 267 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 268 | |||
| 269 | if ($operator === 'IN') { |
||
| 270 | return sprintf('%s IS NULL', $column); |
||
| 271 | } |
||
| 272 | |||
| 273 | return sprintf('%s IS NOT NULL', $column); |
||
| 274 | } |
||
| 275 | |||
| 276 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 292 | } |
||
| 293 | } |
||
| 294 |