Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | abstract class Table implements TableInterface |
||
| 44 | { |
||
| 45 | use Instance; |
||
| 46 | use TableRelations; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string the table name |
||
| 50 | */ |
||
| 51 | protected $name; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string the model name |
||
| 55 | */ |
||
| 56 | protected $model; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array table meta |
||
| 60 | */ |
||
| 61 | protected $meta = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string default SQL query for select |
||
| 65 | */ |
||
| 66 | protected $select = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array the primary key column or columns (only as array). |
||
| 70 | */ |
||
| 71 | protected $primary; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string the sequence name, required for PostgreSQL |
||
| 75 | */ |
||
| 76 | protected $sequence; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string row class name |
||
| 80 | */ |
||
| 81 | protected $rowClass; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create and initialize Table instance |
||
| 85 | */ |
||
| 86 | 2 | private function __construct() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Initialization hook. |
||
| 120 | * Subclasses may override this method |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 2 | public function init(): void |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get primary key(s) |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | * @throws InvalidPrimaryKeyException if primary key was not set or has wrong format |
||
| 133 | */ |
||
| 134 | 39 | public function getPrimaryKey(): array |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Get table name |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | 3 | public function getName(): string |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Get model name |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 2 | public function getModel(): string |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Return information about table columns |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 8 | public static function getMeta(): array |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Return names of table columns |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | 7 | public static function getColumns(): array |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Filter columns for insert/update queries by table columns definition |
||
| 208 | * |
||
| 209 | * @param array $data |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | 6 | public static function filterColumns($data): array |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Fetching rows by SQL query |
||
| 220 | * |
||
| 221 | * @param string $sql SQL query with placeholders |
||
| 222 | * @param array $params Params for query placeholders |
||
| 223 | * |
||
| 224 | * @return RowInterface[] of rows results in FETCH_CLASS mode |
||
| 225 | */ |
||
| 226 | 10 | protected static function fetch($sql, $params = []): array |
|
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | * |
||
| 235 | * @throws DbException |
||
| 236 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
| 237 | * @throws \InvalidArgumentException |
||
| 238 | */ |
||
| 239 | 10 | public static function find(...$keys): array |
|
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | * |
||
| 268 | * @throws \InvalidArgumentException |
||
| 269 | * @throws Exception\DbException |
||
| 270 | */ |
||
| 271 | 10 | public static function findWhere(...$where): array |
|
| 272 | { |
||
| 273 | 10 | $self = static::getInstance(); |
|
| 274 | |||
| 275 | 10 | $whereParams = []; |
|
| 276 | |||
| 277 | 10 | if (\count($where) === 2 && \is_string($where[0])) { |
|
| 278 | $whereClause = $where[0]; |
||
| 279 | $whereParams = (array)$where[1]; |
||
| 280 | 10 | } elseif (\count($where)) { |
|
| 281 | 10 | $whereOrTerms = []; |
|
| 282 | 10 | foreach ($where as $keyValueSets) { |
|
| 283 | 10 | $whereAndTerms = []; |
|
| 284 | 10 | foreach ($keyValueSets as $keyName => $keyValue) { |
|
| 285 | 10 | if (\is_array($keyValue)) { |
|
| 286 | $keyValue = array_map( |
||
| 287 | function ($value) { |
||
| 288 | return DbProxy::quote($value); |
||
| 289 | }, |
||
| 290 | $keyValue |
||
| 291 | ); |
||
| 292 | $keyValue = implode(',', $keyValue); |
||
| 293 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IN (' . $keyValue . ')'; |
||
| 294 | 10 | } elseif (null === $keyValue) { |
|
| 295 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IS NULL'; |
||
| 296 | 10 | } elseif ('%' === $keyValue{0} || '%' === $keyValue{-1}) { |
|
| 297 | $whereAndTerms[] = $self->name . '.' . $keyName . ' LIKE ?'; |
||
| 298 | $whereParams[] = $keyValue; |
||
| 299 | } else { |
||
| 300 | 10 | $whereAndTerms[] = $self->name . '.' . $keyName . ' = ?'; |
|
| 301 | 10 | $whereParams[] = $keyValue; |
|
| 302 | } |
||
| 303 | 10 | if (!is_scalar($keyValue) && !is_null($keyValue)) { |
|
| 304 | throw new \InvalidArgumentException( |
||
| 305 | "Wrong arguments of method 'findWhere'.\n" . |
||
| 306 | 10 | "Please use syntax described at https://github.com/bluzphp/framework/wiki/Db-Table" |
|
| 307 | ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | 10 | $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')'; |
|
| 311 | } |
||
| 312 | 10 | $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')'; |
|
| 313 | } else { |
||
| 314 | throw new DbException( |
||
| 315 | "Method `Table::findWhere()` can't return all records from table" |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | |||
| 319 | 10 | return self::fetch($self->select . ' WHERE ' . $whereClause, $whereParams); |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | * |
||
| 325 | * @throws DbException |
||
| 326 | * @throws \InvalidArgumentException |
||
| 327 | * @throws InvalidPrimaryKeyException |
||
| 328 | */ |
||
| 329 | 8 | public static function findRow($primaryKey): ?RowInterface |
|
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritdoc} |
||
| 337 | * |
||
| 338 | * @throws DbException |
||
| 339 | * @throws \InvalidArgumentException |
||
| 340 | */ |
||
| 341 | 2 | public static function findRowWhere(array $whereList): ?RowInterface |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Prepare array for WHERE or SET statements |
||
| 349 | * |
||
| 350 | * @param array $where |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | 6 | private static function prepareStatement(array $where): array |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Prepare Db\Query\Select for current table: |
||
| 365 | * - predefine "select" section as "*" from current table |
||
| 366 | * - predefine "from" section as current table name and first letter as alias |
||
| 367 | * - predefine fetch type |
||
| 368 | * |
||
| 369 | * <code> |
||
| 370 | * // use default select "*" |
||
| 371 | * $select = Users\Table::select(); |
||
| 372 | * $arrUsers = $select->where('u.id = ?', $id) |
||
| 373 | * ->execute(); |
||
| 374 | * |
||
| 375 | * // setup custom select "u.id, u.login" |
||
| 376 | * $select = Users\Table::select(); |
||
| 377 | * $arrUsers = $select->select('u.id, u.login') |
||
| 378 | * ->where('u.id = ?', $id) |
||
| 379 | * ->execute(); |
||
| 380 | * </code> |
||
| 381 | * |
||
| 382 | * @return Query\Select |
||
| 383 | */ |
||
| 384 | 2 | public static function select(): Query\Select |
|
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritdoc} |
||
| 398 | */ |
||
| 399 | 2 | public static function create(array $data = []): RowInterface |
|
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | * |
||
| 411 | * @throws Exception\DbException |
||
| 412 | */ |
||
| 413 | 2 | public static function insert(array $data) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | * |
||
| 448 | * @throws Exception\DbException |
||
| 449 | */ |
||
| 450 | 2 | public static function update(array $data, array $where): int |
|
| 478 | |||
| 479 | /** |
||
| 480 | * {@inheritdoc} |
||
| 481 | * |
||
| 482 | * @throws \Bluz\Db\Exception\DbException |
||
| 483 | */ |
||
| 484 | 2 | public static function delete(array $where): int |
|
| 509 | } |
||
| 510 |