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 | class Table |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $name; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $options = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
| 55 | */ |
||
| 56 | protected $adapter; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $columns = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $indexes = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \Phinx\Db\Table\ForeignKey[] |
||
| 70 | */ |
||
| 71 | protected $foreignKeys = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $data = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Class Constuctor. |
||
| 80 | * |
||
| 81 | * @param string $name Table Name |
||
| 82 | * @param array $options Options |
||
| 83 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
||
| 84 | 239 | */ |
|
| 85 | public function __construct($name, $options = [], AdapterInterface $adapter = null) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Sets the table name. |
||
| 97 | * |
||
| 98 | * @param string $name Table Name |
||
| 99 | * @return \Phinx\Db\Table |
||
| 100 | 239 | */ |
|
| 101 | public function setName($name) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Gets the table name. |
||
| 110 | * |
||
| 111 | 215 | * @return string |
|
| 112 | */ |
||
| 113 | 215 | public function getName() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the table options. |
||
| 120 | * |
||
| 121 | * @param array $options |
||
| 122 | 239 | * @return \Phinx\Db\Table |
|
| 123 | */ |
||
| 124 | 239 | public function setOptions($options) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Gets the table options. |
||
| 133 | 189 | * |
|
| 134 | * @return array |
||
| 135 | 189 | */ |
|
| 136 | public function getOptions() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Sets the database adapter. |
||
| 143 | * |
||
| 144 | 231 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
|
| 145 | * @return \Phinx\Db\Table |
||
| 146 | 231 | */ |
|
| 147 | 231 | public function setAdapter(AdapterInterface $adapter) |
|
| 153 | |||
| 154 | /** |
||
| 155 | 225 | * Gets the database adapter. |
|
| 156 | * |
||
| 157 | 225 | * @return \Phinx\Db\Adapter\AdapterInterface |
|
| 158 | */ |
||
| 159 | public function getAdapter() |
||
| 163 | |||
| 164 | /** |
||
| 165 | 195 | * Does the table exist? |
|
| 166 | * |
||
| 167 | 195 | * @return bool |
|
| 168 | */ |
||
| 169 | public function exists() |
||
| 173 | |||
| 174 | /** |
||
| 175 | 1 | * Drops the database table. |
|
| 176 | * |
||
| 177 | 1 | * @return void |
|
| 178 | 1 | */ |
|
| 179 | public function drop() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Renames the database table. |
||
| 186 | 3 | * |
|
| 187 | * @param string $newTableName New Table Name |
||
| 188 | 3 | * @return \Phinx\Db\Table |
|
| 189 | 3 | */ |
|
| 190 | 3 | public function rename($newTableName) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Sets an array of columns waiting to be committed. |
||
| 200 | * Use setPendingColumns |
||
| 201 | * |
||
| 202 | * @deprecated |
||
| 203 | * @param array $columns Columns |
||
| 204 | * @return \Phinx\Db\Table |
||
| 205 | */ |
||
| 206 | public function setColumns($columns) |
||
| 212 | |||
| 213 | 10 | /** |
|
| 214 | * Gets an array of the table columns. |
||
| 215 | * |
||
| 216 | * @return \Phinx\Db\Table\Column[] |
||
| 217 | */ |
||
| 218 | public function getColumns() |
||
| 222 | 196 | ||
| 223 | /** |
||
| 224 | 196 | * Sets an array of columns waiting to be committed. |
|
| 225 | 196 | * |
|
| 226 | * @param array $columns Columns |
||
| 227 | * @return \Phinx\Db\Table |
||
| 228 | */ |
||
| 229 | public function setPendingColumns($columns) |
||
| 235 | 204 | ||
| 236 | /** |
||
| 237 | * Gets an array of columns waiting to be committed. |
||
| 238 | * |
||
| 239 | * @return \Phinx\Db\Table\Column[] |
||
| 240 | */ |
||
| 241 | public function getPendingColumns() |
||
| 245 | |||
| 246 | 196 | /** |
|
| 247 | 196 | * Sets an array of columns waiting to be indexed. |
|
| 248 | * |
||
| 249 | * @param array $indexes Indexes |
||
| 250 | * @return \Phinx\Db\Table |
||
| 251 | */ |
||
| 252 | public function setIndexes($indexes) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets an array of indexes waiting to be committed. |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getIndexes() |
||
| 268 | 196 | ||
| 269 | 196 | /** |
|
| 270 | * Sets an array of foreign keys waiting to be commited. |
||
| 271 | * |
||
| 272 | * @param \Phinx\Db\Table\ForeignKey[] $foreignKeys foreign keys |
||
| 273 | * @return \Phinx\Db\Table |
||
| 274 | */ |
||
| 275 | public function setForeignKeys($foreignKeys) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets an array of foreign keys waiting to be commited. |
||
| 284 | * |
||
| 285 | * @return array|\Phinx\Db\Table\ForeignKey[] |
||
| 286 | */ |
||
| 287 | public function getForeignKeys() |
||
| 291 | 196 | ||
| 292 | /** |
||
| 293 | * Sets an array of data to be inserted. |
||
| 294 | * |
||
| 295 | * @param array $data Data |
||
| 296 | * @return \Phinx\Db\Table |
||
| 297 | */ |
||
| 298 | public function setData($data) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Gets the data waiting to be inserted. |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | 196 | */ |
|
| 310 | public function getData() |
||
| 314 | 196 | ||
| 315 | 196 | /** |
|
| 316 | * Resets all of the pending table changes. |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function reset() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Add a table column. |
||
| 330 | * |
||
| 331 | * Type can be: string, text, integer, float, decimal, datetime, timestamp, |
||
| 332 | 210 | * time, date, binary, boolean. |
|
| 333 | * |
||
| 334 | * Valid options can be: limit, default, null, precision or scale. |
||
| 335 | 210 | * |
|
| 336 | 1 | * @param string|\Phinx\Db\Table\Column $columnName Column Name |
|
| 337 | * @param string $type Column Type |
||
| 338 | * @param array $options Column Options |
||
| 339 | * @throws \RuntimeException |
||
| 340 | 209 | * @throws \InvalidArgumentException |
|
| 341 | 207 | * @return \Phinx\Db\Table |
|
| 342 | 207 | */ |
|
| 343 | 207 | public function addColumn($columnName, $type = null, $options = []) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Add a table column with a custom type definition. |
||
| 378 | * |
||
| 379 | * Type can be any string that you expect your DBMS to understand |
||
| 380 | * |
||
| 381 | 4 | * Valid options can be: default or null. |
|
| 382 | * |
||
| 383 | 4 | * For widely supported types @see addColumn |
|
| 384 | 4 | * |
|
| 385 | * @param string|CustomColumn $columnName Column Name |
||
| 386 | * @param string $type Column Type |
||
| 387 | * @param array $options Column Options |
||
| 388 | * @return Table |
||
| 389 | */ |
||
| 390 | public function addCustomColumn($columnName, $type = null, $options = []) |
||
| 406 | |||
| 407 | 17 | /** |
|
| 408 | 15 | * Remove a table column. |
|
| 409 | 15 | * |
|
| 410 | * @param string $columnName Column Name |
||
| 411 | 17 | * @return \Phinx\Db\Table |
|
| 412 | 17 | */ |
|
| 413 | public function removeColumn($columnName) |
||
| 419 | |||
| 420 | /** |
||
| 421 | 89 | * Rename a table column. |
|
| 422 | * |
||
| 423 | 89 | * @param string $oldName Old Column Name |
|
| 424 | * @param string $newName New Column Name |
||
| 425 | * @return \Phinx\Db\Table |
||
| 426 | */ |
||
| 427 | public function renameColumn($oldName, $newName) |
||
| 433 | |||
| 434 | /** |
||
| 435 | 29 | * Change a table column type. |
|
| 436 | * |
||
| 437 | * @param string $columnName Column Name |
||
| 438 | 29 | * @param string|\Phinx\Db\Table\Column $newColumnType New Column Type |
|
| 439 | 28 | * @param array $options Options |
|
| 440 | 28 | * @return \Phinx\Db\Table |
|
| 441 | 22 | */ |
|
| 442 | 22 | public function changeColumn($columnName, $newColumnType, $options = []) |
|
| 462 | 1 | ||
| 463 | /** |
||
| 464 | * Checks to see if a column exists. |
||
| 465 | * |
||
| 466 | * @param string $columnName Column Name |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | public function hasColumn($columnName) |
||
| 473 | 1 | ||
| 474 | 1 | /** |
|
| 475 | * Add an index to a database table. |
||
| 476 | * |
||
| 477 | * In $options you can specific unique = true/false or name (index name). |
||
| 478 | * |
||
| 479 | * @param string|array|\Phinx\Db\Table\Index $columns Table Column(s) |
||
| 480 | * @param array $options Index Options |
||
| 481 | * @return \Phinx\Db\Table |
||
| 482 | */ |
||
| 483 | public function addIndex($columns, $options = []) |
||
| 501 | 8 | ||
| 502 | /** |
||
| 503 | 8 | * Removes the given index from a table. |
|
| 504 | 4 | * |
|
| 505 | 4 | * @param array $columns Columns |
|
| 506 | 8 | * @return \Phinx\Db\Table |
|
| 507 | 8 | */ |
|
| 508 | public function removeIndex($columns) |
||
| 514 | 8 | ||
| 515 | 8 | /** |
|
| 516 | * Removes the given index identified by its name from a table. |
||
| 517 | 8 | * |
|
| 518 | * @param string $name Index name |
||
| 519 | * @return \Phinx\Db\Table |
||
| 520 | */ |
||
| 521 | public function removeIndexByName($name) |
||
| 527 | 1 | ||
| 528 | /** |
||
| 529 | 1 | * Checks to see if an index exists. |
|
| 530 | 1 | * |
|
| 531 | 1 | * @param string|array $columns Columns |
|
| 532 | 1 | * @param array $options Options |
|
|
|
|||
| 533 | * @return bool |
||
| 534 | */ |
||
| 535 | 1 | public function hasIndex($columns) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Add a foreign key to a database table. |
||
| 542 | * |
||
| 543 | * In $options you can specify on_delete|on_delete = cascade|no_action .., |
||
| 544 | * on_update, constraint = constraint name. |
||
| 545 | * |
||
| 546 | * @param string|array $columns Columns |
||
| 547 | * @param string|\Phinx\Db\Table $referencedTable Referenced Table |
||
| 548 | 1 | * @param string|array $referencedColumns Referenced Columns |
|
| 549 | * @param array $options Options |
||
| 550 | 1 | * @return \Phinx\Db\Table |
|
| 551 | */ |
||
| 552 | public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], $options = []) |
||
| 570 | 15 | ||
| 571 | /** |
||
| 572 | 15 | * Removes the given foreign key from the table. |
|
| 573 | * |
||
| 574 | 15 | * @param string|array $columns Column(s) |
|
| 575 | * @param null|string $constraint Constraint names |
||
| 576 | * @return \Phinx\Db\Table |
||
| 577 | */ |
||
| 578 | public function dropForeignKey($columns, $constraint = null) |
||
| 591 | |||
| 592 | 17 | /** |
|
| 593 | 11 | * Checks to see if a foreign key exists. |
|
| 594 | 11 | * |
|
| 595 | 11 | * @param string|array $columns Column(s) |
|
| 596 | 11 | * @param null|string $constraint Constraint names |
|
| 597 | * @return bool |
||
| 598 | 8 | */ |
|
| 599 | 8 | public function hasForeignKey($columns, $constraint = null) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Add timestamp columns created_at and updated_at to the table. |
||
| 606 | * |
||
| 607 | 196 | * @param string $createdAtColumnName |
|
| 608 | * @param string $updatedAtColumnName |
||
| 609 | 196 | * |
|
| 610 | 196 | * @return \Phinx\Db\Table |
|
| 611 | 196 | */ |
|
| 612 | 196 | public function addTimestamps($createdAtColumnName = 'created_at', $updatedAtColumnName = 'updated_at') |
|
| 627 | 46 | ||
| 628 | 38 | /** |
|
| 629 | 46 | * Insert data into the table. |
|
| 630 | * |
||
| 631 | 46 | * @param array $data array of data in the form: |
|
| 632 | 6 | * array( |
|
| 633 | 46 | * array("col1" => "value1", "col2" => "anotherValue1"), |
|
| 634 | * array("col2" => "value2", "col2" => "anotherValue2"), |
||
| 635 | 46 | * ) |
|
| 636 | 3 | * or array("col1" => "value1", "col2" => "anotherValue1") |
|
| 637 | 46 | * |
|
| 638 | * @return \Phinx\Db\Table |
||
| 639 | 46 | */ |
|
| 640 | 46 | public function insert($data) |
|
| 654 | |||
| 655 | 12 | /** |
|
| 656 | 12 | * Creates a table from the object instance. |
|
| 657 | 12 | * |
|
| 658 | 12 | * @return void |
|
| 659 | 12 | */ |
|
| 660 | 12 | public function create() |
|
| 666 | 12 | ||
| 667 | 11 | /** |
|
| 668 | 11 | * Updates a table from the object instance. |
|
| 669 | 1 | * |
|
| 670 | 1 | * @throws \RuntimeException |
|
| 671 | 1 | * @return void |
|
| 672 | */ |
||
| 673 | 12 | public function update() |
|
| 695 | 45 | ||
| 696 | 45 | /** |
|
| 697 | 195 | * Commit the pending data waiting for insertion. |
|
| 698 | * |
||
| 699 | * @return void |
||
| 700 | 195 | */ |
|
| 701 | 195 | public function saveData() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Truncates the table. |
||
| 730 | * |
||
| 731 | * @return void |
||
| 732 | */ |
||
| 733 | public function truncate() |
||
| 734 | { |
||
| 735 | $this->getAdapter()->truncateTable($this->getName()); |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Commits the table changes. |
||
| 740 | * |
||
| 741 | * If the table doesn't exist it is created otherwise it is updated. |
||
| 742 | * |
||
| 743 | * @return void |
||
| 744 | */ |
||
| 745 | public function save() |
||
| 755 | } |
||
| 756 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.