Complex classes like AbstractTable 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 AbstractTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | abstract class AbstractTable implements TableInterface |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Table states. |
||
| 53 | */ |
||
| 54 | const STATUS_NEW = 0; |
||
| 55 | const STATUS_EXISTS = 1; |
||
| 56 | const STATUS_DROPPED = 2; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Indication that table is exists and current schema is fetched from database. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | private $status = self::STATUS_NEW; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Database specific tablePrefix. Required for table renames. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $prefix = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @invisible |
||
| 74 | * |
||
| 75 | * @var Driver |
||
| 76 | */ |
||
| 77 | protected $driver = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Initial table state. |
||
| 81 | * |
||
| 82 | * @invisible |
||
| 83 | * @var TableState |
||
| 84 | */ |
||
| 85 | protected $initial = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Currently defined table state. |
||
| 89 | * |
||
| 90 | * @invisible |
||
| 91 | * @var TableState |
||
| 92 | */ |
||
| 93 | protected $current = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param Driver $driver Parent driver. |
||
| 97 | * @param string $name Table name, must include table prefix. |
||
| 98 | * @param string $prefix Database specific table prefix. |
||
| 99 | */ |
||
| 100 | public function __construct(Driver $driver, string $name, string $prefix) |
||
| 101 | { |
||
| 102 | $this->driver = $driver; |
||
| 103 | $this->prefix = $prefix; |
||
| 104 | |||
| 105 | //Initializing states |
||
| 106 | $this->initial = new TableState($this->prefix . $name); |
||
| 107 | $this->current = new TableState($this->prefix . $name); |
||
| 108 | |||
| 109 | if ($this->driver->hasTable($this->getName())) { |
||
| 110 | $this->status = self::STATUS_EXISTS; |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($this->exists()) { |
||
| 114 | //Initiating table schema |
||
| 115 | $this->initSchema($this->initial); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->setState($this->initial); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get instance of associated driver. |
||
| 123 | * |
||
| 124 | * @return Driver |
||
| 125 | */ |
||
| 126 | public function getDriver(): Driver |
||
| 127 | { |
||
| 128 | return $this->driver; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return database specific table prefix. |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getPrefix(): string |
||
| 137 | { |
||
| 138 | return $this->prefix; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return StateComparator |
||
| 143 | */ |
||
| 144 | public function getComparator(): StateComparator |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Check if table schema has been modified since synchronization. |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | protected function hasChanges(): bool |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function exists(): bool |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Table status (see codes above). |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | public function getStatus(): int |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Sets table name. Use this function in combination with save to rename table. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * |
||
| 182 | * @return string Prefixed table name. |
||
| 183 | */ |
||
| 184 | public function setName(string $name): string |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function getName(): string |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Table name before rename. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getInitialName(): string |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Declare table as dropped, you have to sync table using "save" method in order to apply this |
||
| 211 | * change. |
||
| 212 | */ |
||
| 213 | public function declareDropped() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set table primary keys. Operation can only be applied for newly created tables. Now every |
||
| 225 | * database might support compound indexes. |
||
| 226 | * |
||
| 227 | * @param array $columns |
||
| 228 | * |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function setPrimaryKeys(array $columns): AbstractTable |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | public function getPrimaryKeys(): array |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function hasColumn(string $name): bool |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | * |
||
| 261 | * @return AbstractColumn[] |
||
| 262 | */ |
||
| 263 | public function getColumns(): array |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function hasIndex(array $columns = []): bool |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | * |
||
| 279 | * @return AbstractIndex[] |
||
| 280 | */ |
||
| 281 | public function getIndexes(): array |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function hasForeign(string $column): bool |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | * |
||
| 297 | * @return AbstractReference[] |
||
| 298 | */ |
||
| 299 | public function getForeigns(): array |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function getDependencies(): array |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get/create instance of AbstractColumn associated with current table. |
||
| 319 | * |
||
| 320 | * Attention, renamed column will be available by it's old name until being synced! |
||
| 321 | * |
||
| 322 | * Examples: |
||
| 323 | * $table->column('name')->string(); |
||
| 324 | * |
||
| 325 | * @param string $name |
||
| 326 | * |
||
| 327 | * @return AbstractColumn |
||
| 328 | */ |
||
| 329 | public function column(string $name): AbstractColumn |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Shortcut for column() method. |
||
| 344 | * |
||
| 345 | * @param string $column |
||
| 346 | * |
||
| 347 | * @return AbstractColumn |
||
| 348 | */ |
||
| 349 | public function __get(string $column) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Column creation/altering shortcut, call chain is identical to: |
||
| 356 | * AbstractTable->column($name)->$type($arguments). |
||
| 357 | * |
||
| 358 | * Example: |
||
| 359 | * $table->string("name"); |
||
| 360 | * $table->text("some_column"); |
||
| 361 | * |
||
| 362 | * @param string $type |
||
| 363 | * @param array $arguments Type specific parameters. |
||
| 364 | * |
||
| 365 | * @return AbstractColumn |
||
| 366 | */ |
||
| 367 | public function __call(string $type, array $arguments) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get/create instance of AbstractIndex associated with current table based on list of forming |
||
| 377 | * column names. |
||
| 378 | * |
||
| 379 | * Example: |
||
| 380 | * $table->index(['key']); |
||
| 381 | * $table->index(['key', 'key2']); |
||
| 382 | * |
||
| 383 | * @param array $columns List of index columns. |
||
| 384 | * |
||
| 385 | * @return AbstractIndex |
||
| 386 | * |
||
| 387 | * @throws SchemaException |
||
| 388 | */ |
||
| 389 | public function index(array $columns): AbstractIndex |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get/create instance of AbstractReference associated with current table based on local column |
||
| 410 | * name. |
||
| 411 | * |
||
| 412 | * @param string $column |
||
| 413 | * |
||
| 414 | * @return AbstractReference |
||
| 415 | * |
||
| 416 | * @throws SchemaException |
||
| 417 | */ |
||
| 418 | public function foreign(string $column): AbstractReference |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Rename column (only if column exists). |
||
| 441 | * |
||
| 442 | * @param string $column |
||
| 443 | * @param string $name New column name. |
||
| 444 | * |
||
| 445 | * @return self |
||
| 446 | * |
||
| 447 | * @throws SchemaException |
||
| 448 | */ |
||
| 449 | public function renameColumn(string $column, string $name): AbstractTable |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Rename index (only if index exists). |
||
| 463 | * |
||
| 464 | * @param array $columns Index forming columns. |
||
| 465 | * @param string $name New index name. |
||
| 466 | * |
||
| 467 | * @return self |
||
| 468 | * |
||
| 469 | * @throws SchemaException |
||
| 470 | */ |
||
| 471 | public function renameIndex(array $columns, string $name): AbstractTable |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Drop column by it's name. |
||
| 487 | * |
||
| 488 | * @param string $column |
||
| 489 | * |
||
| 490 | * @return self |
||
| 491 | * |
||
| 492 | * @throws SchemaException |
||
| 493 | */ |
||
| 494 | public function dropColumn(string $column): AbstractTable |
||
| 495 | { |
||
| 496 | if (empty($schema = $this->current->findColumn($column))) { |
||
| 497 | throw new SchemaException("Undefined column '{$column}' in '{$this->getName()}'"); |
||
| 498 | } |
||
| 499 | |||
| 500 | //Dropping column from current schema |
||
| 501 | $this->current->forgetColumn($schema); |
||
| 502 | |||
| 503 | return $this; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Drop index by it's forming columns. |
||
| 508 | * |
||
| 509 | * @param array $columns |
||
| 510 | * |
||
| 511 | * @return self |
||
| 512 | * |
||
| 513 | * @throws SchemaException |
||
| 514 | */ |
||
| 515 | public function dropIndex(array $columns): AbstractTable |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Drop foreign key by it's name. |
||
| 531 | * |
||
| 532 | * @param string $column |
||
| 533 | * |
||
| 534 | * @return self |
||
| 535 | * |
||
| 536 | * @throws SchemaException |
||
| 537 | */ |
||
| 538 | public function dropForeign($column): AbstractTable |
||
| 539 | { |
||
| 540 | if (empty($schema = $this->current->findForeign($column))) { |
||
| 541 | throw new SchemaException( |
||
| 542 | "Undefined FK on '{$column}' in '{$this->getName()}'" |
||
| 543 | ); |
||
| 544 | } |
||
| 545 | |||
| 546 | //Dropping foreign from current schema |
||
| 547 | $this->current->forgetForeign($schema); |
||
| 548 | |||
| 549 | return $this; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Reset table state to new form. |
||
| 554 | * |
||
| 555 | * @param TableState $state Use null to flush table schema. |
||
| 556 | * |
||
| 557 | * @return self|$this |
||
| 558 | */ |
||
| 559 | public function setState(TableState $state = null): AbstractTable |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Reset table state to it initial form. |
||
| 573 | * |
||
| 574 | * @return self|$this |
||
| 575 | */ |
||
| 576 | public function resetState(): AbstractTable |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Save table schema including every column, index, foreign key creation/altering. If table |
||
| 585 | * does not exist it must be created. If table declared as dropped it will be removed from |
||
| 586 | * the database. |
||
| 587 | * |
||
| 588 | * @param int $behaviour Operation to be performed while table being saved. In some |
||
| 589 | * cases (when multiple tables are being updated) it is |
||
| 590 | * reasonable to drop foreing keys and indexes prior to |
||
| 591 | * dropping related columns. See sync bus class to get more |
||
| 592 | * details. |
||
| 593 | * @param LoggerInterface $logger Optional, aggregates messages for data syncing. |
||
| 594 | * @param bool $reset When true schema will be marked as synced. |
||
| 595 | * |
||
| 596 | * @throws HandlerException |
||
| 597 | * |
||
| 598 | * @throws SchemaException |
||
| 599 | */ |
||
| 600 | public function save( |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Ensure that no wrong indexes left in table. |
||
| 642 | * |
||
| 643 | * @return AbstractTable |
||
| 644 | */ |
||
| 645 | protected function prepareSchema() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return AbstractColumn|string |
||
| 705 | */ |
||
| 706 | public function __toString(): string |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Cloning schemas as well. |
||
| 713 | */ |
||
| 714 | public function __clone() |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @return array |
||
| 722 | */ |
||
| 723 | public function __debugInfo() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Populate table schema with values from database. |
||
| 736 | * |
||
| 737 | * @param TableState $state |
||
| 738 | */ |
||
| 739 | protected function initSchema(TableState $state) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Fetch index declarations from database. |
||
| 760 | * |
||
| 761 | * @return AbstractColumn[] |
||
| 762 | */ |
||
| 763 | abstract protected function fetchColumns(): array; |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Fetch index declarations from database. |
||
| 767 | * |
||
| 768 | * @return AbstractIndex[] |
||
| 769 | */ |
||
| 770 | abstract protected function fetchIndexes(): array; |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Fetch references declaration from database. |
||
| 774 | * |
||
| 775 | * @return AbstractReference[] |
||
| 776 | */ |
||
| 777 | abstract protected function fetchReferences(): array; |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Fetch names of primary keys from table. |
||
| 781 | * |
||
| 782 | * @return array |
||
| 783 | */ |
||
| 784 | abstract protected function fetchPrimaryKeys(): array; |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Create column with a given name. |
||
| 788 | * |
||
| 789 | * @param string $name |
||
| 790 | * |
||
| 791 | * @return AbstractColumn |
||
| 792 | */ |
||
| 793 | abstract protected function createColumn(string $name): AbstractColumn; |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Create index for a given set of columns. |
||
| 797 | * |
||
| 798 | * @param string $name |
||
| 799 | * |
||
| 800 | * @return AbstractIndex |
||
| 801 | */ |
||
| 802 | abstract protected function createIndex(string $name): AbstractIndex; |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Create reference on a given column set. |
||
| 806 | * |
||
| 807 | * @param string $name |
||
| 808 | * |
||
| 809 | * @return AbstractReference |
||
| 810 | */ |
||
| 811 | abstract protected function createForeign(string $name): AbstractReference; |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Generate unique name for indexes and foreign keys. |
||
| 815 | * |
||
| 816 | * @param string $type |
||
| 817 | * @param array $columns |
||
| 818 | * |
||
| 819 | * @return string |
||
| 820 | */ |
||
| 821 | protected function createIdentifier(string $type, array $columns): string |
||
| 832 | } |