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 | 2 | protected $rowClass; |
|
| 82 | |||
| 83 | 2 | /** |
|
| 84 | 2 | * Create and initialize Table instance |
|
| 85 | */ |
||
| 86 | private function __construct() |
||
| 117 | 2 | ||
| 118 | /** |
||
| 119 | 2 | * Initialization hook. |
|
| 120 | * Subclasses may override this method |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function init() : void |
||
| 127 | |||
| 128 | 47 | /** |
|
| 129 | 47 | * Get primary key(s) |
|
| 130 | 3 | * |
|
| 131 | * @return array |
||
| 132 | * @throws InvalidPrimaryKeyException if primary key was not set or has wrong format |
||
| 133 | 47 | */ |
|
| 134 | public function getPrimaryKey() : array |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get table name |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getName() : string |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get model name |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getModel() : string |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return information about table columns |
||
| 164 | * |
||
| 165 | 39 | * @return array |
|
| 166 | */ |
||
| 167 | 39 | public static function getMeta() : array |
|
| 190 | 2 | ||
| 191 | /** |
||
| 192 | * Return names of table columns |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public static function getColumns() : array |
||
| 201 | 5 | ||
| 202 | 1 | /** |
|
| 203 | 1 | * Filter columns for insert/update queries by table columns definition |
|
| 204 | 1 | * |
|
| 205 | 1 | * @param array $data |
|
| 206 | * |
||
| 207 | 1 | * @return array |
|
| 208 | 1 | */ |
|
| 209 | public static function filterColumns($data) : array |
||
| 213 | 1 | ||
| 214 | /** |
||
| 215 | 1 | * Fetching rows by SQL query |
|
| 216 | * |
||
| 217 | 1 | * @param string $sql SQL query with placeholders |
|
| 218 | * @param array $params Params for query placeholders |
||
| 219 | 5 | * |
|
| 220 | * @return RowInterface[] of rows results in FETCH_CLASS mode |
||
| 221 | */ |
||
| 222 | protected static function fetch($sql, $params = []) : array |
||
| 227 | 4 | ||
| 228 | /** |
||
| 229 | 4 | * {@inheritdoc} |
|
| 230 | 4 | * |
|
| 231 | * @throws DbException |
||
| 232 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
| 233 | * @throws \InvalidArgumentException |
||
| 234 | */ |
||
| 235 | public static function find(...$keys) : array |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | * |
||
| 264 | * @throws \InvalidArgumentException |
||
| 265 | * @throws Exception\DbException |
||
| 266 | */ |
||
| 267 | public static function findWhere(...$where) : array |
||
| 314 | |||
| 315 | 8 | /** |
|
| 316 | * {@inheritdoc} |
||
| 317 | * |
||
| 318 | * @throws DbException |
||
| 319 | * @throws \InvalidArgumentException |
||
| 320 | * @throws InvalidPrimaryKeyException |
||
| 321 | */ |
||
| 322 | public static function findRow($primaryKey) : ?RowInterface |
||
| 327 | |||
| 328 | 8 | /** |
|
| 329 | * {@inheritdoc} |
||
| 330 | * |
||
| 331 | 8 | * @throws DbException |
|
| 332 | * @throws \InvalidArgumentException |
||
| 333 | */ |
||
| 334 | public static function findRowWhere(array $whereList) : ?RowInterface |
||
| 339 | |||
| 340 | /** |
||
| 341 | 8 | * Prepare array for WHERE or SET statements |
|
| 342 | * |
||
| 343 | 8 | * @param array $where |
|
| 344 | * |
||
| 345 | * @return array |
||
| 346 | 8 | * @throws \Bluz\Common\Exception\ConfigurationException |
|
| 347 | 8 | */ |
|
| 348 | 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 | 8 | * ->execute(); |
|
| 368 | * |
||
| 369 | 8 | * // setup custom select "u.id, u.login" |
|
| 370 | * $select = Users\Table::select(); |
||
| 371 | 8 | * $arrUsers = $select->select('u.id, u.login') |
|
| 372 | * ->where('u.id = ?', $id) |
||
| 373 | 8 | * ->execute(); |
|
| 374 | * </code> |
||
| 375 | * |
||
| 376 | 8 | * @return Query\Select |
|
| 377 | 8 | */ |
|
| 378 | 8 | public static function select() : Query\Select |
|
| 389 | |||
| 390 | 8 | /** |
|
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | 8 | public static function create(array $data = []) : RowInterface |
|
| 401 | |||
| 402 | /** |
||
| 403 | 8 | * {@inheritdoc} |
|
| 404 | * |
||
| 405 | 8 | * @throws \Bluz\Common\Exception\ConfigurationException |
|
| 406 | * @throws Exception\DbException |
||
| 407 | */ |
||
| 408 | public static function insert(array $data) |
||
| 439 | 3 | ||
| 440 | 3 | /** |
|
| 441 | 3 | * {@inheritdoc} |
|
| 442 | * |
||
| 443 | 3 | * @throws \Bluz\Common\Exception\ConfigurationException |
|
| 444 | * @throws Exception\DbException |
||
| 445 | */ |
||
| 446 | public static function update(array $data, array $where) : int |
||
| 474 | 2 | ||
| 475 | /** |
||
| 476 | 2 | * {@inheritdoc} |
|
| 477 | * |
||
| 478 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 479 | * @throws \Bluz\Db\Exception\DbException |
||
| 480 | */ |
||
| 481 | public static function delete(array $where) : int |
||
| 506 | } |
||
| 507 |