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 columns |
||
| 55 | */ |
||
| 56 | protected $columns = []; |
||
| 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() |
|
|
|
|||
| 82 | { |
||
| 83 | 2 | $tableClass = static::class; |
|
| 84 | 2 | $namespace = class_namespace($tableClass); |
|
| 85 | |||
| 86 | // autodetect model name |
||
| 87 | 2 | if (!$this->model) { |
|
| 88 | 2 | $this->model = substr($namespace, strrpos($namespace, '\\') + 1); |
|
| 89 | } |
||
| 90 | |||
| 91 | // autodetect table name - camelCase to uppercase |
||
| 92 | 2 | if (!$this->name) { |
|
| 93 | $table = preg_replace('/(?<=\\w)(?=[A-Z])/', '_$1', $this->model); |
||
| 94 | $this->name = strtolower($table); |
||
| 95 | } |
||
| 96 | |||
| 97 | // autodetect row class |
||
| 98 | 2 | if (!$this->rowClass) { |
|
| 99 | 1 | $this->rowClass = $namespace . '\\Row'; |
|
| 100 | } |
||
| 101 | |||
| 102 | // setup default select query |
||
| 103 | 2 | if (empty($this->select)) { |
|
| 104 | 2 | $this->select = 'SELECT * ' . |
|
| 105 | 2 | 'FROM ' . DbProxy::quoteIdentifier($this->name); |
|
| 106 | } |
||
| 107 | |||
| 108 | 2 | Relations::addClassMap($this->model, $tableClass); |
|
| 109 | |||
| 110 | 2 | $this->init(); |
|
| 111 | 2 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Initialization hook. |
||
| 115 | * Subclasses may override this method |
||
| 116 | */ |
||
| 117 | 2 | public function init() |
|
| 118 | { |
||
| 119 | 2 | } |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get Table instance |
||
| 123 | * |
||
| 124 | * @return static |
||
| 125 | */ |
||
| 126 | 36 | public static function getInstance() |
|
| 127 | { |
||
| 128 | 36 | static $instance; |
|
| 129 | 36 | if (null === $instance) { |
|
| 130 | 3 | $instance = new static(); |
|
| 131 | } |
||
| 132 | |||
| 133 | 36 | return $instance; |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set select query |
||
| 138 | * |
||
| 139 | * @param string $select SQL query |
||
| 140 | * |
||
| 141 | * @return Table |
||
| 142 | */ |
||
| 143 | public function setSelectQuery($select) |
||
| 144 | { |
||
| 145 | $this->select = $select; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get select query |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getSelectQuery() |
||
| 155 | { |
||
| 156 | return $this->select; |
||
| 157 | } |
||
| 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 | 30 | public function getPrimaryKey() |
|
| 166 | { |
||
| 167 | 30 | if (!is_array($this->primary)) { |
|
| 168 | 1 | throw new InvalidPrimaryKeyException('The primary key must be set as an array'); |
|
| 169 | } |
||
| 170 | 29 | return $this->primary; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get table name |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 3 | public function getName() |
|
| 179 | { |
||
| 180 | 3 | return $this->name; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get model name |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 2 | public function getModel() |
|
| 189 | { |
||
| 190 | 2 | return $this->model; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return information about tables columns |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 3 | public function getColumns() |
|
| 199 | { |
||
| 200 | 3 | if (empty($this->columns)) { |
|
| 201 | 1 | $cacheKey = "db.table.{$this->name}"; |
|
| 202 | 1 | $columns = Cache::get($cacheKey); |
|
| 203 | 1 | if (!$columns) { |
|
| 204 | 1 | $schema = DbProxy::getOption('connect', 'name'); |
|
| 205 | |||
| 206 | 1 | $columns = DbProxy::fetchColumn( |
|
| 207 | ' |
||
| 208 | SELECT COLUMN_NAME |
||
| 209 | FROM INFORMATION_SCHEMA.COLUMNS |
||
| 210 | WHERE TABLE_SCHEMA = ? |
||
| 211 | 1 | AND TABLE_NAME = ?', |
|
| 212 | 1 | [$schema, $this->getName()] |
|
| 213 | ); |
||
| 214 | 1 | Cache::set($cacheKey, $columns, Cache::TTL_NO_EXPIRY, ['system', 'db']); |
|
| 215 | } |
||
| 216 | 1 | $this->columns = $columns; |
|
| 217 | } |
||
| 218 | 3 | return $this->columns; |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Filter columns for insert/update queries by table columns definition |
||
| 223 | * |
||
| 224 | * @param array $data |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | 3 | public static function filterColumns($data) |
|
| 229 | { |
||
| 230 | 3 | $self = static::getInstance(); |
|
| 231 | 3 | return array_intersect_key($data, array_flip($self->getColumns())); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Fetching rows by SQL query |
||
| 236 | * |
||
| 237 | * @param string $sql SQL query with placeholders |
||
| 238 | * @param array $params Params for query placeholders |
||
| 239 | * |
||
| 240 | * @return array of rows results in FETCH_CLASS mode |
||
| 241 | */ |
||
| 242 | 8 | public static function fetch($sql, $params = []) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Fetch all rows from table |
||
| 250 | * Be carefully with this method, can be very slow |
||
| 251 | * |
||
| 252 | * @return array of rows results in FETCH_CLASS mode |
||
| 253 | */ |
||
| 254 | public static function fetchAll() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Fetches rows by primary key. The argument specifies one or more primary |
||
| 262 | * key value(s). To find multiple rows by primary key, the argument must |
||
| 263 | * be an array. |
||
| 264 | * |
||
| 265 | * This method accepts a variable number of arguments. If the table has a |
||
| 266 | * multi-column primary key, the number of arguments must be the same as |
||
| 267 | * the number of columns in the primary key. To find multiple rows in a |
||
| 268 | * table with a multi-column primary key, each argument must be an array |
||
| 269 | * with the same number of elements. |
||
| 270 | * |
||
| 271 | * The find() method always returns a array |
||
| 272 | * |
||
| 273 | * Row by primary key, return array |
||
| 274 | * Table::find(123); |
||
| 275 | * |
||
| 276 | * Row by compound primary key, return array |
||
| 277 | * Table::find([123, 'abc']); |
||
| 278 | * |
||
| 279 | * Multiple rows by primary key |
||
| 280 | * Table::find(123, 234, 345); |
||
| 281 | * |
||
| 282 | * Multiple rows by compound primary key |
||
| 283 | * Table::find([123, 'abc'], [234, 'def'], [345, 'ghi']) |
||
| 284 | * |
||
| 285 | * @param mixed ...$keys The value(s) of the primary keys. |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
| 289 | */ |
||
| 290 | 10 | public static function find(...$keys) |
|
| 291 | { |
||
| 292 | 10 | $keyNames = array_values(static::getInstance()->getPrimaryKey()); |
|
| 293 | 10 | $whereList = []; |
|
| 294 | 10 | foreach ($keys as $keyValues) { |
|
| 295 | 10 | $keyValues = (array)$keyValues; |
|
| 296 | 10 | if (count($keyValues) < count($keyNames)) { |
|
| 297 | 2 | throw new InvalidPrimaryKeyException( |
|
| 298 | "Too few columns for the primary key.\n" . |
||
| 299 | 2 | "Please check " . static::class . " initialization or usage.\n" . |
|
| 300 | 2 | "Settings described at https://github.com/bluzphp/framework/wiki/Db-Table" |
|
| 301 | ); |
||
| 302 | } |
||
| 303 | |||
| 304 | 8 | if (count($keyValues) > count($keyNames)) { |
|
| 305 | throw new InvalidPrimaryKeyException( |
||
| 306 | "Too many columns for the primary key.\n" . |
||
| 307 | "Please check " . static::class . " initialization or usage.\n" . |
||
| 308 | "Settings described at https://github.com/bluzphp/framework/wiki/Db-Table" |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | |||
| 312 | 8 | if (array_keys($keyValues)[0] === 0) { |
|
| 313 | // for numerical array |
||
| 314 | 7 | $whereList[] = array_combine($keyNames, $keyValues); |
|
| 315 | } else { |
||
| 316 | // for assoc array |
||
| 317 | 8 | $whereList[] = $keyValues; |
|
| 318 | } |
||
| 319 | } |
||
| 320 | 8 | return static::findWhere(...$whereList); |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Find row by primary key |
||
| 325 | * |
||
| 326 | * @param mixed $primaryKey |
||
| 327 | * |
||
| 328 | * @return Row |
||
| 329 | */ |
||
| 330 | 8 | public static function findRow($primaryKey) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Find rows by WHERE |
||
| 341 | * // WHERE alias = 'foo' |
||
| 342 | * Table::findWhere(['alias'=>'foo']); |
||
| 343 | * // WHERE alias = 'foo' OR 'alias' = 'bar' |
||
| 344 | * Table::findWhere(['alias'=>'foo'], ['alias'=>'bar']); |
||
| 345 | * // WHERE (alias = 'foo' AND userId = 2) OR ('alias' = 'bar' AND userId = 4) |
||
| 346 | * Table::findWhere(['alias'=>'foo', 'userId'=> 2], ['alias'=>'foo', 'userId'=>4]); |
||
| 347 | * // WHERE alias IN ('foo', 'bar') |
||
| 348 | * Table::findWhere(['alias'=> ['foo', 'bar']]); |
||
| 349 | * |
||
| 350 | * @param mixed ...$where |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | * @throws \InvalidArgumentException |
||
| 354 | * @throws Exception\DbException |
||
| 355 | */ |
||
| 356 | 8 | public static function findWhere(...$where) |
|
| 357 | { |
||
| 358 | 8 | $self = static::getInstance(); |
|
| 359 | |||
| 360 | 8 | $whereParams = []; |
|
| 361 | |||
| 362 | 8 | if (count($where) === 2 && is_string($where[0])) { |
|
| 363 | $whereClause = $where[0]; |
||
| 364 | $whereParams = (array)$where[1]; |
||
| 365 | 8 | } elseif (count($where)) { |
|
| 366 | 8 | $whereOrTerms = []; |
|
| 367 | 8 | foreach ($where as $keyValueSets) { |
|
| 368 | 8 | $whereAndTerms = []; |
|
| 369 | 8 | foreach ($keyValueSets as $keyName => $keyValue) { |
|
| 370 | 8 | if (is_array($keyValue)) { |
|
| 371 | $keyValue = array_map( |
||
| 372 | function ($value) use ($self) { |
||
| 373 | return DbProxy::quote($value); |
||
| 374 | }, |
||
| 375 | $keyValue |
||
| 376 | ); |
||
| 377 | $keyValue = implode(',', $keyValue); |
||
| 378 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IN (' . $keyValue . ')'; |
||
| 379 | 8 | } elseif (is_null($keyValue)) { |
|
| 380 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IS NULL'; |
||
| 381 | } else { |
||
| 382 | 8 | $whereAndTerms[] = $self->name . '.' . $keyName . ' = ?'; |
|
| 383 | 8 | $whereParams[] = $keyValue; |
|
| 384 | } |
||
| 385 | 8 | if (!is_scalar($keyValue) && !is_null($keyValue)) { |
|
| 386 | throw new \InvalidArgumentException( |
||
| 387 | "Wrong arguments of method 'findWhere'.\n" . |
||
| 388 | 8 | "Please use syntax described at https://github.com/bluzphp/framework/wiki/Db-Table" |
|
| 389 | ); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | 8 | $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')'; |
|
| 393 | } |
||
| 394 | 8 | $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')'; |
|
| 395 | } else { |
||
| 396 | throw new DbException( |
||
| 397 | "Method `Table::findWhere()` can't return all records from table,\n" . |
||
| 398 | "please use `Table::fetchAll()` instead" |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | |||
| 402 | 8 | return static::fetch($self->select . ' WHERE ' . $whereClause, $whereParams); |
|
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Find row by where condition |
||
| 407 | * |
||
| 408 | * @param array $whereList |
||
| 409 | * |
||
| 410 | * @return Row |
||
| 411 | */ |
||
| 412 | public static function findRowWhere($whereList) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Prepare array for WHERE or SET statements |
||
| 420 | * |
||
| 421 | * @param array $where |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
| 425 | */ |
||
| 426 | 3 | private static function prepareStatement($where) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Prepare Db\Query\Select for current table: |
||
| 437 | * - predefine "select" section as "*" from current table |
||
| 438 | * - predefine "from" section as current table name and first letter as alias |
||
| 439 | * - predefine fetch type |
||
| 440 | * |
||
| 441 | * <code> |
||
| 442 | * // use default select "*" |
||
| 443 | * $select = Users\Table::select(); |
||
| 444 | * $arrUsers = $select->where('u.id = ?', $id) |
||
| 445 | * ->execute(); |
||
| 446 | * |
||
| 447 | * // setup custom select "u.id, u.login" |
||
| 448 | * $select = Users\Table::select(); |
||
| 449 | * $arrUsers = $select->select('u.id, u.login') |
||
| 450 | * ->where('u.id = ?', $id) |
||
| 451 | * ->execute(); |
||
| 452 | * </code> |
||
| 453 | * |
||
| 454 | * @return Query\Select |
||
| 455 | */ |
||
| 456 | 2 | public static function select() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Create Row instance |
||
| 470 | * |
||
| 471 | * @param array $data |
||
| 472 | * |
||
| 473 | * @return Row |
||
| 474 | */ |
||
| 475 | 2 | public static function create(array $data = []) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Insert new record to table and return last insert Id |
||
| 486 | * |
||
| 487 | * <code> |
||
| 488 | * Table::insert(['login' => 'Man', 'email' => '[email protected]']) |
||
| 489 | * </code> |
||
| 490 | * |
||
| 491 | * @param array $data Column-value pairs |
||
| 492 | * |
||
| 493 | * @return string|null Primary key or null |
||
| 494 | * @throws Exception\DbException |
||
| 495 | */ |
||
| 496 | 1 | public static function insert(array $data) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Updates existing rows |
||
| 530 | * |
||
| 531 | * <code> |
||
| 532 | * Table::insert(['login' => 'Man', 'email' => '[email protected]'], ['id' => 42]) |
||
| 533 | * </code> |
||
| 534 | * |
||
| 535 | * @param array $data Column-value pairs. |
||
| 536 | * @param array $where An array of SQL WHERE clause(s) |
||
| 537 | * |
||
| 538 | * @return integer The number of rows updated |
||
| 539 | * @throws Exception\DbException |
||
| 540 | */ |
||
| 541 | 1 | public static function update(array $data, array $where) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Deletes existing rows |
||
| 573 | * |
||
| 574 | * <code> |
||
| 575 | * Table::delete(['login' => 'Man']) |
||
| 576 | * </code> |
||
| 577 | * |
||
| 578 | * @param array $where An array of SQL WHERE clause(s) |
||
| 579 | * |
||
| 580 | * @return integer The number of rows deleted |
||
| 581 | * @throws Exception\DbException |
||
| 582 | */ |
||
| 583 | 1 | public static function delete(array $where) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Setup relation "one to one" or "one to many" |
||
| 611 | * |
||
| 612 | * @param string $key |
||
| 613 | * @param string $model |
||
| 614 | * @param string $foreign |
||
| 615 | * |
||
| 616 | * @return void |
||
| 617 | */ |
||
| 618 | public function linkTo($key, $model, $foreign) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Setup relation "many to many" |
||
| 625 | * [table1-key] [table1_key-table2-table3_key] [table3-key] |
||
| 626 | * |
||
| 627 | * @param string $model |
||
| 628 | * @param string $link |
||
| 629 | * |
||
| 630 | * @return void |
||
| 631 | */ |
||
| 632 | public function linkToMany($model, $link) |
||
| 636 | } |
||
| 637 |