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 |
||
| 41 | abstract class Table |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string the table name |
||
| 45 | */ |
||
| 46 | protected $name; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string the model name |
||
| 50 | */ |
||
| 51 | protected $model; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array table meta |
||
| 55 | */ |
||
| 56 | protected $meta = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string default SQL query for select |
||
| 60 | */ |
||
| 61 | protected $select = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array the primary key column or columns (only as array). |
||
| 65 | */ |
||
| 66 | protected $primary; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string the sequence name, required for PostgreSQL |
||
| 70 | */ |
||
| 71 | protected $sequence; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string row class name |
||
| 75 | */ |
||
| 76 | protected $rowClass; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create and initialize Table instance |
||
| 80 | */ |
||
| 81 | 2 | private function __construct() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Initialization hook. |
||
| 115 | * Subclasses may override this method |
||
| 116 | */ |
||
| 117 | 2 | public function init() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get Table instance |
||
| 123 | * |
||
| 124 | * @return static |
||
| 125 | */ |
||
| 126 | 47 | public static function getInstance() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Set select query |
||
| 138 | * |
||
| 139 | * @param string $select SQL query |
||
| 140 | * |
||
| 141 | * @return Table |
||
| 142 | */ |
||
| 143 | public function setSelectQuery($select) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get select query |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getSelectQuery() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get primary key(s) |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | * @throws InvalidPrimaryKeyException if primary key was not set or has wrong format |
||
| 164 | */ |
||
| 165 | 39 | public function getPrimaryKey() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get table name |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 3 | public function getName() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get model name |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 2 | public function getModel() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Return information about table columns |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 5 | public static function getMeta() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Return names of table columns |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | 4 | public static function getColumns() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Filter columns for insert/update queries by table columns definition |
||
| 235 | * |
||
| 236 | * @param array $data |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | 3 | public static function filterColumns($data) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Fetching rows by SQL query |
||
| 247 | * |
||
| 248 | * @param string $sql SQL query with placeholders |
||
| 249 | * @param array $params Params for query placeholders |
||
| 250 | * |
||
| 251 | * @return array of rows results in FETCH_CLASS mode |
||
| 252 | */ |
||
| 253 | 8 | public static function fetch($sql, $params = []) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Fetch all rows from table |
||
| 261 | * Be carefully with this method, can be very slow |
||
| 262 | * |
||
| 263 | * @return array of rows results in FETCH_CLASS mode |
||
| 264 | */ |
||
| 265 | public static function fetchAll() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Fetches rows by primary key. The argument specifies one or more primary |
||
| 273 | * key value(s). To find multiple rows by primary key, the argument must |
||
| 274 | * be an array. |
||
| 275 | * |
||
| 276 | * This method accepts a variable number of arguments. If the table has a |
||
| 277 | * multi-column primary key, the number of arguments must be the same as |
||
| 278 | * the number of columns in the primary key. To find multiple rows in a |
||
| 279 | * table with a multi-column primary key, each argument must be an array |
||
| 280 | * with the same number of elements. |
||
| 281 | * |
||
| 282 | * The find() method always returns a array |
||
| 283 | * |
||
| 284 | * Row by primary key, return array |
||
| 285 | * Table::find(123); |
||
| 286 | * |
||
| 287 | * Row by compound primary key, return array |
||
| 288 | * Table::find([123, 'abc']); |
||
| 289 | * |
||
| 290 | * Multiple rows by primary key |
||
| 291 | * Table::find(123, 234, 345); |
||
| 292 | * |
||
| 293 | * Multiple rows by compound primary key |
||
| 294 | * Table::find([123, 'abc'], [234, 'def'], [345, 'ghi']) |
||
| 295 | * |
||
| 296 | * @param mixed ...$keys The value(s) of the primary keys. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
| 300 | */ |
||
| 301 | 10 | public static function find(...$keys) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Find row by primary key |
||
| 336 | * |
||
| 337 | * @param mixed $primaryKey |
||
| 338 | * |
||
| 339 | * @return Row |
||
| 340 | */ |
||
| 341 | 8 | public static function findRow($primaryKey) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Find rows by WHERE |
||
| 352 | * // WHERE alias = 'foo' |
||
| 353 | * Table::findWhere(['alias'=>'foo']); |
||
| 354 | * // WHERE alias = 'foo' OR 'alias' = 'bar' |
||
| 355 | * Table::findWhere(['alias'=>'foo'], ['alias'=>'bar']); |
||
| 356 | * // WHERE (alias = 'foo' AND userId = 2) OR ('alias' = 'bar' AND userId = 4) |
||
| 357 | * Table::findWhere(['alias'=>'foo', 'userId'=> 2], ['alias'=>'foo', 'userId'=>4]); |
||
| 358 | * // WHERE alias IN ('foo', 'bar') |
||
| 359 | * Table::findWhere(['alias'=> ['foo', 'bar']]); |
||
| 360 | * |
||
| 361 | * @param mixed ...$where |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | * @throws \InvalidArgumentException |
||
| 365 | * @throws Exception\DbException |
||
| 366 | */ |
||
| 367 | 8 | public static function findWhere(...$where) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Find row by where condition |
||
| 418 | * |
||
| 419 | * @param array $whereList |
||
| 420 | * |
||
| 421 | * @return Row |
||
| 422 | */ |
||
| 423 | public static function findRowWhere($whereList) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Prepare array for WHERE or SET statements |
||
| 431 | * |
||
| 432 | * @param array $where |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 436 | */ |
||
| 437 | 3 | private static function prepareStatement($where) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Prepare Db\Query\Select for current table: |
||
| 448 | * - predefine "select" section as "*" from current table |
||
| 449 | * - predefine "from" section as current table name and first letter as alias |
||
| 450 | * - predefine fetch type |
||
| 451 | * |
||
| 452 | * <code> |
||
| 453 | * // use default select "*" |
||
| 454 | * $select = Users\Table::select(); |
||
| 455 | * $arrUsers = $select->where('u.id = ?', $id) |
||
| 456 | * ->execute(); |
||
| 457 | * |
||
| 458 | * // setup custom select "u.id, u.login" |
||
| 459 | * $select = Users\Table::select(); |
||
| 460 | * $arrUsers = $select->select('u.id, u.login') |
||
| 461 | * ->where('u.id = ?', $id) |
||
| 462 | * ->execute(); |
||
| 463 | * </code> |
||
| 464 | * |
||
| 465 | * @return Query\Select |
||
| 466 | */ |
||
| 467 | 2 | public static function select() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Create Row instance |
||
| 481 | * |
||
| 482 | * @param array $data |
||
| 483 | * |
||
| 484 | * @return Row |
||
| 485 | */ |
||
| 486 | 2 | public static function create(array $data = []) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Insert new record to table and return last insert Id |
||
| 497 | * |
||
| 498 | * <code> |
||
| 499 | * Table::insert(['login' => 'Man', 'email' => '[email protected]']) |
||
| 500 | * </code> |
||
| 501 | * |
||
| 502 | * @param array $data Column-value pairs |
||
| 503 | * |
||
| 504 | * @return string|null Primary key or null |
||
| 505 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 506 | * @throws Exception\DbException |
||
| 507 | */ |
||
| 508 | 1 | public static function insert(array $data) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Updates existing rows |
||
| 542 | * |
||
| 543 | * <code> |
||
| 544 | * Table::insert(['login' => 'Man', 'email' => '[email protected]'], ['id' => 42]) |
||
| 545 | * </code> |
||
| 546 | * |
||
| 547 | * @param array $data Column-value pairs. |
||
| 548 | * @param array $where An array of SQL WHERE clause(s) |
||
| 549 | * |
||
| 550 | * @return integer The number of rows updated |
||
| 551 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 552 | * @throws Exception\DbException |
||
| 553 | */ |
||
| 554 | 1 | public static function update(array $data, array $where) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Deletes existing rows |
||
| 586 | * |
||
| 587 | * <code> |
||
| 588 | * Table::delete(['login' => 'Man']) |
||
| 589 | * </code> |
||
| 590 | * |
||
| 591 | * @param array $where An array of SQL WHERE clause(s) |
||
| 592 | * |
||
| 593 | * @return integer The number of rows deleted |
||
| 594 | * @throws Exception\DbException |
||
| 595 | */ |
||
| 596 | 1 | public static function delete(array $where) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Setup relation "one to one" or "one to many" |
||
| 624 | * |
||
| 625 | * @param string $key |
||
| 626 | * @param string $model |
||
| 627 | * @param string $foreign |
||
| 628 | * |
||
| 629 | * @return void |
||
| 630 | */ |
||
| 631 | public function linkTo($key, $model, $foreign) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Setup relation "many to many" |
||
| 638 | * [table1-key] [table1_key-table2-table3_key] [table3-key] |
||
| 639 | * |
||
| 640 | * @param string $model |
||
| 641 | * @param string $link |
||
| 642 | * |
||
| 643 | * @return void |
||
| 644 | */ |
||
| 645 | public function linkToMany($model, $link) |
||
| 649 | } |
||
| 650 |