Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Plan 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 Plan, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class Plan |
||
| 52 | { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * List of tables to be created |
||
| 56 | * |
||
| 57 | * @var \Phinx\Db\Plan\NewTable[] |
||
| 58 | */ |
||
| 59 | protected $tableCreates = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * List of table updates |
||
| 63 | * |
||
| 64 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 65 | */ |
||
| 66 | protected $tableUpdates = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * List of table removals or renames |
||
| 70 | * |
||
| 71 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 72 | */ |
||
| 73 | protected $tableMoves = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * List of index additions or removals |
||
| 77 | * |
||
| 78 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 79 | */ |
||
| 80 | protected $indexes = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List of constraint additions or removals |
||
| 84 | * |
||
| 85 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 86 | */ |
||
| 87 | protected $constraints = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Constructor |
||
| 91 | * |
||
| 92 | * @param Intent $intent All the actions that should be executed |
||
| 93 | */ |
||
| 94 | public function __construct(Intent $intent) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Parses the given Intent and creates the separate steps to execute |
||
| 101 | * |
||
| 102 | * @param Intent $actions The actions to use for the plan |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | protected function createPlan($actions) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a nested list of all the steps to execute |
||
| 117 | * |
||
| 118 | * @return AlterTable[][] |
||
| 119 | */ |
||
| 120 | protected function updatesSequence() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Executes this plan using the given AdapterInterface |
||
| 132 | * |
||
| 133 | * @param AdapterInterface $executor The executor object for the plan |
||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function execute(AdapterInterface $executor) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Executes the inverse plan (rollback the actions) with the given AdapterInterface:w |
||
| 151 | * |
||
| 152 | * @param AdapterInterface $executor The executor object for the plan |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function executeInverse(AdapterInterface $executor) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Deletes certain actions from the plan if they are found to be conflicting or redundant. |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | protected function resolveConflicts() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Deletes all actions related to the given table and keeps the |
||
| 207 | * rest |
||
| 208 | * |
||
| 209 | * @param Table $table The table to find in the list of actions |
||
| 210 | * @param AlterTable[] $actions The actions to transform |
||
| 211 | * @return AlterTable[] The list of actions without actions for the given table |
||
| 212 | */ |
||
| 213 | protected function forgetTable(Table $table, $actions) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Finds all DropForeignKey actions in an AlterTable and moves |
||
| 228 | * all conflicting DropIndex action in `$this->indexes` into the |
||
| 229 | * given AlterTable. |
||
| 230 | * |
||
| 231 | * @param AlterTable $alter The collection of actions to inspect |
||
| 232 | * @return AlterTable The updated AlterTable object. This function |
||
| 233 | * has the side effect of changing the `$this->indexes` property. |
||
| 234 | */ |
||
| 235 | protected function remapContraintAndIndexConflicts(AlterTable $alter) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Deletes any DropIndex actions for the given table and exact columns |
||
| 265 | * |
||
| 266 | * @param Table $table The table to find in the list of actions |
||
| 267 | * @param string[] $columns The column names to match |
||
| 268 | * @param AlterTable[] $actions The actions to transform |
||
| 269 | * @return array A tuple containing the list of actions without actions for dropping the index |
||
| 270 | * and a list of drop index actions that were removed. |
||
| 271 | */ |
||
| 272 | protected function forgetDropIndex(Table $table, array $columns, array $actions) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Finds conflixting DropForeignKey/AddForeignKey and returns a list |
||
| 312 | * of non conflicting AlterTable instructions. |
||
| 313 | * |
||
| 314 | * @param AlterTable $alter The collection of actions to inspect |
||
| 315 | * @return AlterTable[] A list of AlterTable that can be executed without |
||
| 316 | * this type of conflict |
||
| 317 | */ |
||
| 318 | protected function remapDropFkConflicts(AlterTable $alter) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Collects all table creation actions from the given intent |
||
| 366 | * |
||
| 367 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function gatherCreates($actions) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Collects all alter table actions from the given intent |
||
| 406 | * |
||
| 407 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | protected function gatherUpdates($actions) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Collects all alter table drop and renames from the given intent |
||
| 437 | * |
||
| 438 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 439 | * @return void |
||
| 440 | */ |
||
| 441 | protected function gatherTableMoves($actions) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Collects all index creation and drops from the given intent |
||
| 464 | * |
||
| 465 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | protected function gatherIndexes($actions) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Collects all foreign key creation and drops from the given intent |
||
| 494 | * |
||
| 495 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | protected function gatherConstraints($actions) |
||
| 516 | } |
||
| 517 |
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: