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 | * List of dropped columns |
||
92 | * |
||
93 | * @var \Phinx\Db\Plan\AlterTable[] |
||
94 | */ |
||
95 | protected $columnRemoves = []; |
||
96 | |||
97 | /** |
||
98 | * Constructor |
||
99 | * |
||
100 | * @param Intent $intent All the actions that should be executed |
||
101 | */ |
||
102 | public function __construct(Intent $intent) |
||
106 | |||
107 | /** |
||
108 | * Parses the given Intent and creates the separate steps to execute |
||
109 | * |
||
110 | * @param Intent $actions The actions to use for the plan |
||
111 | * @return void |
||
112 | */ |
||
113 | protected function createPlan($actions) |
||
122 | |||
123 | /** |
||
124 | * Returns a nested list of all the steps to execute |
||
125 | * |
||
126 | * @return AlterTable[][] |
||
127 | */ |
||
128 | View Code Duplication | protected function updatesSequence() |
|
138 | |||
139 | /** |
||
140 | * Returns a nested list of all the steps to execute in inverse order |
||
141 | * |
||
142 | * @return AlterTable[][] |
||
143 | */ |
||
144 | View Code Duplication | protected function inverseUpdatesSequence() |
|
154 | |||
155 | /** |
||
156 | * Executes this plan using the given AdapterInterface |
||
157 | * |
||
158 | * @param AdapterInterface $executor The executor object for the plan |
||
159 | * @return void |
||
160 | */ |
||
161 | View Code Duplication | public function execute(AdapterInterface $executor) |
|
173 | |||
174 | /** |
||
175 | * Executes the inverse plan (rollback the actions) with the given AdapterInterface:w |
||
176 | * |
||
177 | * @param AdapterInterface $executor The executor object for the plan |
||
178 | * @return void |
||
179 | */ |
||
180 | View Code Duplication | public function executeInverse(AdapterInterface $executor) |
|
192 | |||
193 | /** |
||
194 | * Deletes certain actions from the plan if they are found to be conflicting or redundant. |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | protected function resolveConflicts() |
||
252 | |||
253 | /** |
||
254 | * Deletes all actions related to the given table and keeps the |
||
255 | * rest |
||
256 | * |
||
257 | * @param Table $table The table to find in the list of actions |
||
258 | * @param AlterTable[] $actions The actions to transform |
||
259 | * @return AlterTable[] The list of actions without actions for the given table |
||
260 | */ |
||
261 | protected function forgetTable(Table $table, $actions) |
||
273 | |||
274 | /** |
||
275 | * Finds all DropForeignKey actions in an AlterTable and moves |
||
276 | * all conflicting DropIndex action in `$this->indexes` into the |
||
277 | * given AlterTable. |
||
278 | * |
||
279 | * @param AlterTable $alter The collection of actions to inspect |
||
280 | * @return AlterTable The updated AlterTable object. This function |
||
281 | * has the side effect of changing the `$this->indexes` property. |
||
282 | */ |
||
283 | protected function remapContraintAndIndexConflicts(AlterTable $alter) |
||
311 | |||
312 | /** |
||
313 | * Deletes any DropIndex actions for the given table and exact columns |
||
314 | * |
||
315 | * @param Table $table The table to find in the list of actions |
||
316 | * @param string[] $columns The column names to match |
||
317 | * @param AlterTable[] $actions The actions to transform |
||
318 | * @return array A tuple containing the list of actions without actions for dropping the index |
||
319 | * and a list of drop index actions that were removed. |
||
320 | */ |
||
321 | View Code Duplication | protected function forgetDropIndex(Table $table, array $columns, array $actions) |
|
358 | |||
359 | /** |
||
360 | * Deletes any RemoveColumn actions for the given table and exact columns |
||
361 | * |
||
362 | * @param Table $table The table to find in the list of actions |
||
363 | * @param string[] $columns The column names to match |
||
364 | * @param AlterTable[] $actions The actions to transform |
||
365 | * @return array A tuple containing the list of actions without actions for removing the column |
||
366 | * and a list of remove column actions that were removed. |
||
367 | */ |
||
368 | View Code Duplication | protected function forgetRemoveColumn(Table $table, array $columns, array $actions) |
|
405 | |||
406 | /** |
||
407 | * Collects all table creation actions from the given intent |
||
408 | * |
||
409 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
410 | * @return void |
||
411 | */ |
||
412 | protected function gatherCreates($actions) |
||
445 | |||
446 | /** |
||
447 | * Collects all alter table actions from the given intent |
||
448 | * |
||
449 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
450 | * @return void |
||
451 | */ |
||
452 | protected function gatherUpdates($actions) |
||
482 | |||
483 | /** |
||
484 | * Collects all alter table drop and renames from the given intent |
||
485 | * |
||
486 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
487 | * @return void |
||
488 | */ |
||
489 | protected function gatherTableMoves($actions) |
||
509 | |||
510 | /** |
||
511 | * Collects all index creation and drops from the given intent |
||
512 | * |
||
513 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
514 | * @return void |
||
515 | */ |
||
516 | protected function gatherIndexes($actions) |
||
539 | |||
540 | /** |
||
541 | * Collects all foreign key creation and drops from the given intent |
||
542 | * |
||
543 | * @param \Phinx\Db\Action\Action[] $actions The actions to parse |
||
544 | * @return void |
||
545 | */ |
||
546 | protected function gatherConstraints($actions) |
||
564 | } |
||
565 |
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: