| Total Complexity | 66 | 
| Total Lines | 381 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like AbstractQueryBuilder 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 AbstractQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 44 | abstract class AbstractQueryBuilder implements QueryBuilderInterface  | 
            ||
| 45 | { | 
            ||
| 46 | /**  | 
            ||
| 47 |      * Defines a UNIQUE index type for {@see createIndex()}. | 
            ||
| 48 | */  | 
            ||
| 49 | public const INDEX_UNIQUE = 'UNIQUE';  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * The prefix for automatically generated query binding parameters.  | 
            ||
| 53 | */  | 
            ||
| 54 | public const PARAM_PREFIX = ':qp';  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @var array the abstract column types mapped to physical column types.  | 
            ||
| 58 | * This is mainly used to support creating/modifying tables using DB-independent data type specifications.  | 
            ||
| 59 | * Child classes should override this property to declare supported type mappings.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @psalm-var string[]  | 
            ||
| 62 | */  | 
            ||
| 63 | protected array $typeMap = [];  | 
            ||
| 64 | |||
| 65 | public function __construct(  | 
            ||
| 66 | private QuoterInterface $quoter,  | 
            ||
| 67 | private SchemaInterface $schema,  | 
            ||
| 68 | private AbstractDDLQueryBuilder $ddlBuilder,  | 
            ||
| 69 | private AbstractDMLQueryBuilder $dmlBuilder,  | 
            ||
| 70 | private AbstractDQLQueryBuilder $dqlBuilder  | 
            ||
| 71 |     ) { | 
            ||
| 72 | }  | 
            ||
| 73 | |||
| 74 | public function addCheck(string $name, string $table, string $expression): string  | 
            ||
| 75 |     { | 
            ||
| 76 | return $this->ddlBuilder->addCheck($name, $table, $expression);  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | public function addColumn(string $table, string $column, string $type): string  | 
            ||
| 80 |     { | 
            ||
| 81 | return $this->ddlBuilder->addColumn($table, $column, $type);  | 
            ||
| 82 | }  | 
            ||
| 83 | |||
| 84 | public function addCommentOnColumn(string $table, string $column, string $comment): string  | 
            ||
| 85 |     { | 
            ||
| 86 | return $this->ddlBuilder->addCommentOnColumn($table, $column, $comment);  | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 | public function addCommentOnTable(string $table, string $comment): string  | 
            ||
| 90 |     { | 
            ||
| 91 | return $this->ddlBuilder->addCommentOnTable($table, $comment);  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | public function addDefaultValue(string $name, string $table, string $column, mixed $value): string  | 
            ||
| 95 |     { | 
            ||
| 96 | return $this->ddlBuilder->addDefaultValue($name, $table, $column, $value);  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | public function addForeignKey(  | 
            ||
| 100 | string $name,  | 
            ||
| 101 | string $table,  | 
            ||
| 102 | array|string $columns,  | 
            ||
| 103 | string $refTable,  | 
            ||
| 104 | array|string $refColumns,  | 
            ||
| 105 | string $delete = null,  | 
            ||
| 106 | string $update = null  | 
            ||
| 107 |     ): string { | 
            ||
| 108 | return $this->ddlBuilder->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update);  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 | public function addPrimaryKey(string $name, string $table, array|string $columns): string  | 
            ||
| 112 |     { | 
            ||
| 113 | return $this->ddlBuilder->addPrimaryKey($name, $table, $columns);  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | public function addUnique(string $name, string $table, array|string $columns): string  | 
            ||
| 117 |     { | 
            ||
| 118 | return $this->ddlBuilder->addUnique($name, $table, $columns);  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string  | 
            ||
| 122 |     { | 
            ||
| 123 | return $this->ddlBuilder->alterColumn($table, $column, $type);  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string  | 
            ||
| 127 |     { | 
            ||
| 128 | return $this->dmlBuilder->batchInsert($table, $columns, $rows, $params);  | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 | public function bindParam(mixed $value, array &$params = []): string  | 
            ||
| 132 |     { | 
            ||
| 133 | $phName = self::PARAM_PREFIX . count($params);  | 
            ||
| 134 | |||
| 135 | $additionalCount = 0;  | 
            ||
| 136 |         while (isset($params[$phName])) { | 
            ||
| 137 | $phName = self::PARAM_PREFIX . count($params) . '_' . $additionalCount;  | 
            ||
| 138 | ++$additionalCount;  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | /** @psalm-var mixed */  | 
            ||
| 142 | $params[$phName] = $value;  | 
            ||
| 143 | |||
| 144 | return $phName;  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | public function build(QueryInterface $query, array $params = []): array  | 
            ||
| 148 |     { | 
            ||
| 149 | return $this->dqlBuilder->build($query, $params);  | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | public function buildColumns(array|string $columns): string  | 
            ||
| 153 |     { | 
            ||
| 154 | return $this->dqlBuilder->buildColumns($columns);  | 
            ||
| 155 | }  | 
            ||
| 156 | |||
| 157 | public function buildCondition(array|string|ExpressionInterface|null $condition, array &$params = []): string  | 
            ||
| 158 |     { | 
            ||
| 159 | return $this->dqlBuilder->buildCondition($condition, $params);  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | public function buildExpression(ExpressionInterface $expression, array &$params = []): string  | 
            ||
| 163 |     { | 
            ||
| 164 | return $this->dqlBuilder->buildExpression($expression, $params);  | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | public function buildFrom(array|null $tables, array &$params): string  | 
            ||
| 168 |     { | 
            ||
| 169 | return $this->dqlBuilder->buildFrom($tables, $params);  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 | public function buildGroupBy(array $columns, array &$params = []): string  | 
            ||
| 173 |     { | 
            ||
| 174 | return $this->dqlBuilder->buildGroupBy($columns, $params);  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | public function buildHaving(array|ExpressionInterface|string|null $condition, array &$params = []): string  | 
            ||
| 178 |     { | 
            ||
| 179 | return $this->dqlBuilder->buildHaving($condition, $params);  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | public function buildJoin(array $joins, array &$params): string  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | public function buildLimit(Expression|int|null $limit, Expression|int|null $offset): string  | 
            ||
| 188 |     { | 
            ||
| 189 | return $this->dqlBuilder->buildLimit($limit, $offset);  | 
            ||
| 190 | }  | 
            ||
| 191 | |||
| 192 | public function buildOrderBy(array $columns, array &$params = []): string  | 
            ||
| 193 |     { | 
            ||
| 194 | return $this->dqlBuilder->buildOrderBy($columns, $params);  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | public function buildOrderByAndLimit(  | 
            ||
| 198 | string $sql,  | 
            ||
| 199 | array $orderBy,  | 
            ||
| 200 | Expression|int|null $limit,  | 
            ||
| 201 | Expression|int|null $offset,  | 
            ||
| 202 | array &$params = []  | 
            ||
| 203 |     ): string { | 
            ||
| 204 | return $this->dqlBuilder->buildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params);  | 
            ||
| 205 | }  | 
            ||
| 206 | |||
| 207 | public function buildSelect(  | 
            ||
| 208 | array $columns,  | 
            ||
| 209 | array &$params,  | 
            ||
| 210 | bool|null $distinct = false,  | 
            ||
| 211 | string $selectOption = null  | 
            ||
| 212 |     ): string { | 
            ||
| 213 | return $this->dqlBuilder->buildSelect($columns, $params, $distinct, $selectOption);  | 
            ||
| 214 | }  | 
            ||
| 215 | |||
| 216 | public function buildUnion(array $unions, array &$params): string  | 
            ||
| 217 |     { | 
            ||
| 218 | return $this->dqlBuilder->buildUnion($unions, $params);  | 
            ||
| 219 | }  | 
            ||
| 220 | |||
| 221 | public function buildWhere(  | 
            ||
| 222 | array|string|ConditionInterface|ExpressionInterface|null $condition,  | 
            ||
| 223 | array &$params = []  | 
            ||
| 224 |     ): string { | 
            ||
| 225 | return $this->dqlBuilder->buildWhere($condition, $params);  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | public function buildWithQueries(array $withs, array &$params): string  | 
            ||
| 229 |     { | 
            ||
| 230 | return $this->dqlBuilder->buildWithQueries($withs, $params);  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string  | 
            ||
| 234 |     { | 
            ||
| 235 | return $this->ddlBuilder->checkIntegrity($schema, $table, $check);  | 
            ||
| 236 | }  | 
            ||
| 237 | |||
| 238 | public function createConditionFromArray(array $condition): ConditionInterface  | 
            ||
| 239 |     { | 
            ||
| 240 | return $this->dqlBuilder->createConditionFromArray($condition);  | 
            ||
| 241 | }  | 
            ||
| 242 | |||
| 243 | public function createIndex(  | 
            ||
| 244 | string $name,  | 
            ||
| 245 | string $table,  | 
            ||
| 246 | array|string $columns,  | 
            ||
| 247 | string $indexType = null,  | 
            ||
| 248 | string $indexMethod = null  | 
            ||
| 249 |     ): string { | 
            ||
| 250 | return $this->ddlBuilder->createIndex($name, $table, $columns, $indexType, $indexMethod);  | 
            ||
| 251 | }  | 
            ||
| 252 | |||
| 253 | public function createTable(string $table, array $columns, string $options = null): string  | 
            ||
| 254 |     { | 
            ||
| 255 | return $this->ddlBuilder->createTable($table, $columns, $options);  | 
            ||
| 256 | }  | 
            ||
| 257 | |||
| 258 | public function createView(string $viewName, QueryInterface|string $subQuery): string  | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 263 | public function delete(string $table, array|string $condition, array &$params): string  | 
            ||
| 264 |     { | 
            ||
| 265 | return $this->dmlBuilder->delete($table, $condition, $params);  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | public function dropCheck(string $name, string $table): string  | 
            ||
| 269 |     { | 
            ||
| 270 | return $this->ddlBuilder->dropCheck($name, $table);  | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 | public function dropColumn(string $table, string $column): string  | 
            ||
| 274 |     { | 
            ||
| 275 | return $this->ddlBuilder->dropColumn($table, $column);  | 
            ||
| 276 | }  | 
            ||
| 277 | |||
| 278 | public function dropCommentFromColumn(string $table, string $column): string  | 
            ||
| 279 |     { | 
            ||
| 280 | return $this->ddlBuilder->dropCommentFromColumn($table, $column);  | 
            ||
| 281 | }  | 
            ||
| 282 | |||
| 283 | public function dropCommentFromTable(string $table): string  | 
            ||
| 284 |     { | 
            ||
| 285 | return $this->ddlBuilder->dropCommentFromTable($table);  | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | public function dropDefaultValue(string $name, string $table): string  | 
            ||
| 289 |     { | 
            ||
| 290 | return $this->ddlBuilder->dropDefaultValue($name, $table);  | 
            ||
| 291 | }  | 
            ||
| 292 | |||
| 293 | public function dropForeignKey(string $name, string $table): string  | 
            ||
| 294 |     { | 
            ||
| 295 | return $this->ddlBuilder->dropForeignKey($name, $table);  | 
            ||
| 296 | }  | 
            ||
| 297 | |||
| 298 | public function dropIndex(string $name, string $table): string  | 
            ||
| 299 |     { | 
            ||
| 300 | return $this->ddlBuilder->dropIndex($name, $table);  | 
            ||
| 301 | }  | 
            ||
| 302 | |||
| 303 | public function dropPrimaryKey(string $name, string $table): string  | 
            ||
| 304 |     { | 
            ||
| 305 | return $this->ddlBuilder->dropPrimaryKey($name, $table);  | 
            ||
| 306 | }  | 
            ||
| 307 | |||
| 308 | public function dropTable(string $table): string  | 
            ||
| 309 |     { | 
            ||
| 310 | return $this->ddlBuilder->dropTable($table);  | 
            ||
| 311 | }  | 
            ||
| 312 | |||
| 313 | public function dropUnique(string $name, string $table): string  | 
            ||
| 314 |     { | 
            ||
| 315 | return $this->ddlBuilder->dropUnique($name, $table);  | 
            ||
| 316 | }  | 
            ||
| 317 | |||
| 318 | public function dropView(string $viewName): string  | 
            ||
| 319 |     { | 
            ||
| 320 | return $this->ddlBuilder->dropView($viewName);  | 
            ||
| 321 | }  | 
            ||
| 322 | |||
| 323 | public function getColumnType(ColumnSchemaBuilder|string $type): string  | 
            ||
| 324 |     { | 
            ||
| 325 |         if ($type instanceof ColumnSchemaBuilder) { | 
            ||
| 326 | $type = $type->__toString();  | 
            ||
| 327 | }  | 
            ||
| 328 | |||
| 329 |         if (isset($this->typeMap[$type])) { | 
            ||
| 330 | return $this->typeMap[$type];  | 
            ||
| 331 | }  | 
            ||
| 332 | |||
| 333 |         if (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) { | 
            ||
| 334 |             if (isset($this->typeMap[$matches[1]])) { | 
            ||
| 335 | return preg_replace(  | 
            ||
| 336 | '/\(.+\)/',  | 
            ||
| 337 |                     '(' . $matches[2] . ')', | 
            ||
| 338 | $this->typeMap[$matches[1]]  | 
            ||
| 339 | ) . $matches[3];  | 
            ||
| 340 | }  | 
            ||
| 341 |         } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) { | 
            ||
| 342 |             if (isset($this->typeMap[$matches[1]])) { | 
            ||
| 343 |                 return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type); | 
            ||
| 344 | }  | 
            ||
| 345 | }  | 
            ||
| 346 | |||
| 347 | return $type;  | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | public function getExpressionBuilder(ExpressionInterface $expression): object  | 
            ||
| 351 |     { | 
            ||
| 352 | return $this->dqlBuilder->getExpressionBuilder($expression);  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 356 |     { | 
            ||
| 357 | return $this->dmlBuilder->insert($table, $columns, $params);  | 
            ||
| 358 | }  | 
            ||
| 359 | |||
| 360 | /**  | 
            ||
| 361 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException  | 
            ||
| 362 | */  | 
            ||
| 363 | public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string  | 
            ||
| 364 |     { | 
            ||
| 365 | return $this->dmlBuilder->insertWithReturningPks($table, $columns, $params);  | 
            ||
| 366 | }  | 
            ||
| 367 | |||
| 368 | public function quoter(): QuoterInterface  | 
            ||
| 369 |     { | 
            ||
| 370 | return $this->quoter;  | 
            ||
| 371 | }  | 
            ||
| 372 | |||
| 373 | public function renameColumn(string $table, string $oldName, string $newName): string  | 
            ||
| 374 |     { | 
            ||
| 375 | return $this->ddlBuilder->renameColumn($table, $oldName, $newName);  | 
            ||
| 376 | }  | 
            ||
| 377 | |||
| 378 | public function renameTable(string $oldName, string $newName): string  | 
            ||
| 379 |     { | 
            ||
| 380 | return $this->ddlBuilder->renameTable($oldName, $newName);  | 
            ||
| 381 | }  | 
            ||
| 382 | |||
| 383 | public function resetSequence(string $tableName, int|string|null $value = null): string  | 
            ||
| 384 |     { | 
            ||
| 385 | return $this->dmlBuilder->resetSequence($tableName, $value);  | 
            ||
| 386 | }  | 
            ||
| 387 | |||
| 388 | public function selectExists(string $rawSql): string  | 
            ||
| 389 |     { | 
            ||
| 390 | return $this->dqlBuilder->selectExists($rawSql);  | 
            ||
| 391 | }  | 
            ||
| 392 | |||
| 393 | public function setConditionClasses(array $classes): void  | 
            ||
| 394 |     { | 
            ||
| 395 | $this->dqlBuilder->setConditionClasses($classes);  | 
            ||
| 396 | }  | 
            ||
| 397 | |||
| 398 | public function setExpressionBuilders(array $builders): void  | 
            ||
| 399 |     { | 
            ||
| 400 | $this->dqlBuilder->setExpressionBuilders($builders);  | 
            ||
| 401 | }  | 
            ||
| 402 | |||
| 403 | public function setSeparator(string $separator): void  | 
            ||
| 404 |     { | 
            ||
| 405 | $this->dqlBuilder->setSeparator($separator);  | 
            ||
| 406 | }  | 
            ||
| 407 | |||
| 408 | public function truncateTable(string $table): string  | 
            ||
| 409 |     { | 
            ||
| 410 | return $this->ddlBuilder->truncateTable($table);  | 
            ||
| 411 | }  | 
            ||
| 412 | |||
| 413 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string  | 
            ||
| 414 |     { | 
            ||
| 415 | return $this->dmlBuilder->update($table, $columns, $condition, $params);  | 
            ||
| 416 | }  | 
            ||
| 417 | |||
| 418 | public function upsert(  | 
            ||
| 425 | }  | 
            ||
| 426 | }  | 
            ||
| 427 |