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 |
||
| 19 | class Table extends AbstractAsset |
||
| 20 | { |
||
| 21 | /** @var Column[] */ |
||
| 22 | protected $_columns = []; |
||
| 23 | |||
| 24 | /** @var Index[] */ |
||
| 25 | private $implicitIndexes = []; |
||
| 26 | |||
| 27 | /** @var Index[] */ |
||
| 28 | protected $_indexes = []; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | protected $_primaryKeyName = false; |
||
| 32 | |||
| 33 | /** @var ForeignKeyConstraint[] */ |
||
| 34 | protected $_fkConstraints = []; |
||
| 35 | |||
| 36 | /** @var mixed[] */ |
||
| 37 | protected $_options = [ |
||
| 38 | 'create_options' => [], |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** @var SchemaConfig|null */ |
||
| 42 | protected $_schemaConfig = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $tableName |
||
| 46 | * @param Column[] $columns |
||
| 47 | * @param Index[] $indexes |
||
| 48 | * @param ForeignKeyConstraint[] $fkConstraints |
||
| 49 | * @param int $idGeneratorType |
||
| 50 | * @param mixed[] $options |
||
| 51 | * |
||
| 52 | * @throws DBALException |
||
| 53 | */ |
||
| 54 | 22007 | public function __construct($tableName, array $columns = [], array $indexes = [], array $fkConstraints = [], $idGeneratorType = 0, array $options = []) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | 20798 | public function setSchemaConfig(SchemaConfig $schemaConfig) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return int |
||
| 87 | */ |
||
| 88 | 20973 | protected function _getMaxIdentifierLength() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Sets the Primary Key. |
||
| 99 | * |
||
| 100 | * @param string[] $columnNames |
||
| 101 | * @param string|false $indexName |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | 21463 | public function setPrimaryKey(array $columnNames, $indexName = false) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string[] $columnNames |
||
| 119 | * @param string|null $indexName |
||
| 120 | * @param string[] $flags |
||
| 121 | * @param mixed[] $options |
||
| 122 | * |
||
| 123 | * @return self |
||
| 124 | */ |
||
| 125 | 19724 | public function addIndex(array $columnNames, $indexName = null, array $flags = [], array $options = []) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Drops the primary key from this table. |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | 18392 | public function dropPrimaryKey() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Drops an index from this table. |
||
| 151 | * |
||
| 152 | * @param string $indexName The index name. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | * |
||
| 156 | * @throws SchemaException If the index does not exist. |
||
| 157 | */ |
||
| 158 | 18416 | public function dropIndex($indexName) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param string[] $columnNames |
||
| 170 | * @param string|null $indexName |
||
| 171 | * @param mixed[] $options |
||
| 172 | * |
||
| 173 | * @return self |
||
| 174 | */ |
||
| 175 | 20678 | public function addUniqueIndex(array $columnNames, $indexName = null, array $options = []) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Renames an index. |
||
| 190 | * |
||
| 191 | * @param string $oldIndexName The name of the index to rename from. |
||
| 192 | * @param string|null $newIndexName The name of the index to rename to. |
||
| 193 | * If null is given, the index name will be auto-generated. |
||
| 194 | * |
||
| 195 | * @return self This table instance. |
||
| 196 | * |
||
| 197 | * @throws SchemaException If no index exists for the given current name |
||
| 198 | * or if an index with the given new name already exists on this table. |
||
| 199 | */ |
||
| 200 | 19052 | public function renameIndex($oldIndexName, $newIndexName = null) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Checks if an index begins in the order of the given columns. |
||
| 236 | * |
||
| 237 | * @param string[] $columnNames |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 19226 | public function columnsAreIndexed(array $columnNames) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string[] $columnNames |
||
| 254 | * @param string $indexName |
||
| 255 | * @param bool $isUnique |
||
| 256 | * @param bool $isPrimary |
||
| 257 | * @param string[] $flags |
||
| 258 | * @param mixed[] $options |
||
| 259 | * |
||
| 260 | * @return Index |
||
| 261 | * |
||
| 262 | * @throws SchemaException |
||
| 263 | */ |
||
| 264 | 21612 | private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = []) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $columnName |
||
| 281 | * @param string $typeName |
||
| 282 | * @param mixed[] $options |
||
| 283 | * |
||
| 284 | * @return Column |
||
| 285 | */ |
||
| 286 | 21829 | public function addColumn($columnName, $typeName, array $options = []) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Renames a Column. |
||
| 297 | * |
||
| 298 | * @deprecated |
||
| 299 | * |
||
| 300 | * @param string $oldColumnName |
||
| 301 | * @param string $newColumnName |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | * |
||
| 305 | * @throws DBALException |
||
| 306 | */ |
||
| 307 | public function renameColumn($oldColumnName, $newColumnName) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Change Column Details. |
||
| 316 | * |
||
| 317 | * @param string $columnName |
||
| 318 | * @param mixed[] $options |
||
| 319 | * |
||
| 320 | * @return self |
||
| 321 | */ |
||
| 322 | 18890 | public function changeColumn($columnName, array $options) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Drops a Column from the Table. |
||
| 332 | * |
||
| 333 | * @param string $columnName |
||
| 334 | * |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | 1325 | public function dropColumn($columnName) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Adds a foreign key constraint. |
||
| 347 | * |
||
| 348 | * Name is inferred from the local columns. |
||
| 349 | * |
||
| 350 | * @param Table|string $foreignTable Table schema instance or table name |
||
| 351 | * @param string[] $localColumnNames |
||
| 352 | * @param string[] $foreignColumnNames |
||
| 353 | * @param mixed[] $options |
||
| 354 | * @param string|null $constraintName |
||
| 355 | * |
||
| 356 | * @return self |
||
| 357 | */ |
||
| 358 | 20916 | public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [], $constraintName = null) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Adds a foreign key constraint. |
||
| 367 | * |
||
| 368 | * Name is to be generated by the database itself. |
||
| 369 | * |
||
| 370 | * @deprecated Use {@link addForeignKeyConstraint} |
||
| 371 | * |
||
| 372 | * @param Table|string $foreignTable Table schema instance or table name |
||
| 373 | * @param string[] $localColumnNames |
||
| 374 | * @param string[] $foreignColumnNames |
||
| 375 | * @param mixed[] $options |
||
| 376 | * |
||
| 377 | * @return self |
||
| 378 | */ |
||
| 379 | 14891 | public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = []) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Adds a foreign key constraint with a given name. |
||
| 386 | * |
||
| 387 | * @deprecated Use {@link addForeignKeyConstraint} |
||
| 388 | * |
||
| 389 | * @param string $name |
||
| 390 | * @param Table|string $foreignTable Table schema instance or table name |
||
| 391 | * @param string[] $localColumnNames |
||
| 392 | * @param string[] $foreignColumnNames |
||
| 393 | * @param mixed[] $options |
||
| 394 | * |
||
| 395 | * @return self |
||
| 396 | * |
||
| 397 | * @throws SchemaException |
||
| 398 | */ |
||
| 399 | 20917 | public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = []) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $name |
||
| 429 | * @param mixed $value |
||
| 430 | * |
||
| 431 | * @return self |
||
| 432 | */ |
||
| 433 | 20158 | public function addOption($name, $value) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @return void |
||
| 442 | * |
||
| 443 | * @throws SchemaException |
||
| 444 | */ |
||
| 445 | 21881 | protected function _addColumn(Column $column) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Adds an index to the table. |
||
| 459 | * |
||
| 460 | * @return self |
||
| 461 | * |
||
| 462 | * @throws SchemaException |
||
| 463 | */ |
||
| 464 | 21617 | protected function _addIndex(Index $indexCandidate) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | 20921 | protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Returns whether this table has a foreign key constraint with the given name. |
||
| 540 | * |
||
| 541 | * @param string $constraintName |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | 19050 | public function hasForeignKey($constraintName) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Returns the foreign key constraint with the given name. |
||
| 554 | * |
||
| 555 | * @param string $constraintName The constraint name. |
||
| 556 | * |
||
| 557 | * @return ForeignKeyConstraint |
||
| 558 | * |
||
| 559 | * @throws SchemaException If the foreign key does not exist. |
||
| 560 | */ |
||
| 561 | 267 | public function getForeignKey($constraintName) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Removes the foreign key constraint with the given name. |
||
| 573 | * |
||
| 574 | * @param string $constraintName The constraint name. |
||
| 575 | * |
||
| 576 | * @return void |
||
| 577 | * |
||
| 578 | * @throws SchemaException |
||
| 579 | */ |
||
| 580 | 268 | public function removeForeignKey($constraintName) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Returns ordered list of columns (primary keys are first, then foreign keys, then the rest) |
||
| 592 | * |
||
| 593 | * @return Column[] |
||
| 594 | */ |
||
| 595 | 21656 | public function getColumns() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Returns foreign key columns |
||
| 609 | * |
||
| 610 | * @return Column[] |
||
| 611 | */ |
||
| 612 | 21656 | private function getForeignKeyColumns() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Returns only columns that have specified names |
||
| 624 | * |
||
| 625 | * @param string[] $columnNames |
||
| 626 | * |
||
| 627 | * @return Column[] |
||
| 628 | */ |
||
| 629 | 21656 | private function filterColumns(array $columnNames) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Returns whether this table has a Column with the given name. |
||
| 638 | * |
||
| 639 | * @param string $columnName The column name. |
||
| 640 | * |
||
| 641 | * @return bool |
||
| 642 | */ |
||
| 643 | 21711 | public function hasColumn($columnName) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Returns the Column with the given name. |
||
| 652 | * |
||
| 653 | * @param string $columnName The column name. |
||
| 654 | * |
||
| 655 | * @return Column |
||
| 656 | * |
||
| 657 | * @throws SchemaException If the column does not exist. |
||
| 658 | */ |
||
| 659 | 21577 | public function getColumn($columnName) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Returns the primary key. |
||
| 671 | * |
||
| 672 | * @return Index|null The primary key, or null if this Table has no primary key. |
||
| 673 | */ |
||
| 674 | 21661 | public function getPrimaryKey() |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Returns the primary key columns. |
||
| 685 | * |
||
| 686 | * @return string[] |
||
| 687 | * |
||
| 688 | * @throws DBALException |
||
| 689 | */ |
||
| 690 | 18387 | public function getPrimaryKeyColumns() |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Returns whether this table has a primary key. |
||
| 703 | * |
||
| 704 | * @return bool |
||
| 705 | */ |
||
| 706 | 21664 | public function hasPrimaryKey() |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Returns whether this table has an Index with the given name. |
||
| 713 | * |
||
| 714 | * @param string $indexName The index name. |
||
| 715 | * |
||
| 716 | * @return bool |
||
| 717 | */ |
||
| 718 | 21402 | public function hasIndex($indexName) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Returns the Index with the given name. |
||
| 727 | * |
||
| 728 | * @param string $indexName The index name. |
||
| 729 | * |
||
| 730 | * @return Index |
||
| 731 | * |
||
| 732 | * @throws SchemaException If the index does not exist. |
||
| 733 | */ |
||
| 734 | 21380 | public function getIndex($indexName) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @return Index[] |
||
| 746 | */ |
||
| 747 | 21628 | public function getIndexes() |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Returns the foreign key constraints. |
||
| 754 | * |
||
| 755 | * @return ForeignKeyConstraint[] |
||
| 756 | */ |
||
| 757 | 21674 | public function getForeignKeys() |
|
| 761 | |||
| 762 | /** |
||
| 763 | * @param string $name |
||
| 764 | * |
||
| 765 | * @return bool |
||
| 766 | */ |
||
| 767 | 20587 | public function hasOption($name) |
|
| 771 | |||
| 772 | /** |
||
| 773 | * @param string $name |
||
| 774 | * |
||
| 775 | * @return mixed |
||
| 776 | */ |
||
| 777 | 18907 | public function getOption($name) |
|
| 781 | |||
| 782 | /** |
||
| 783 | * @return mixed[] |
||
| 784 | */ |
||
| 785 | 21521 | public function getOptions() |
|
| 789 | |||
| 790 | /** |
||
| 791 | * @return void |
||
| 792 | */ |
||
| 793 | 20704 | public function visit(Visitor $visitor) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Clone of a Table triggers a deep clone of all affected assets. |
||
| 812 | * |
||
| 813 | * @return void |
||
| 814 | */ |
||
| 815 | 19841 | public function __clone() |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Normalizes a given identifier. |
||
| 833 | * |
||
| 834 | * Trims quotes and lowercases the given identifier. |
||
| 835 | * |
||
| 836 | * @param string|null $identifier The identifier to normalize. |
||
| 837 | * |
||
| 838 | * @return string The normalized identifier. |
||
| 839 | */ |
||
| 840 | 21885 | private function normalizeIdentifier($identifier) |
|
| 848 | |||
| 849 | 18480 | public function setComment(?string $comment) : self |
|
| 856 | |||
| 857 | 18480 | public function getComment() : ?string |
|
| 861 | } |
||
| 862 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.