| Total Complexity | 66 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class QueryBuilder 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 DDLQueryBuilder $ddlBuilder, |
||
| 69 | private DMLQueryBuilder $dmlBuilder, |
||
| 70 | private DQLQueryBuilder $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 | /** @psalm-var mixed */ |
||
| 135 | $params[$phName] = $value; |
||
| 136 | |||
| 137 | return $phName; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function build(QueryInterface $query, array $params = []): array |
||
| 141 | { |
||
| 142 | return $this->dqlBuilder->build($query, $params); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function buildColumns(array|string $columns): string |
||
| 146 | { |
||
| 147 | return $this->dqlBuilder->buildColumns($columns); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function buildCondition(array|string|ExpressionInterface|null $condition, array &$params = []): string |
||
| 151 | { |
||
| 152 | return $this->dqlBuilder->buildCondition($condition, $params); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function buildExpression(ExpressionInterface $expression, array &$params = []): string |
||
| 156 | { |
||
| 157 | return $this->dqlBuilder->buildExpression($expression, $params); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function buildFrom(?array $tables, array &$params): string |
||
| 161 | { |
||
| 162 | return $this->dqlBuilder->buildFrom($tables, $params); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function buildGroupBy(array $columns, array &$params = []): string |
||
| 166 | { |
||
| 167 | return $this->dqlBuilder->buildGroupBy($columns, $params); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function buildHaving(array|ExpressionInterface|string|null $condition, array &$params = []): string |
||
| 171 | { |
||
| 172 | return $this->dqlBuilder->buildHaving($condition, $params); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function buildJoin(array $joins, array &$params): string |
||
| 176 | { |
||
| 177 | return $this->dqlBuilder->buildJoin($joins, $params); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function buildLimit(Expression|int|null $limit, Expression|int|null $offset): string |
||
| 181 | { |
||
| 182 | return $this->dqlBuilder->buildLimit($limit, $offset); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function buildOrderBy(array $columns, array &$params = []): string |
||
| 186 | { |
||
| 187 | return $this->dqlBuilder->buildOrderBy($columns, $params); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function buildOrderByAndLimit( |
||
| 191 | string $sql, |
||
| 192 | array $orderBy, |
||
| 193 | Expression|int|null $limit, |
||
| 194 | Expression|int|null $offset, |
||
| 195 | array &$params = [] |
||
| 196 | ): string { |
||
| 197 | return $this->dqlBuilder->buildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function buildSelect( |
||
| 201 | array $columns, |
||
| 202 | array &$params, |
||
| 203 | ?bool $distinct = false, |
||
| 204 | string $selectOption = null |
||
| 205 | ): string { |
||
| 206 | return $this->dqlBuilder->buildSelect($columns, $params, $distinct, $selectOption); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function buildUnion(array $unions, array &$params): string |
||
| 210 | { |
||
| 211 | return $this->dqlBuilder->buildUnion($unions, $params); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function buildWhere( |
||
| 215 | array|string|ConditionInterface|ExpressionInterface|null $condition, |
||
| 216 | array &$params = [] |
||
| 217 | ): string { |
||
| 218 | return $this->dqlBuilder->buildWhere($condition, $params); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function buildWithQueries(array $withs, array &$params): string |
||
| 222 | { |
||
| 223 | return $this->dqlBuilder->buildWithQueries($withs, $params); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
||
| 227 | { |
||
| 228 | return $this->ddlBuilder->checkIntegrity($schema, $table, $check); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function createConditionFromArray(array $condition): ConditionInterface |
||
| 232 | { |
||
| 233 | return $this->dqlBuilder->createConditionFromArray($condition); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function createIndex(string $name, string $table, array|string $columns, ?string $indexType = null, ?string $indexMethod = null): string |
||
| 237 | { |
||
| 238 | return $this->ddlBuilder->createIndex($name, $table, $columns, $indexType, $indexMethod); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function createTable(string $table, array $columns, ?string $options = null): string |
||
| 242 | { |
||
| 243 | return $this->ddlBuilder->createTable($table, $columns, $options); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function createView(string $viewName, QueryInterface|string $subQuery): string |
||
| 247 | { |
||
| 248 | return $this->ddlBuilder->createView($viewName, $subQuery); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function delete(string $table, array|string $condition, array &$params): string |
||
| 252 | { |
||
| 253 | return $this->dmlBuilder->delete($table, $condition, $params); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function dropCheck(string $name, string $table): string |
||
| 257 | { |
||
| 258 | return $this->ddlBuilder->dropCheck($name, $table); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function dropColumn(string $table, string $column): string |
||
| 262 | { |
||
| 263 | return $this->ddlBuilder->dropColumn($table, $column); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function dropCommentFromColumn(string $table, string $column): string |
||
| 267 | { |
||
| 268 | return $this->ddlBuilder->dropCommentFromColumn($table, $column); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function dropCommentFromTable(string $table): string |
||
| 272 | { |
||
| 273 | return $this->ddlBuilder->dropCommentFromTable($table); |
||
| 274 | } |
||
| 275 | |||
| 276 | public function dropDefaultValue(string $name, string $table): string |
||
| 277 | { |
||
| 278 | return $this->ddlBuilder->dropDefaultValue($name, $table); |
||
| 279 | } |
||
| 280 | |||
| 281 | public function dropForeignKey(string $name, string $table): string |
||
| 282 | { |
||
| 283 | return $this->ddlBuilder->dropForeignKey($name, $table); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function dropIndex(string $name, string $table): string |
||
| 287 | { |
||
| 288 | return $this->ddlBuilder->dropIndex($name, $table); |
||
| 289 | } |
||
| 290 | |||
| 291 | public function dropPrimaryKey(string $name, string $table): string |
||
| 292 | { |
||
| 293 | return $this->ddlBuilder->dropPrimaryKey($name, $table); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function dropTable(string $table): string |
||
| 297 | { |
||
| 298 | return $this->ddlBuilder->dropTable($table); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function dropUnique(string $name, string $table): string |
||
| 304 | } |
||
| 305 | |||
| 306 | public function dropView(string $viewName): string |
||
| 307 | { |
||
| 308 | return $this->ddlBuilder->dropView($viewName); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function getColumnType(ColumnSchemaBuilder|string $type): string |
||
| 312 | { |
||
| 313 | if ($type instanceof ColumnSchemaBuilder) { |
||
| 314 | $type = $type->__toString(); |
||
| 315 | } |
||
| 316 | |||
| 317 | if (isset($this->typeMap[$type])) { |
||
| 318 | return $this->typeMap[$type]; |
||
| 319 | } |
||
| 320 | |||
| 321 | if (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) { |
||
| 322 | if (isset($this->typeMap[$matches[1]])) { |
||
| 323 | return preg_replace( |
||
| 324 | '/\(.+\)/', |
||
| 325 | '(' . $matches[2] . ')', |
||
| 326 | $this->typeMap[$matches[1]] |
||
| 327 | ) . $matches[3]; |
||
| 328 | } |
||
| 329 | } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) { |
||
| 330 | if (isset($this->typeMap[$matches[1]])) { |
||
| 331 | return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | return $type; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function getExpressionBuilder(ExpressionInterface $expression): object |
||
| 339 | { |
||
| 340 | return $this->dqlBuilder->getExpressionBuilder($expression); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
||
| 344 | { |
||
| 345 | return $this->dmlBuilder->insert($table, $columns, $params); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
||
| 350 | */ |
||
| 351 | public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string |
||
| 352 | { |
||
| 353 | return $this->dmlBuilder->insertEx($table, $columns, $params); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function quoter(): QuoterInterface |
||
| 357 | { |
||
| 358 | return $this->quoter; |
||
| 359 | } |
||
| 360 | |||
| 361 | public function renameColumn(string $table, string $oldName, string $newName): string |
||
| 364 | } |
||
| 365 | |||
| 366 | public function renameTable(string $oldName, string $newName): string |
||
| 367 | { |
||
| 368 | return $this->ddlBuilder->renameTable($oldName, $newName); |
||
| 369 | } |
||
| 370 | |||
| 371 | public function resetSequence(string $tableName, array|int|string|null $value = null): string |
||
| 372 | { |
||
| 373 | return $this->dmlBuilder->resetSequence($tableName, $value); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function schema(): SchemaInterface |
||
| 377 | { |
||
| 378 | return $this->schema; |
||
| 379 | } |
||
| 380 | |||
| 381 | public function selectExists(string $rawSql): string |
||
| 382 | { |
||
| 383 | return $this->dqlBuilder->selectExists($rawSql); |
||
| 384 | } |
||
| 385 | |||
| 386 | public function setConditionClasses(array $classes): void |
||
| 387 | { |
||
| 388 | $this->dqlBuilder->setConditionClasses($classes); |
||
| 389 | } |
||
| 390 | |||
| 391 | public function setExpressionBuilders(array $builders): void |
||
| 392 | { |
||
| 393 | $this->dqlBuilder->setExpressionBuilders($builders); |
||
| 394 | } |
||
| 395 | |||
| 396 | public function setSeparator(string $separator): void |
||
| 397 | { |
||
| 398 | $this->dqlBuilder->setSeparator($separator); |
||
| 399 | } |
||
| 400 | |||
| 401 | public function truncateTable(string $table): string |
||
| 402 | { |
||
| 403 | return $this->dmlBuilder->truncateTable($table); |
||
| 404 | } |
||
| 405 | |||
| 406 | public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
||
| 409 | } |
||
| 410 | |||
| 411 | public function upsert( |
||
| 412 | string $table, |
||
| 413 | QueryInterface|array $insertColumns, |
||
| 414 | bool|array $updateColumns, |
||
| 415 | array &$params = [] |
||
| 416 | ): string { |
||
| 417 | return $this->dmlBuilder->upsert($table, $insertColumns, $updateColumns, $params); |
||
| 418 | } |
||
| 419 | } |
||
| 420 |