Complex classes like UninstallerTrait 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 UninstallerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | trait UninstallerTrait |
||
19 | { |
||
20 | |||
21 | private $tables; |
||
22 | |||
23 | /** |
||
24 | * @return \SpoonDatabase |
||
25 | */ |
||
26 | abstract protected function getDatabase(): \SpoonDatabase; |
||
27 | |||
28 | /** |
||
29 | * @return string |
||
30 | */ |
||
31 | abstract protected function getModule(): string; |
||
32 | |||
33 | /** |
||
34 | * @return null|\Symfony\Component\Console\Input\InputInterface |
||
35 | */ |
||
36 | abstract public function getInput(): ?InputInterface; |
||
37 | |||
38 | /** |
||
39 | * @return null|\Symfony\Component\Console\Output\OutputInterface |
||
40 | */ |
||
41 | abstract public function getOutput(): ?OutputInterface; |
||
42 | |||
43 | /** |
||
44 | * @param string|array $table |
||
45 | * @return bool |
||
46 | */ |
||
47 | protected function tableExists($table): bool |
||
64 | |||
65 | /** |
||
66 | * Full delete module |
||
67 | * |
||
68 | * @param string|null $module |
||
69 | */ |
||
70 | protected function dropModule(string $module = null): void |
||
84 | |||
85 | /** |
||
86 | * @param array|string $tables |
||
87 | */ |
||
88 | protected function dropDatabase($tables) |
||
105 | |||
106 | /** |
||
107 | * Delete a module. |
||
108 | * |
||
109 | * @param string $module The name of the module. |
||
110 | */ |
||
111 | protected function deleteModule(string $module): void |
||
121 | |||
122 | /** |
||
123 | * Delete searchable mark for module. |
||
124 | * |
||
125 | * @param string $module The name of the module. |
||
126 | */ |
||
127 | protected function deleteSearchable(string $module): void |
||
137 | |||
138 | /** |
||
139 | * Delete module locale. |
||
140 | * |
||
141 | * @param string $module The name of the module. |
||
142 | */ |
||
143 | protected function deleteLocale(string $module): void |
||
154 | |||
155 | /** |
||
156 | * Delete module settings. |
||
157 | * |
||
158 | * @param string $module The name of the module. |
||
159 | * @param string|null $name The name of the option. |
||
160 | */ |
||
161 | protected function deleteSettings(string $module, string $name = null): void |
||
179 | |||
180 | /** |
||
181 | * Inserts a new module. |
||
182 | * |
||
183 | * @param string $module The name of the module. |
||
184 | * @param array|null $labels |
||
185 | */ |
||
186 | protected function deleteModuleExtra(string $module, array $labels = null): void |
||
211 | |||
212 | /** |
||
213 | * Get a navigation item. |
||
214 | * |
||
215 | * @param int|null $parentId Id of the navigation item under we should add this. |
||
216 | * @param string $label Label for the item. |
||
217 | * @param string|null $url Url for the item. If omitted the first child is used. |
||
|
|||
218 | * @return int|null |
||
219 | */ |
||
220 | protected function getNavigation( |
||
237 | |||
238 | /** |
||
239 | * Delete a navigation item. |
||
240 | * |
||
241 | * @param string $path path of removed navigation |
||
242 | */ |
||
243 | protected function deleteNavigation($path): void |
||
283 | |||
284 | /** |
||
285 | * Delete the rights for an action |
||
286 | * |
||
287 | * @param string $module The module wherein the action appears. |
||
288 | */ |
||
289 | protected function deleteActionRights(string $module): void |
||
299 | |||
300 | /** |
||
301 | * Delete the rights for a module |
||
302 | * |
||
303 | * @param string $module The module too set the rights for. |
||
304 | */ |
||
305 | protected function deleteModuleRights(string $module): void |
||
315 | |||
316 | /** |
||
317 | * Delete dashboard widgets |
||
318 | * |
||
319 | * @param string $module |
||
320 | * @param array $widgets |
||
321 | */ |
||
322 | protected function deleteDashboardWidgets(string $module, array $widgets): void |
||
389 | |||
390 | /** |
||
391 | * Delete pages. |
||
392 | * |
||
393 | * @param array $pages |
||
394 | */ |
||
395 | protected function deletePages(array $pages): void |
||
405 | } |
||
406 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.