Complex classes like SQLBuilder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SQLBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 13 | class SQLBuilder |
||
| 14 | { |
||
| 15 | const NUMERIC_COLUMNS_TYPES = ['int', 'float', 'longint', 'smallint', 'tinyint']; |
||
| 16 | const DATE_COLUMNS_TYPES = ['date', 'datetime', 'timestamp']; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Build update statement. |
||
| 20 | * |
||
| 21 | * @param TableMetadata $tableMetadata Table metadata |
||
| 22 | * @param array $columnValues Collection of columnName => columnValue |
||
| 23 | * @param Condition|null $condition Update filtering condition |
||
| 24 | * |
||
| 25 | * @return string Update SQL statement |
||
| 26 | * @throws \InvalidArgumentException |
||
| 27 | */ |
||
| 28 | public function buildUpdateStatement(TableMetadata $tableMetadata, array $columnValues, Condition $condition = null) : string |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Build full table column name. |
||
| 47 | * |
||
| 48 | * @param string $tableName Table name |
||
| 49 | * @param string $columnName Field name |
||
| 50 | * |
||
| 51 | * @return string Full table column name |
||
| 52 | */ |
||
| 53 | protected function buildFullColumnName(string $tableName, string $columnName) : string |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Build argument value statement. |
||
| 60 | * |
||
| 61 | * @param string|array $value Argument column value |
||
| 62 | * @param string $columnType Argument column type |
||
| 63 | * @param string $relation Argument relation |
||
| 64 | * @param bool $nullable |
||
| 65 | * @param string $defaultValue |
||
| 66 | * |
||
| 67 | * @return string Argument relation with value statement |
||
| 68 | */ |
||
| 69 | protected function buildArgumentValue( |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Build array argument value statement. |
||
| 84 | * |
||
| 85 | * @param string $columnType Table column type |
||
| 86 | * @param array $value Table column array value |
||
| 87 | * @param string $relation Table column relation to value |
||
| 88 | * |
||
| 89 | * @return string Array argument relation with value statement |
||
| 90 | */ |
||
| 91 | protected function buildArrayValue(string $columnType, array $value, string $relation = 'IN') : string |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Define if table column type is numeric. |
||
| 102 | * |
||
| 103 | * @param string $columnType Table column type |
||
| 104 | * |
||
| 105 | * @return bool True if column type is numeric |
||
| 106 | */ |
||
| 107 | protected function isColumnNumeric(string $columnType) : bool |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Build array with numeric values statement. |
||
| 114 | * |
||
| 115 | * @param array $value Array with numeric values |
||
| 116 | * @param string $relation Table column relation to value |
||
| 117 | * |
||
| 118 | * @return string Array with numeric values statement |
||
| 119 | */ |
||
| 120 | protected function buildNumericArrayValue(array $value, string $relation) : string |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Build array string value statement. |
||
| 127 | * |
||
| 128 | * @param array $value Array with string values |
||
| 129 | * @param string $relation Table column relation to value |
||
| 130 | * |
||
| 131 | * @return string Array with string values statement |
||
| 132 | */ |
||
| 133 | protected function buildStringArrayValue(array $value, string $relation) : string |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Build not array argument value statement. |
||
| 140 | * |
||
| 141 | * @param string $columnType Table column type |
||
| 142 | * @param mixed $value Table column value |
||
| 143 | * @param string $relation Table column relation to value |
||
| 144 | * |
||
| 145 | * @return string Not array argument relation with value statement |
||
| 146 | */ |
||
| 147 | protected function buildValue( |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Build not array numeric value statement. |
||
| 177 | * |
||
| 178 | * @param mixed $value Numeric value |
||
| 179 | * |
||
| 180 | * @return string Not array numeric value statement |
||
| 181 | */ |
||
| 182 | protected function buildNumericValue($value) : string |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Define if table column type is date. |
||
| 189 | * |
||
| 190 | * @param string $columnType Table column type |
||
| 191 | * |
||
| 192 | * @return bool True if column type is date |
||
| 193 | */ |
||
| 194 | protected function isColumnDate(string $columnType) : bool |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Build not array date value statement. |
||
| 201 | * |
||
| 202 | * @param mixed $value Date value |
||
| 203 | * |
||
| 204 | * @return string Not array date value statement |
||
| 205 | */ |
||
| 206 | protected function buildDateValue($value) : string |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Build not array string value statement. |
||
| 213 | * |
||
| 214 | * @param string $value String value |
||
| 215 | * |
||
| 216 | * @return string Not array string value statement |
||
| 217 | */ |
||
| 218 | protected function buildStringValue(string $value) : string |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Build not array NULL value statement. |
||
| 225 | * |
||
| 226 | * @return string Not array string value statement |
||
| 227 | */ |
||
| 228 | protected function buildNullValue() : string |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Build where statement. |
||
| 235 | * |
||
| 236 | * @param TableMetadata $metadata |
||
| 237 | * @param Condition $condition |
||
| 238 | * |
||
| 239 | * @return string Limitation statement |
||
| 240 | * @throws \InvalidArgumentException |
||
| 241 | * |
||
| 242 | */ |
||
| 243 | public function buildWhereStatement(TableMetadata $metadata, Condition $condition) : string |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Build argument condition. |
||
| 260 | * |
||
| 261 | * @param Argument $argument Condition argument |
||
| 262 | * @param TableMetadata $metadata Table metadata |
||
| 263 | * |
||
| 264 | * @return string Argument condition statement |
||
| 265 | * @throws \InvalidArgumentException If argument column does not exist |
||
| 266 | */ |
||
| 267 | protected function buildArgumentCondition(Argument $argument, TableMetadata $metadata) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Build own condition statement. |
||
| 291 | * |
||
| 292 | * @param string $ownCondition Condition statement |
||
| 293 | * |
||
| 294 | * @return string Own condition statement |
||
| 295 | */ |
||
| 296 | protected function buildOwnCondition(string $ownCondition) : string |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Build generic condition statement. |
||
| 303 | * |
||
| 304 | * @param string $columnName Table column name |
||
| 305 | * @param string $relation Table column value relation |
||
| 306 | * @param string $value Table column value |
||
| 307 | * |
||
| 308 | * @return string Generic condition statement |
||
| 309 | */ |
||
| 310 | protected function buildCondition(string $columnName, string $relation = '', string $value = '') : string |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build is null/not null condition statement. |
||
| 317 | * |
||
| 318 | * @param string $columnName Table column name |
||
| 319 | * @param string $nullRelation Table column null relation |
||
| 320 | * |
||
| 321 | * @return string Is null/not null condition statement |
||
| 322 | */ |
||
| 323 | protected function buildNullCondition(string $columnName, string $nullRelation) : string |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Build insert statement. |
||
| 330 | * |
||
| 331 | * @param TableMetadata $tableMetadata Table metadata |
||
| 332 | * @param array $columnValues Collection of columnName => columnValue |
||
| 333 | * |
||
| 334 | * @return string Insert SQL statement |
||
| 335 | * @throws \InvalidArgumentException |
||
| 336 | */ |
||
| 337 | public function buildInsertStatement(TableMetadata $tableMetadata, array $columnValues) : string |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Build selected fields SELECT statement part. |
||
| 360 | * |
||
| 361 | * @param array $tableColumns Tables and column names collection |
||
| 362 | * |
||
| 363 | * @return string SELECT statement |
||
| 364 | */ |
||
| 365 | public function buildSelectStatement(array $tableColumns) : string |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Build full table column names collection. |
||
| 372 | * |
||
| 373 | * @param array $tableColumns Tables and column names collection |
||
| 374 | * |
||
| 375 | * @return array Collection of full column names for query |
||
| 376 | */ |
||
| 377 | protected function buildFullColumnNames(array $tableColumns) : array |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Build FROM statement part. |
||
| 392 | * |
||
| 393 | * @param array $tableNames Tables and column names collection |
||
| 394 | * |
||
| 395 | * @return string FROM statement |
||
| 396 | */ |
||
| 397 | public function buildFromStatement(array $tableNames = []) : string |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Build grouping statement. |
||
| 404 | * |
||
| 405 | * @param array $tableColumns Tables and column names collection |
||
| 406 | * |
||
| 407 | * @return string Grouping statement |
||
| 408 | */ |
||
| 409 | public function buildGroupStatement(array $tableColumns) : string |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Build ordering statement. |
||
| 416 | * |
||
| 417 | * @param array $tableColumns Tables and column names collection |
||
| 418 | * @param array $orders Collection of columns sorting order |
||
| 419 | * |
||
| 420 | * @return string Ordering statement |
||
| 421 | * @throws \InvalidArgumentException |
||
| 422 | */ |
||
| 423 | public function buildOrderStatement(array $tableColumns, array $orders) : string |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Build limitation statement. |
||
| 436 | * |
||
| 437 | * @param int $rows Rows amount for limitation |
||
| 438 | * @param int $offset Rows offset |
||
| 439 | * |
||
| 440 | * @return string Limitation statement |
||
| 441 | */ |
||
| 442 | public function buildLimitStatement(int $rows, int $offset = 0) : string |
||
| 446 | } |
||
| 447 |