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 |
||
| 53 | abstract class AbstractTable implements TableInterface |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Table states. |
||
| 57 | */ |
||
| 58 | const STATUS_NEW = 0; |
||
| 59 | const STATUS_EXISTS = 1; |
||
| 60 | const STATUS_DROPPED = 2; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Indication that table is exists and current schema is fetched from database. |
||
| 64 | * |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | private $status = self::STATUS_NEW; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Database specific tablePrefix. Required for table renames. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $prefix = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @invisible |
||
| 78 | * |
||
| 79 | * @var Driver |
||
| 80 | */ |
||
| 81 | protected $driver = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Initial table state. |
||
| 85 | * |
||
| 86 | * @invisible |
||
| 87 | * @var TableState |
||
| 88 | */ |
||
| 89 | protected $initial = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Currently defined table state. |
||
| 93 | * |
||
| 94 | * @invisible |
||
| 95 | * @var TableState |
||
| 96 | */ |
||
| 97 | protected $current = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Driver $driver Parent driver. |
||
| 101 | * @param string $name Table name, must include table prefix. |
||
| 102 | * @param string $prefix Database specific table prefix. |
||
| 103 | */ |
||
| 104 | public function __construct(Driver $driver, string $name, string $prefix) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get instance of associated driver. |
||
| 127 | * |
||
| 128 | * @return Driver |
||
| 129 | */ |
||
| 130 | public function getDriver(): Driver |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return database specific table prefix. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getPrefix(): string |
||
| 141 | { |
||
| 142 | return $this->prefix; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return StateComparator |
||
| 147 | */ |
||
| 148 | public function getComparator(): StateComparator |
||
| 149 | { |
||
| 150 | return new StateComparator($this->initial, $this->current); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Check if table schema has been modified since synchronization. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | protected function hasChanges(): bool |
||
| 159 | { |
||
| 160 | return $this->getComparator()->hasChanges(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function exists(): bool |
||
| 167 | { |
||
| 168 | return $this->status == self::STATUS_EXISTS || $this->status == self::STATUS_DROPPED; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Table status (see codes above). |
||
| 173 | * |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | public function getStatus(): int |
||
| 177 | { |
||
| 178 | return $this->status; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets table name. Use this function in combination with save to rename table. |
||
| 183 | * |
||
| 184 | * @param string $name |
||
| 185 | * |
||
| 186 | * @return string Prefixed table name. |
||
| 187 | */ |
||
| 188 | public function setName(string $name): string |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function getName(): string |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Table name before rename. |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getInitialName(): string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Declare table as dropped, you have to sync table using "save" method in order to apply this |
||
| 215 | * change. |
||
| 216 | */ |
||
| 217 | public function declareDropped() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set table primary keys. Operation can only be applied for newly created tables. Now every |
||
| 229 | * database might support compound indexes. |
||
| 230 | * |
||
| 231 | * @param array $columns |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | public function setPrimaryKeys(array $columns): AbstractTable |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function getPrimaryKeys(): array |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function hasColumn(string $name): bool |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | * |
||
| 265 | * @return AbstractColumn[] |
||
| 266 | */ |
||
| 267 | public function getColumns(): array |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function hasIndex(array $columns = []): bool |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | * |
||
| 283 | * @return AbstractIndex[] |
||
| 284 | */ |
||
| 285 | public function getIndexes(): array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function hasForeign(string $column): bool |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | * |
||
| 301 | * @return AbstractReference[] |
||
| 302 | */ |
||
| 303 | public function getForeigns(): array |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getDependencies(): array |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get/create instance of AbstractColumn associated with current table. |
||
| 323 | * |
||
| 324 | * Attention, renamed column will be available by it's old name until being synced! |
||
| 325 | * |
||
| 326 | * Examples: |
||
| 327 | * $table->column('name')->string(); |
||
| 328 | * |
||
| 329 | * @param string $name |
||
| 330 | * |
||
| 331 | * @return AbstractColumn |
||
| 332 | */ |
||
| 333 | public function column(string $name): AbstractColumn |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Shortcut for column() method. |
||
| 348 | * |
||
| 349 | * @param string $column |
||
| 350 | * |
||
| 351 | * @return AbstractColumn |
||
| 352 | */ |
||
| 353 | public function __get(string $column) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Column creation/altering shortcut, call chain is identical to: |
||
| 360 | * AbstractTable->column($name)->$type($arguments). |
||
| 361 | * |
||
| 362 | * Example: |
||
| 363 | * $table->string("name"); |
||
| 364 | * $table->text("some_column"); |
||
| 365 | * |
||
| 366 | * @param string $type |
||
| 367 | * @param array $arguments Type specific parameters. |
||
| 368 | * |
||
| 369 | * @return AbstractColumn |
||
| 370 | */ |
||
| 371 | public function __call(string $type, array $arguments) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get/create instance of AbstractIndex associated with current table based on list of forming |
||
| 381 | * column names. |
||
| 382 | * |
||
| 383 | * Example: |
||
| 384 | * $table->index(['key']); |
||
| 385 | * $table->index(['key', 'key2']); |
||
| 386 | * |
||
| 387 | * @param array $columns List of index columns. |
||
| 388 | * |
||
| 389 | * @return AbstractIndex |
||
| 390 | * |
||
| 391 | * @throws SchemaException |
||
| 392 | */ |
||
| 393 | public function index(array $columns): AbstractIndex |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get/create instance of AbstractReference associated with current table based on local column |
||
| 414 | * name. |
||
| 415 | * |
||
| 416 | * @param string $column |
||
| 417 | * |
||
| 418 | * @return AbstractReference |
||
| 419 | * |
||
| 420 | * @throws SchemaException |
||
| 421 | */ |
||
| 422 | public function foreign(string $column): AbstractReference |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Rename column (only if column exists). |
||
| 445 | * |
||
| 446 | * @param string $column |
||
| 447 | * @param string $name New column name. |
||
| 448 | * |
||
| 449 | * @return self |
||
| 450 | * |
||
| 451 | * @throws SchemaException |
||
| 452 | */ |
||
| 453 | public function renameColumn(string $column, string $name): AbstractTable |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Rename index (only if index exists). |
||
| 467 | * |
||
| 468 | * @param array $columns Index forming columns. |
||
| 469 | * @param string $name New index name. |
||
| 470 | * |
||
| 471 | * @return self |
||
| 472 | * |
||
| 473 | * @throws SchemaException |
||
| 474 | */ |
||
| 475 | public function renameIndex(array $columns, string $name): AbstractTable |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Drop column by it's name. |
||
| 491 | * |
||
| 492 | * @param string $column |
||
| 493 | * |
||
| 494 | * @return self |
||
| 495 | * |
||
| 496 | * @throws SchemaException |
||
| 497 | */ |
||
| 498 | public function dropColumn(string $column): AbstractTable |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Drop index by it's forming columns. |
||
| 512 | * |
||
| 513 | * @param array $columns |
||
| 514 | * |
||
| 515 | * @return self |
||
| 516 | * |
||
| 517 | * @throws SchemaException |
||
| 518 | */ |
||
| 519 | public function dropIndex(array $columns): AbstractTable |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Drop foreign key by it's name. |
||
| 535 | * |
||
| 536 | * @param string $column |
||
| 537 | * |
||
| 538 | * @return self |
||
| 539 | * |
||
| 540 | * @throws SchemaException |
||
| 541 | */ |
||
| 542 | public function dropForeign($column): AbstractTable |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Reset table state to new form. |
||
| 558 | * |
||
| 559 | * @param TableState $state Use null to flush table schema. |
||
| 560 | * |
||
| 561 | * @return self|$this |
||
| 562 | */ |
||
| 563 | public function setState(TableState $state = null): AbstractTable |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Reset table state to it initial form. |
||
| 577 | * |
||
| 578 | * @return self|$this |
||
| 579 | */ |
||
| 580 | public function resetState(): AbstractTable |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Save table schema including every column, index, foreign key creation/altering. If table |
||
| 589 | * does not exist it must be created. If table declared as dropped it will be removed from |
||
| 590 | * the database. |
||
| 591 | * |
||
| 592 | * @param int $behaviour Operation to be performed while table being saved. In some |
||
| 593 | * cases (when multiple tables are being updated) it is |
||
| 594 | * reasonable to drop foreing keys and indexes prior to |
||
| 595 | * dropping related columns. See sync bus class to get more |
||
| 596 | * details. |
||
| 597 | * @param LoggerInterface $logger Optional, aggregates messages for data syncing. |
||
| 598 | * |
||
| 599 | * @throws HandlerException |
||
| 600 | */ |
||
| 601 | public function save(int $behaviour = AbstractHandler::DO_ALL, LoggerInterface $logger = null) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Ensure that no wrong indexes left in table. |
||
| 638 | * |
||
| 639 | * @return AbstractTable |
||
| 640 | */ |
||
| 641 | protected function prepareSchema() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @return AbstractColumn|string |
||
| 701 | */ |
||
| 702 | public function __toString(): string |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Cloning schemas as well. |
||
| 709 | */ |
||
| 710 | public function __clone() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @return array |
||
| 718 | */ |
||
| 719 | public function __debugInfo() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Populate table schema with values from database. |
||
| 732 | * |
||
| 733 | * @param TableState $state |
||
| 734 | */ |
||
| 735 | protected function initSchema(TableState $state) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Fetch index declarations from database. |
||
| 756 | * |
||
| 757 | * @return AbstractColumn[] |
||
| 758 | */ |
||
| 759 | abstract protected function fetchColumns(): array; |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Fetch index declarations from database. |
||
| 763 | * |
||
| 764 | * @return AbstractIndex[] |
||
| 765 | */ |
||
| 766 | abstract protected function fetchIndexes(): array; |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Fetch references declaration from database. |
||
| 770 | * |
||
| 771 | * @return AbstractReference[] |
||
| 772 | */ |
||
| 773 | abstract protected function fetchReferences(): array; |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Fetch names of primary keys from table. |
||
| 777 | * |
||
| 778 | * @return array |
||
| 779 | */ |
||
| 780 | abstract protected function fetchPrimaryKeys(): array; |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Create column with a given name. |
||
| 784 | * |
||
| 785 | * @param string $name |
||
| 786 | * |
||
| 787 | * @return AbstractColumn |
||
| 788 | */ |
||
| 789 | abstract protected function createColumn(string $name): AbstractColumn; |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Create index for a given set of columns. |
||
| 793 | * |
||
| 794 | * @param string $name |
||
| 795 | * |
||
| 796 | * @return AbstractIndex |
||
| 797 | */ |
||
| 798 | abstract protected function createIndex(string $name): AbstractIndex; |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Create reference on a given column set. |
||
| 802 | * |
||
| 803 | * @param string $name |
||
| 804 | * |
||
| 805 | * @return AbstractReference |
||
| 806 | */ |
||
| 807 | abstract protected function createForeign(string $name): AbstractReference; |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Generate unique name for indexes and foreign keys. |
||
| 811 | * |
||
| 812 | * @param string $type |
||
| 813 | * @param array $columns |
||
| 814 | * |
||
| 815 | * @return string |
||
| 816 | */ |
||
| 817 | protected function createIdentifier(string $type, array $columns): string |
||
| 828 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: