Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
31 | { |
||
32 | const PERMISSION_CODE = 0755; |
||
33 | const REPEAT = 5; |
||
34 | const DIRECTORIES = [ |
||
35 | 'application', |
||
36 | 'data', |
||
37 | 'public', |
||
38 | 'tests' |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var Installer |
||
43 | */ |
||
44 | protected $installer; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $environment; |
||
50 | |||
51 | /** |
||
52 | * @var Filesystem |
||
53 | */ |
||
54 | protected $filesystem; |
||
55 | |||
56 | /** |
||
57 | * Create instance, define constants |
||
58 | */ |
||
59 | public function __construct() |
||
64 | |||
65 | /** |
||
66 | * Called after the plugin is loaded |
||
67 | * |
||
68 | * It setup composer installer |
||
69 | * |
||
70 | * {@inheritDoc} |
||
71 | */ |
||
72 | public function activate(Composer $composer, IOInterface $io) |
||
77 | |||
78 | /** |
||
79 | * Registered events after the plugin is loaded |
||
80 | * |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public static function getSubscribedEvents(): array |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Hook which is called after install package |
||
104 | * It copies bluz module |
||
105 | * |
||
106 | * @throws \InvalidArgumentException |
||
107 | */ |
||
108 | public function copyFiles() |
||
114 | |||
115 | /** |
||
116 | * Hook which is called before update package |
||
117 | * It checks bluz module |
||
118 | */ |
||
119 | public function removeFiles() |
||
125 | |||
126 | /** |
||
127 | * Copy extra files from compose.json of project |
||
128 | * |
||
129 | * @param Event $event |
||
130 | * |
||
131 | * @return void |
||
132 | * @throws \InvalidArgumentException |
||
133 | */ |
||
134 | public function copyExtraFiles(Event $event) |
||
145 | |||
146 | /** |
||
147 | * Remove extra files from compose.json of project |
||
148 | * |
||
149 | * @param Event $event |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public function removeExtraFiles(Event $event) |
||
160 | |||
161 | /** |
||
162 | * Get Filesystem |
||
163 | * |
||
164 | * @return Filesystem |
||
165 | */ |
||
166 | protected function getFilesystem() |
||
173 | |||
174 | /** |
||
175 | * getExtra |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function getExtraFiles() : array |
||
188 | |||
189 | /** |
||
190 | * Copy Module files |
||
191 | * |
||
192 | * @return void |
||
193 | * @throws \InvalidArgumentException |
||
194 | */ |
||
195 | protected function copyModule() |
||
214 | |||
215 | /** |
||
216 | * copyExtras |
||
217 | * |
||
218 | * @param array $files |
||
219 | * |
||
220 | * @return void |
||
221 | * @throws \InvalidArgumentException |
||
222 | */ |
||
223 | protected function copyExtras($files) |
||
232 | |||
233 | /** |
||
234 | * It recursively copies the files and directories |
||
235 | * |
||
236 | * @param $source |
||
237 | * @param $target |
||
238 | * |
||
239 | * @return void |
||
240 | * @throws \InvalidArgumentException |
||
241 | */ |
||
242 | protected function copy($source, $target) |
||
314 | |||
315 | /** |
||
316 | * It recursively removes the files and empty directories |
||
317 | * @return void |
||
318 | */ |
||
319 | protected function removeModule() |
||
335 | |||
336 | /** |
||
337 | * removeExtras |
||
338 | * |
||
339 | * @param array $files |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | protected function removeExtras($files) |
||
352 | |||
353 | /** |
||
354 | * It recursively removes the files and directories |
||
355 | * @param $directory |
||
356 | * @return void |
||
357 | */ |
||
358 | protected function remove($directory) |
||
420 | } |
||
421 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.