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 |
||
37 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
38 | { |
||
39 | /** |
||
40 | * The type of packages this plugin supports |
||
41 | */ |
||
42 | const PACKAGE_TYPE = 'magento-module'; |
||
43 | |||
44 | const VENDOR_DIR_KEY = 'vendor-dir'; |
||
45 | |||
46 | const BIN_DIR_KEY = 'bin-dir'; |
||
47 | |||
48 | const THESEER_AUTOLOAD_EXEC_BIN_PATH = '/phpab'; |
||
49 | |||
50 | const THESEER_AUTOLOAD_EXEC_REL_PATH = '/theseer/autoload/composer/bin/phpab'; |
||
51 | |||
52 | /** |
||
53 | * @var IOInterface |
||
54 | */ |
||
55 | protected $io; |
||
56 | |||
57 | /** |
||
58 | * @var ProjectConfig |
||
59 | */ |
||
60 | protected $config; |
||
61 | |||
62 | /** |
||
63 | * @var DeployManager |
||
64 | */ |
||
65 | protected $deployManager; |
||
66 | |||
67 | /** |
||
68 | * @var Composer |
||
69 | */ |
||
70 | protected $composer; |
||
71 | |||
72 | /** |
||
73 | * @var Filesystem |
||
74 | */ |
||
75 | protected $filesystem; |
||
76 | |||
77 | /** |
||
78 | * @var EntryFactory |
||
79 | */ |
||
80 | protected $entryFactory; |
||
81 | |||
82 | /** |
||
83 | * @var EventManager |
||
84 | */ |
||
85 | private $eventManager; |
||
86 | |||
87 | /** |
||
88 | * @var ModuleManager |
||
89 | */ |
||
90 | private $moduleManager; |
||
91 | |||
92 | /** |
||
93 | * init the DeployManager |
||
94 | * |
||
95 | * @param Composer $composer |
||
96 | * @param IOInterface $io |
||
97 | */ |
||
98 | 6 | protected function initDeployManager(Composer $composer, IOInterface $io, EventManager $eventManager) |
|
105 | |||
106 | 6 | protected function applyEvents(EventManager $eventManager) |
|
124 | |||
125 | /** |
||
126 | * get Sort Priority from extra Config |
||
127 | * |
||
128 | * @param \Composer\Composer $composer |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 6 | private function getSortPriority(Composer $composer) |
|
140 | |||
141 | /** |
||
142 | * Apply plugin modifications to composer |
||
143 | * |
||
144 | * @param Composer $composer |
||
145 | * @param IOInterface $io |
||
146 | */ |
||
147 | 6 | public function activate(Composer $composer, IOInterface $io) |
|
168 | |||
169 | /** |
||
170 | * Returns an array of event names this subscriber wants to listen to. |
||
171 | * |
||
172 | * The array keys are event names and the value can be: |
||
173 | * |
||
174 | * * The method name to call (priority defaults to 0) |
||
175 | * * An array composed of the method name to call and the priority |
||
176 | * * An array of arrays composed of the method names to call and respective |
||
177 | * priorities, or 0 if unset |
||
178 | * |
||
179 | * For instance: |
||
180 | * |
||
181 | * * array('eventName' => 'methodName') |
||
182 | * * array('eventName' => array('methodName', $priority)) |
||
183 | * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) |
||
184 | * |
||
185 | * @return array The event names to listen to |
||
186 | */ |
||
187 | public static function getSubscribedEvents() |
||
198 | |||
199 | /** |
||
200 | * event listener is named this way, as it listens for events leading to changed code files |
||
201 | * |
||
202 | * @param Event $event |
||
203 | */ |
||
204 | 3 | public function onNewCodeEvent(Event $event) |
|
251 | |||
252 | /** |
||
253 | * test configured repositories and give message about adding recommended ones |
||
254 | */ |
||
255 | 5 | protected function suggestComposerRepositories() |
|
283 | |||
284 | /** |
||
285 | * deploy Libraries |
||
286 | */ |
||
287 | 3 | protected function deployLibraries() |
|
334 | |||
335 | /** |
||
336 | * return the autoload generator binary path or false if not found |
||
337 | * |
||
338 | * @return bool|string |
||
339 | */ |
||
340 | protected function getTheseerAutoloadExecutable() |
||
361 | |||
362 | /** |
||
363 | * get Theseer Autoload Generator Params |
||
364 | * |
||
365 | * @param string $libraryPath |
||
366 | * @param array $autoloadDirectories |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | protected function getTheseerAutoloadParams($libraryPath, $autoloadDirectories) |
||
375 | |||
376 | /** |
||
377 | * Copy then delete is a non-atomic version of {@link rename}. |
||
378 | * |
||
379 | * Some systems can't rename and also don't have proc_open, |
||
380 | * which requires this solution. |
||
381 | * |
||
382 | * copied from \Composer\Util\Filesystem::copyThenRemove and removed the remove part |
||
383 | * |
||
384 | * @param string $source |
||
385 | * @param string $target |
||
386 | */ |
||
387 | protected function copyRecursive($source, $target) |
||
402 | |||
403 | /** |
||
404 | * print Debug Message |
||
405 | * |
||
406 | * @param $message |
||
407 | */ |
||
408 | 6 | private function writeDebug($message, $varDump = null) |
|
418 | |||
419 | /** |
||
420 | * @param PackageInterface $package |
||
421 | * @return string |
||
422 | */ |
||
423 | public function getPackageInstallPath(PackageInterface $package) |
||
428 | |||
429 | /** |
||
430 | * @return EventManager |
||
431 | */ |
||
432 | protected function getEventManager() |
||
440 | |||
441 | /** |
||
442 | * @return ModuleManager |
||
443 | */ |
||
444 | protected function getModuleManager() |
||
461 | } |
||
462 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.