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 | public function copyFiles() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Hook which is called before update package |
||
| 115 | * It checks bluz module |
||
| 116 | */ |
||
| 117 | public function removeFiles() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Copy extra files from compose.json of project |
||
| 127 | * |
||
| 128 | * @param Event $event |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public function copyExtraFiles(Event $event) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Remove extra files from compose.json of project |
||
| 146 | * |
||
| 147 | * @param Event $event |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function removeExtraFiles(Event $event) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get Filesystem |
||
| 161 | * |
||
| 162 | * @return Filesystem |
||
| 163 | */ |
||
| 164 | protected function getFilesystem() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * getExtra |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | protected function getExtraFiles() : array |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Copy Module files |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | * @throws \InvalidArgumentException |
||
| 192 | */ |
||
| 193 | protected function copyModule() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * copyExtras |
||
| 215 | * |
||
| 216 | * @param array $files |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | * @throws \InvalidArgumentException |
||
| 220 | */ |
||
| 221 | protected function copyExtras($files) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * It recursively copies the files and directories |
||
| 233 | * |
||
| 234 | * @param $source |
||
| 235 | * @param $target |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | * @throws \InvalidArgumentException |
||
| 239 | */ |
||
| 240 | protected function copy($source, $target) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * It recursively removes the files and empty directories |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | protected function removeModule() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * removeExtras |
||
| 336 | * |
||
| 337 | * @param array $files |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | protected function removeExtras($files) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * It recursively removes the files and directories |
||
| 353 | * @param $directory |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | protected function remove($directory) |
||
| 418 | } |
||
| 419 |