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 |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Copy extra files from compose.json of project |
||
| 100 | * |
||
| 101 | * @param Event $event |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | * @throws \InvalidArgumentException |
||
| 105 | */ |
||
| 106 | public function copyProjectExtraFiles(Event $event) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Hook which is called after install package |
||
| 120 | * It copies bluz module |
||
| 121 | * |
||
| 122 | * @throws \InvalidArgumentException |
||
| 123 | */ |
||
| 124 | public function copyModuleFiles() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Hook which is called before update package |
||
| 133 | * It checks bluz module |
||
| 134 | */ |
||
| 135 | public function removeModuleFiles() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get Filesystem |
||
| 144 | * |
||
| 145 | * @return Filesystem |
||
| 146 | */ |
||
| 147 | protected function getFilesystem() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * getExtra |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | protected function getExtraFiles() : array |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Copy Module files |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | * @throws \InvalidArgumentException |
||
| 175 | */ |
||
| 176 | protected function copyModule() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * copyExtras |
||
| 198 | * |
||
| 199 | * @param array $files |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | * @throws \InvalidArgumentException |
||
| 203 | */ |
||
| 204 | protected function copyExtras($files) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * It recursively copies the files and directories |
||
| 216 | * |
||
| 217 | * @param $source |
||
| 218 | * @param $target |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | * @throws \InvalidArgumentException |
||
| 222 | */ |
||
| 223 | protected function copy($source, $target) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * It recursively removes the files and empty directories |
||
| 298 | * @return void |
||
| 299 | */ |
||
| 300 | protected function removeModule() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * removeExtras |
||
| 319 | * |
||
| 320 | * @param array $files |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | protected function removeExtras($files) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * It recursively removes the files and directories |
||
| 336 | * @param $directory |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | protected function remove($directory) |
||
| 401 | } |
||
| 402 |