| Total Complexity | 60 | 
| Total Lines | 426 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like DMLQueryBuilder 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 DMLQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 35 | abstract class DMLQueryBuilder implements DMLQueryBuilderInterface  | 
            ||
| 36 | { | 
            ||
| 37 | public function __construct(  | 
            ||
| 42 | }  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * @psalm-param string[] $columns  | 
            ||
| 46 | * @psalm-suppress MixedArrayOffset  | 
            ||
| 47 | */  | 
            ||
| 48 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException  | 
            ||
| 96 | */  | 
            ||
| 97 | public function delete(string $table, array|string $condition, array &$params): string  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 106 |     { | 
            ||
| 107 | /**  | 
            ||
| 108 | * @psalm-var string[] $names  | 
            ||
| 109 | * @psalm-var string[] $placeholders  | 
            ||
| 110 | * @psalm-var string $values  | 
            ||
| 111 | */  | 
            ||
| 112 | [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params);  | 
            ||
| 113 | |||
| 114 | return 'INSERT INTO '  | 
            ||
| 115 | . $this->quoter->quoteTableName($table)  | 
            ||
| 116 |             . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') | 
            ||
| 117 |             . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException  | 
            ||
| 122 | */  | 
            ||
| 123 | public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 126 | }  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * @throws NotSupportedException  | 
            ||
| 130 | */  | 
            ||
| 131 | public function resetSequence(string $tableName, int|string|null $value = null): string  | 
            ||
| 132 |     { | 
            ||
| 133 | throw new NotSupportedException(__METHOD__ . '() is not supported by this DBMS.');  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * @psalm-suppress MixedArgument  | 
            ||
| 138 | */  | 
            ||
| 139 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string  | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * @throws NotSupportedException  | 
            ||
| 151 | */  | 
            ||
| 152 | public function upsert(  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | /**  | 
            ||
| 162 | * Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement.  | 
            ||
| 163 | *  | 
            ||
| 164 | * @param QueryInterface $columns Object, which represents select query.  | 
            ||
| 165 | * @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included  | 
            ||
| 166 | * in the result with the additional parameters generated during the query building process.  | 
            ||
| 167 | *  | 
            ||
| 168 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException  | 
            ||
| 169 | *  | 
            ||
| 170 | * @return array array of column names, values and params.  | 
            ||
| 171 | */  | 
            ||
| 172 | protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $params = []): array  | 
            ||
| 173 |     { | 
            ||
| 174 |         if (empty($columns->getSelect()) || in_array('*', $columns->getSelect(), true)) { | 
            ||
| 175 |             throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters'); | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | [$values, $params] = $this->queryBuilder->build($columns, $params);  | 
            ||
| 179 | |||
| 180 | $names = [];  | 
            ||
| 181 | $values = ' ' . $values;  | 
            ||
| 182 | /** @psalm-var string[] $select */  | 
            ||
| 183 | $select = $columns->getSelect();  | 
            ||
| 184 | |||
| 185 |         foreach ($select as $title => $field) { | 
            ||
| 186 |             if (is_string($title)) { | 
            ||
| 187 | $names[] = $this->quoter->quoteColumnName($title);  | 
            ||
| 188 |             } else { | 
            ||
| 189 |                 if ($field instanceof ExpressionInterface) { | 
            ||
| 190 | $field = $this->queryBuilder->buildExpression($field, $params);  | 
            ||
| 191 | }  | 
            ||
| 192 | |||
| 193 |                 if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $field, $matches)) { | 
            ||
| 194 | $names[] = $this->quoter->quoteColumnName($matches[2]);  | 
            ||
| 195 |                 } else { | 
            ||
| 196 | $names[] = $this->quoter->quoteColumnName($field);  | 
            ||
| 197 | }  | 
            ||
| 198 | }  | 
            ||
| 199 | }  | 
            ||
| 200 | |||
| 201 | return [$names, $values, $params];  | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 | protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array  | 
            ||
| 205 |     { | 
            ||
| 206 | $tableSchema = $this->schema->getTableSchema($table);  | 
            ||
| 207 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : [];  | 
            ||
| 208 | $names = [];  | 
            ||
| 209 | $placeholders = [];  | 
            ||
| 210 | $values = ' DEFAULT VALUES';  | 
            ||
| 211 | |||
| 212 |         if ($columns instanceof QueryInterface) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 213 | [$names, $values, $params] = $this->prepareInsertSelectSubQuery($columns, $params);  | 
            ||
| 214 |         } else { | 
            ||
| 215 | $columns = $this->normalizeColumnNames($table, $columns);  | 
            ||
| 216 | /**  | 
            ||
| 217 | * @var mixed $value  | 
            ||
| 218 | * @psalm-var array<string, mixed> $columns  | 
            ||
| 219 | */  | 
            ||
| 220 |             foreach ($columns as $name => $value) { | 
            ||
| 221 | $names[] = $this->quoter->quoteColumnName($name);  | 
            ||
| 222 | /** @var mixed $value */  | 
            ||
| 223 | $value = $this->getTypecastValue($value, $columnSchemas[$name] ?? null);  | 
            ||
| 224 | |||
| 225 |                 if ($value instanceof ExpressionInterface) { | 
            ||
| 226 | $placeholders[] = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 227 |                 } else { | 
            ||
| 228 | $placeholders[] = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 229 | }  | 
            ||
| 230 | }  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | return [$names, $placeholders, $values, $params];  | 
            ||
| 234 | }  | 
            ||
| 235 | |||
| 236 | protected function prepareUpdateSets(string $table, array $columns, array $params = []): array  | 
            ||
| 237 |     { | 
            ||
| 238 | $tableSchema = $this->schema->getTableSchema($table);  | 
            ||
| 239 | |||
| 240 | $columnSchemas = $tableSchema !== null ? $tableSchema->getColumns() : [];  | 
            ||
| 241 | |||
| 242 | $sets = [];  | 
            ||
| 243 | |||
| 244 | $columns = $this->normalizeColumnNames($table, $columns);  | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * @psalm-var array<string, mixed> $columns  | 
            ||
| 248 | * @psalm-var mixed $value  | 
            ||
| 249 | */  | 
            ||
| 250 |         foreach ($columns as $name => $value) { | 
            ||
| 251 | /** @psalm-var mixed $value */  | 
            ||
| 252 | $value = isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value;  | 
            ||
| 253 |             if ($value instanceof ExpressionInterface) { | 
            ||
| 254 | $placeholder = $this->queryBuilder->buildExpression($value, $params);  | 
            ||
| 255 |             } else { | 
            ||
| 256 | $placeholder = $this->queryBuilder->bindParam($value, $params);  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | $sets[] = $this->quoter->quoteColumnName($name) . '=' . $placeholder;  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | return [$sets, $params];  | 
            ||
| 263 | }  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * @psalm-param Constraint[] $constraints  | 
            ||
| 267 | *  | 
            ||
| 268 | * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException  | 
            ||
| 269 | */  | 
            ||
| 270 | protected function prepareUpsertColumns(  | 
            ||
| 271 | string $table,  | 
            ||
| 272 | QueryInterface|array $insertColumns,  | 
            ||
| 273 | QueryInterface|bool|array $updateColumns,  | 
            ||
| 274 | array &$constraints = []  | 
            ||
| 275 |     ): array { | 
            ||
| 276 | $insertNames = [];  | 
            ||
| 277 | |||
| 278 |         if (!$insertColumns instanceof QueryInterface) { | 
            ||
| 279 | $insertColumns = $this->normalizeColumnNames($table, $insertColumns);  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 |         if (is_array($updateColumns)) { | 
            ||
| 283 | $updateColumns = $this->normalizeColumnNames($table, $updateColumns);  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 |         if ($insertColumns instanceof QueryInterface) { | 
            ||
| 287 | /** @psalm-var list<string> $insertNames */  | 
            ||
| 288 | [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns);  | 
            ||
| 289 |         } else { | 
            ||
| 290 | /** @psalm-var array<string, string> $insertColumns */  | 
            ||
| 291 |             foreach ($insertColumns as $key => $_value) { | 
            ||
| 292 | $insertNames[] = $this->quoter->quoteColumnName($key);  | 
            ||
| 293 | }  | 
            ||
| 294 | }  | 
            ||
| 295 | |||
| 296 | /** @psalm-var string[] $uniqueNames */  | 
            ||
| 297 | $uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints);  | 
            ||
| 298 | |||
| 299 |         foreach ($uniqueNames as $key => $name) { | 
            ||
| 300 | $insertNames[$key] = $this->quoter->quoteColumnName($name);  | 
            ||
| 301 | }  | 
            ||
| 302 | |||
| 303 |         if ($updateColumns !== true) { | 
            ||
| 304 | return [$uniqueNames, $insertNames, null];  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | return [$uniqueNames, $insertNames, array_diff($insertNames, $uniqueNames)];  | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * Returns all column names belonging to constraints enforcing uniqueness (`PRIMARY KEY`, `UNIQUE INDEX`, etc.)  | 
            ||
| 312 | * for the named table removing constraints which did not cover the specified column list.  | 
            ||
| 313 | *  | 
            ||
| 314 | * The column list will be unique by column names.  | 
            ||
| 315 | *  | 
            ||
| 316 | * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.  | 
            ||
| 317 | * @param string[] $columns source column list.  | 
            ||
| 318 | * @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints  | 
            ||
| 319 | * will be unique by their column names.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @throws JsonException  | 
            ||
| 322 | *  | 
            ||
| 323 | * @return array column list.  | 
            ||
| 324 | * @psalm-suppress ReferenceConstraintViolation  | 
            ||
| 325 | */  | 
            ||
| 326 | private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array  | 
            ||
| 391 | }  | 
            ||
| 392 | |||
| 393 | protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed  | 
            ||
| 400 | }  | 
            ||
| 401 | |||
| 402 | /**  | 
            ||
| 403 | * Normalizes column names  | 
            ||
| 404 | *  | 
            ||
| 405 | * @param string $table the table that data will be saved into.  | 
            ||
| 406 | * @param array $columns the column data (name => value) to be saved into the table or instance of  | 
            ||
| 407 |      * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of | 
            ||
| 408 |      * {@see QueryInterface}. | 
            ||
| 409 | *  | 
            ||
| 410 | * @return array normalized columns.  | 
            ||
| 411 | */  | 
            ||
| 412 | protected function normalizeColumnNames(string $table, array $columns): array  | 
            ||
| 432 | }  | 
            ||
| 433 | |||
| 434 | /**  | 
            ||
| 435 | * Get map of normalized columns  | 
            ||
| 436 | *  | 
            ||
| 437 | * @param string $table  | 
            ||
| 438 | * @param string[] $columns  | 
            ||
| 439 | *  | 
            ||
| 440 | * @return string[]  | 
            ||
| 441 | */  | 
            ||
| 442 | protected function getNormalizeColumnNames(string $table, array $columns): array  | 
            ||
| 461 | }  | 
            ||
| 462 | }  | 
            ||
| 463 |