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 |
||
| 40 | class Table |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $name; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $options = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var AdapterInterface |
||
| 54 | */ |
||
| 55 | protected $adapter; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $columns = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $indexes = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ForeignKey[] |
||
| 69 | */ |
||
| 70 | protected $foreignKeys = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $data = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Class Constuctor. |
||
| 79 | * |
||
| 80 | * @param string $name Table Name |
||
| 81 | * @param array $options Options |
||
| 82 | * @param AdapterInterface $adapter Database Adapter |
||
| 83 | */ |
||
| 84 | 239 | public function __construct($name, $options = [], AdapterInterface $adapter = null) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Sets the table name. |
||
| 96 | * |
||
| 97 | * @param string $name Table Name |
||
| 98 | * @return Table |
||
| 99 | */ |
||
| 100 | 239 | public function setName($name) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Gets the table name. |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 215 | public function getName() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Sets the table options. |
||
| 118 | * |
||
| 119 | * @param array $options |
||
| 120 | * @return Table |
||
| 121 | */ |
||
| 122 | 239 | public function setOptions($options) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the table options. |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 189 | public function getOptions() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the database adapter. |
||
| 140 | * |
||
| 141 | * @param AdapterInterface $adapter Database Adapter |
||
| 142 | * @return Table |
||
| 143 | */ |
||
| 144 | 231 | public function setAdapter(AdapterInterface $adapter) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the database adapter. |
||
| 152 | * |
||
| 153 | * @return AdapterInterface |
||
| 154 | */ |
||
| 155 | 225 | public function getAdapter() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Does the table exist? |
||
| 162 | * |
||
| 163 | * @return boolean |
||
| 164 | */ |
||
| 165 | 195 | public function exists() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Drops the database table. |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | 1 | public function drop() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Renames the database table. |
||
| 182 | * |
||
| 183 | * @param string $newTableName New Table Name |
||
| 184 | * @return Table |
||
| 185 | */ |
||
| 186 | 3 | public function rename($newTableName) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Sets an array of columns waiting to be committed. |
||
| 195 | * Use setPendingColumns |
||
| 196 | * |
||
| 197 | * @deprecated |
||
| 198 | * @param array $columns Columns |
||
| 199 | * @return Table |
||
| 200 | */ |
||
| 201 | public function setColumns($columns) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Gets an array of the table columns. |
||
| 208 | * |
||
| 209 | * @return Column[] |
||
| 210 | */ |
||
| 211 | 10 | public function getColumns() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Sets an array of columns waiting to be committed. |
||
| 218 | * |
||
| 219 | * @param array $columns Columns |
||
| 220 | * @return Table |
||
| 221 | */ |
||
| 222 | 196 | public function setPendingColumns($columns) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Gets an array of columns waiting to be committed. |
||
| 230 | * |
||
| 231 | * @return Column[] |
||
| 232 | */ |
||
| 233 | 204 | public function getPendingColumns() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Sets an array of columns waiting to be indexed. |
||
| 240 | * |
||
| 241 | * @param array $indexes Indexes |
||
| 242 | * @return Table |
||
| 243 | */ |
||
| 244 | 196 | public function setIndexes($indexes) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Gets an array of indexes waiting to be committed. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 191 | public function getIndexes() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Sets an array of foreign keys waiting to be commited. |
||
| 262 | * |
||
| 263 | * @param ForeignKey[] $foreignKeys foreign keys |
||
| 264 | * @return Table |
||
| 265 | */ |
||
| 266 | 196 | public function setForeignKeys($foreignKeys) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Gets an array of foreign keys waiting to be commited. |
||
| 274 | * |
||
| 275 | * @return array|ForeignKey[] |
||
| 276 | */ |
||
| 277 | 192 | public function getForeignKeys() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Sets an array of data to be inserted. |
||
| 284 | * |
||
| 285 | * @param array $data Data |
||
| 286 | * @return Table |
||
| 287 | */ |
||
| 288 | 196 | public function setData($data) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets the data waiting to be inserted. |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 197 | public function getData() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Resets all of the pending table changes. |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 196 | public function reset() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Add a table column. |
||
| 319 | * |
||
| 320 | * Type can be: string, text, integer, float, decimal, datetime, timestamp, |
||
| 321 | * time, date, binary, boolean. |
||
| 322 | * |
||
| 323 | * Valid options can be: limit, default, null, precision or scale. |
||
| 324 | * |
||
| 325 | * @param string|Column $columnName Column Name |
||
| 326 | * @param string $type Column Type |
||
| 327 | * @param array $options Column Options |
||
| 328 | * @throws \RuntimeException |
||
| 329 | * @throws \InvalidArgumentException |
||
| 330 | * @return Table |
||
| 331 | */ |
||
| 332 | 210 | public function addColumn($columnName, $type = null, $options = []) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Remove a table column. |
||
| 364 | * |
||
| 365 | * @param string $columnName Column Name |
||
| 366 | * @return Table |
||
| 367 | */ |
||
| 368 | 1 | public function removeColumn($columnName) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Rename a table column. |
||
| 376 | * |
||
| 377 | * @param string $oldName Old Column Name |
||
| 378 | * @param string $newName New Column Name |
||
| 379 | * @return Table |
||
| 380 | */ |
||
| 381 | 4 | public function renameColumn($oldName, $newName) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Change a table column type. |
||
| 389 | * |
||
| 390 | * @param string $columnName Column Name |
||
| 391 | * @param string|Column $newColumnType New Column Type |
||
| 392 | * @param array $options Options |
||
| 393 | * @return Table |
||
| 394 | */ |
||
| 395 | 17 | public function changeColumn($columnName, $newColumnType, $options = []) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Checks to see if a column exists. |
||
| 417 | * |
||
| 418 | * @param string $columnName Column Name |
||
| 419 | * @return boolean |
||
| 420 | */ |
||
| 421 | 89 | public function hasColumn($columnName) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Add an index to a database table. |
||
| 428 | * |
||
| 429 | * In $options you can specific unique = true/false or name (index name). |
||
| 430 | * |
||
| 431 | * @param string|array|Index $columns Table Column(s) |
||
| 432 | * @param array $options Index Options |
||
| 433 | * @return Table |
||
| 434 | */ |
||
| 435 | 29 | public function addIndex($columns, $options = []) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Removes the given index from a table. |
||
| 455 | * |
||
| 456 | * @param array $columns Columns |
||
| 457 | * @return Table |
||
| 458 | */ |
||
| 459 | 1 | public function removeIndex($columns) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Removes the given index identified by its name from a table. |
||
| 467 | * |
||
| 468 | * @param string $name Index name |
||
| 469 | * @return Table |
||
| 470 | */ |
||
| 471 | 1 | public function removeIndexByName($name) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Checks to see if an index exists. |
||
| 479 | * |
||
| 480 | * @param string|array $columns Columns |
||
| 481 | * @param array $options Options |
||
|
|
|||
| 482 | * @return boolean |
||
| 483 | */ |
||
| 484 | 12 | public function hasIndex($columns) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Add a foreign key to a database table. |
||
| 491 | * |
||
| 492 | * In $options you can specify on_delete|on_delete = cascade|no_action .., |
||
| 493 | * on_update, constraint = constraint name. |
||
| 494 | * |
||
| 495 | * @param string|array $columns Columns |
||
| 496 | * @param string|Table $referencedTable Referenced Table |
||
| 497 | * @param string|array $referencedColumns Referenced Columns |
||
| 498 | * @param array $options Options |
||
| 499 | * @return Table |
||
| 500 | */ |
||
| 501 | 8 | public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], $options = []) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Removes the given foreign key from the table. |
||
| 522 | * |
||
| 523 | * @param string|array $columns Column(s) |
||
| 524 | * @param null|string $constraint Constraint names |
||
| 525 | * @return Table |
||
| 526 | */ |
||
| 527 | 1 | public function dropForeignKey($columns, $constraint = null) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Checks to see if a foreign key exists. |
||
| 543 | * |
||
| 544 | * @param string|array $columns Column(s) |
||
| 545 | * @param null|string $constraint Constraint names |
||
| 546 | * @return boolean |
||
| 547 | */ |
||
| 548 | 1 | public function hasForeignKey($columns, $constraint = null) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Add timestamp columns created_at and updated_at to the table. |
||
| 555 | * |
||
| 556 | * @param string $createdAtColumnName |
||
| 557 | * @param string $updatedAtColumnName |
||
| 558 | * |
||
| 559 | * @return Table |
||
| 560 | */ |
||
| 561 | 15 | public function addTimestamps($createdAtColumnName = 'created_at', $updatedAtColumnName = 'updated_at') |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Insert data into the table. |
||
| 579 | * |
||
| 580 | * @param $data array of data in the form: |
||
| 581 | * array( |
||
| 582 | * array("col1" => "value1", "col2" => "anotherValue1"), |
||
| 583 | * array("col2" => "value2", "col2" => "anotherValue2"), |
||
| 584 | * ) |
||
| 585 | * or array("col1" => "value1", "col2" => "anotherValue1") |
||
| 586 | * |
||
| 587 | * @return Table |
||
| 588 | */ |
||
| 589 | 17 | public function insert($data) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Creates a table from the object instance. |
||
| 604 | * |
||
| 605 | * @return void |
||
| 606 | */ |
||
| 607 | 196 | public function create() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Updates a table from the object instance. |
||
| 616 | * |
||
| 617 | * @throws \RuntimeException |
||
| 618 | * @return void |
||
| 619 | */ |
||
| 620 | 46 | public function update() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Commit the pending data waiting for insertion. |
||
| 645 | * |
||
| 646 | * @return void |
||
| 647 | */ |
||
| 648 | 196 | public function saveData() |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Truncates the table. |
||
| 677 | * |
||
| 678 | * @return void |
||
| 679 | */ |
||
| 680 | 2 | public function truncate() |
|
| 681 | { |
||
| 682 | 2 | $this->getAdapter()->truncateTable($this->getName()); |
|
| 683 | 2 | } |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Commits the table changes. |
||
| 687 | * |
||
| 688 | * If the table doesn't exist it is created otherwise it is updated. |
||
| 689 | * |
||
| 690 | * @return void |
||
| 691 | */ |
||
| 692 | 195 | public function save() |
|
| 702 | } |
||
| 703 |
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.