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() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Deletes all actions related to the given table and keeps the |
||
| 200 | * rest |
||
| 201 | * |
||
| 202 | * @param Table $table The table to find in the list of actions |
||
| 203 | * @param AlterTable[] $actions The actions to transform |
||
| 204 | * @return AlterTable[] The list of actions without actions for the given table |
||
| 205 | */ |
||
| 206 | protected function forgetTable(Table $table, $actions) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Finds all DropForeignKey actions in an AlterTable and moves |
||
| 221 | * all conflicting DropIndex action in `$this->indexes` into the |
||
| 222 | * given AlterTable. |
||
| 223 | * |
||
| 224 | * @param AlterTable $alter The collection of actions to inspect |
||
| 225 | * @return AlterTable The updated AlterTable object. This function |
||
| 226 | * has the side effect of changing the `$this->indexes` property. |
||
| 227 | */ |
||
| 228 | protected function remapContraintAndIndexConflicts(AlterTable $alter) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Deletes any DropIndex actions for the given table and exact columns |
||
| 258 | * |
||
| 259 | * @param Table $table The table to find in the list of actions |
||
| 260 | * @param string[] $columns The column names to match |
||
| 261 | * @param AlterTable[] $actions The actions to transform |
||
| 262 | * @return array A tuple containing the list of actions without actions for dropping the index |
||
| 263 | * and a list of drop index actions that were removed. |
||
| 264 | */ |
||
| 265 | protected function forgetDropIndex(Table $table, array $columns, array $actions) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Collects all table creation actions from the given intent |
||
| 305 | * |
||
| 306 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | protected function gatherCreates($actions) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Collects all alter table actions from the given intent |
||
| 345 | * |
||
| 346 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | protected function gatherUpdates($actions) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Collects all alter table drop and renames from the given intent |
||
| 376 | * |
||
| 377 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | protected function gatherTableMoves($actions) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Collects all index creation and drops from the given intent |
||
| 403 | * |
||
| 404 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | protected function gatherIndexes($actions) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Collects all foreign key creation and drops from the given intent |
||
| 433 | * |
||
| 434 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | protected function gatherConstraints($actions) |
||
| 455 | } |
||
| 456 |
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: