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 |
||
| 52 | class Plan |
||
| 53 | { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * List of tables to be created |
||
| 57 | * |
||
| 58 | * @var \Phinx\Db\Plan\NewTable[] |
||
| 59 | */ |
||
| 60 | protected $tableCreates = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * List of table updates |
||
| 64 | * |
||
| 65 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 66 | */ |
||
| 67 | protected $tableUpdates = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * List of table removals or renames |
||
| 71 | * |
||
| 72 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 73 | */ |
||
| 74 | protected $tableMoves = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * List of index additions or removals |
||
| 78 | * |
||
| 79 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 80 | */ |
||
| 81 | protected $indexes = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * List of constraint additions or removals |
||
| 85 | * |
||
| 86 | * @var \Phinx\Db\Plan\AlterTable[] |
||
| 87 | */ |
||
| 88 | protected $constraints = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Constructor |
||
| 92 | * |
||
| 93 | * @param Intent $intent All the actions that should be executed |
||
| 94 | */ |
||
| 95 | public function __construct(Intent $intent) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Parses the given Intent and creates the separate steps to execute |
||
| 102 | * |
||
| 103 | * @param Intent $actions The actions to use for the plan |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | protected function createPlan($actions) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns a nested list of all the steps to execute |
||
| 118 | * |
||
| 119 | * @return AlterTable[][] |
||
| 120 | */ |
||
| 121 | protected function updatesSequence() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Executes this plan using the given AdapterInterface |
||
| 133 | * |
||
| 134 | * @param AdapterInterface $executor The executor object for the plan |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function execute(AdapterInterface $executor) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Executes the inverse plan (rollback the actions) with the given AdapterInterface:w |
||
| 152 | * |
||
| 153 | * @param AdapterInterface $executor The executor object for the plan |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function executeInverse(AdapterInterface $executor) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Deletes certain actions from the plan if they are found to be conflicting or redundant. |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | protected function resolveConflicts() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Deletes all actions related to the given table and keeps the |
||
| 217 | * rest |
||
| 218 | * |
||
| 219 | * @param Table $table The table to find in the list of actions |
||
| 220 | * @param AlterTable[] $actions The actions to transform |
||
| 221 | * @return AlterTable[] The list of actions without actions for the given table |
||
| 222 | */ |
||
| 223 | protected function forgetTable(Table $table, $actions) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Finds all DropForeignKey actions in an AlterTable and moves |
||
| 238 | * all conflicting DropIndex action in `$this->indexes` into the |
||
| 239 | * given AlterTable. |
||
| 240 | * |
||
| 241 | * @param AlterTable $alter The collection of actions to inspect |
||
| 242 | * @return AlterTable The updated AlterTable object. This function |
||
| 243 | * has the side effect of changing the `$this->indexes` property. |
||
| 244 | */ |
||
| 245 | protected function remapContraintAndIndexConflicts(AlterTable $alter) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Deletes any DropIndex actions for the given table and exact columns |
||
| 275 | * |
||
| 276 | * @param Table $table The table to find in the list of actions |
||
| 277 | * @param string[] $columns The column names to match |
||
| 278 | * @param AlterTable[] $actions The actions to transform |
||
| 279 | * @return array A tuple containing the list of actions without actions for dropping the index |
||
| 280 | * and a list of drop index actions that were removed. |
||
| 281 | */ |
||
| 282 | protected function forgetDropIndex(Table $table, array $columns, array $actions) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Collects all table creation actions from the given intent |
||
| 322 | * |
||
| 323 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | protected function gatherCreates($actions) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Collects all alter table actions from the given intent |
||
| 362 | * |
||
| 363 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | protected function gatherUpdates($actions) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Collects all alter table drop and renames from the given intent |
||
| 393 | * |
||
| 394 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | protected function gatherTableMoves($actions) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Collects all index creation and drops from the given intent |
||
| 420 | * |
||
| 421 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 422 | * @return void |
||
| 423 | */ |
||
| 424 | protected function gatherIndexes($actions) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Collects all foreign key creation and drops from the given intent |
||
| 450 | * |
||
| 451 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | protected function gatherConstraints($actions) |
||
| 472 | } |
||
| 473 |
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: