| Total Complexity | 52 |
| Total Lines | 245 |
| 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( |
||
| 178 | string $operator, |
||
| 179 | iterable|string|Iterator $columns, |
||
| 180 | ExpressionInterface $values, |
||
| 181 | array &$params = [] |
||
| 182 | ): string { |
||
| 183 | $query = ''; |
||
| 184 | $sql = $this->queryBuilder->buildExpression($values, $params); |
||
| 185 | |||
| 186 | if (is_array($columns)) { |
||
| 187 | /** @psalm-var string[] $columns */ |
||
| 188 | foreach ($columns as $i => $col) { |
||
| 189 | if ($col instanceof ExpressionInterface) { |
||
| 190 | /** @psalm-suppress InvalidCast */ |
||
| 191 | $columns[$i] = (string) $col; |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | |||
| 195 | if (!str_contains($col, '(')) { |
||
| 196 | $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($col); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | $query = '(' . implode(', ', $columns) . ") $operator $sql"; |
||
| 201 | } |
||
| 202 | |||
| 203 | if (is_string($columns) && !str_contains($columns, '(')) { |
||
| 204 | $columns = $this->queryBuilder->quoter()->quoteColumnName($columns); |
||
| 205 | $query = "$columns $operator $sql"; |
||
| 206 | } |
||
| 207 | |||
| 208 | return $query; |
||
| 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 (isset($value[$column])) { |
||
| 228 | $vs[] = $this->queryBuilder->bindParam($value[$column], $params); |
||
| 229 | } else { |
||
| 230 | $vs[] = 'NULL'; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | $vss[] = '(' . implode(', ', $vs) . ')'; |
||
| 234 | } |
||
| 235 | |||
| 236 | if (empty($vss)) { |
||
| 237 | return $operator === 'IN' ? '0=1' : ''; |
||
| 238 | } |
||
| 239 | |||
| 240 | $sqlColumns = []; |
||
| 241 | |||
| 242 | /** @psalm-var string[] $columns */ |
||
| 243 | foreach ($columns as $column) { |
||
| 244 | $sqlColumns[] = !str_contains($column, '(') |
||
| 245 | ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column; |
||
| 246 | } |
||
| 247 | |||
| 248 | return '(' . implode(', ', $sqlColumns) . ") $operator (" . implode(', ', $vss) . ')'; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Builds is null/is not null condition for column based on operator. |
||
| 253 | */ |
||
| 254 | protected function getNullCondition(string $operator, string $column): string |
||
| 255 | { |
||
| 256 | $column = $this->queryBuilder->quoter()->quoteColumnName($column); |
||
| 257 | |||
| 258 | if ($operator === 'IN') { |
||
| 259 | return sprintf('%s IS NULL', $column); |
||
| 260 | } |
||
| 261 | |||
| 262 | return sprintf('%s IS NOT NULL', $column); |
||
| 263 | } |
||
| 264 | |||
| 265 | protected function getRawValuesFromTraversableObject(Traversable $traversableObject): array |
||
| 281 | } |
||
| 282 | } |
||
| 283 |