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 |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Return names of table columns |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 7 | public static function getColumns() : array |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Filter columns for insert/update queries by table columns definition |
||
| 204 | * |
||
| 205 | * @param array $data |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | 6 | public static function filterColumns($data) : array |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Fetching rows by SQL query |
||
| 216 | * |
||
| 217 | * @param string $sql SQL query with placeholders |
||
| 218 | * @param array $params Params for query placeholders |
||
| 219 | * |
||
| 220 | * @return RowInterface[] of rows results in FETCH_CLASS mode |
||
| 221 | */ |
||
| 222 | 10 | protected static function fetch($sql, $params = []) : array |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | * |
||
| 231 | * @throws DbException |
||
| 232 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
| 233 | * @throws \InvalidArgumentException |
||
| 234 | */ |
||
| 235 | 10 | public static function find(...$keys) : array |
|
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | * |
||
| 264 | * @throws \InvalidArgumentException |
||
| 265 | * @throws Exception\DbException |
||
| 266 | */ |
||
| 267 | 10 | public static function findWhere(...$where) : array |
|
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | * |
||
| 318 | * @throws DbException |
||
| 319 | * @throws \InvalidArgumentException |
||
| 320 | * @throws InvalidPrimaryKeyException |
||
| 321 | */ |
||
| 322 | 8 | public static function findRow($primaryKey) : ?RowInterface |
|
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | * |
||
| 331 | * @throws DbException |
||
| 332 | * @throws \InvalidArgumentException |
||
| 333 | */ |
||
| 334 | 2 | public static function findRowWhere(array $whereList) : ?RowInterface |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Prepare array for WHERE or SET statements |
||
| 342 | * |
||
| 343 | * @param array $where |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 347 | */ |
||
| 348 | 6 | private static function prepareStatement(array $where) : array |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Prepare Db\Query\Select for current table: |
||
| 359 | * - predefine "select" section as "*" from current table |
||
| 360 | * - predefine "from" section as current table name and first letter as alias |
||
| 361 | * - predefine fetch type |
||
| 362 | * |
||
| 363 | * <code> |
||
| 364 | * // use default select "*" |
||
| 365 | * $select = Users\Table::select(); |
||
| 366 | * $arrUsers = $select->where('u.id = ?', $id) |
||
| 367 | * ->execute(); |
||
| 368 | * |
||
| 369 | * // setup custom select "u.id, u.login" |
||
| 370 | * $select = Users\Table::select(); |
||
| 371 | * $arrUsers = $select->select('u.id, u.login') |
||
| 372 | * ->where('u.id = ?', $id) |
||
| 373 | * ->execute(); |
||
| 374 | * </code> |
||
| 375 | * |
||
| 376 | * @return Query\Select |
||
| 377 | */ |
||
| 378 | 2 | public static function select() : Query\Select |
|
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | 2 | public static function create(array $data = []) : RowInterface |
|
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritdoc} |
||
| 404 | * |
||
| 405 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 406 | * @throws Exception\DbException |
||
| 407 | */ |
||
| 408 | 2 | public static function insert(array $data) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | * |
||
| 443 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 444 | * @throws Exception\DbException |
||
| 445 | */ |
||
| 446 | 2 | public static function update(array $data, array $where) : int |
|
| 474 | |||
| 475 | /** |
||
| 476 | * {@inheritdoc} |
||
| 477 | * |
||
| 478 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 479 | * @throws \Bluz\Db\Exception\DbException |
||
| 480 | */ |
||
| 481 | 2 | public static function delete(array $where) : int |
|
| 506 | } |
||
| 507 |