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 | 5 | protected function initDeployManager(Composer $composer, IOInterface $io, EventManager $eventManager) |
|
| 105 | |||
| 106 | 5 | 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 | 5 | private function getSortPriority(Composer $composer) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Apply plugin modifications to composer |
||
| 143 | * |
||
| 144 | * @param Composer $composer |
||
| 145 | * @param IOInterface $io |
||
| 146 | */ |
||
| 147 | 5 | 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 | 2 | public function onNewCodeEvent(Event $event) |
|
| 205 | { |
||
| 206 | |||
| 207 | 2 | $packageTypeToMatch = static::PACKAGE_TYPE; |
|
| 208 | 2 | $magentoModules = array_filter( |
|
| 209 | 2 | $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(), |
|
| 210 | 2 | function (PackageInterface $package) use ($packageTypeToMatch) { |
|
| 211 | 2 | if ($package instanceof AliasPackage) { |
|
| 212 | 1 | return false; |
|
| 213 | } |
||
| 214 | 2 | return $package->getType() === $packageTypeToMatch; |
|
| 215 | 2 | } |
|
| 216 | ); |
||
| 217 | |||
| 218 | 2 | $vendorDir = rtrim($this->composer->getConfig()->get(self::VENDOR_DIR_KEY), '/'); |
|
| 219 | |||
| 220 | 2 | Helper::initMagentoRootDir( |
|
| 221 | 2 | $this->config, |
|
| 222 | 2 | $this->io, |
|
| 223 | 2 | $this->filesystem, |
|
| 224 | $vendorDir |
||
| 225 | ); |
||
| 226 | |||
| 227 | 2 | $this->applyEvents($this->getEventManager()); |
|
| 228 | |||
| 229 | 2 | if (in_array('--redeploy', $event->getArguments())) { |
|
| 230 | $this->writeDebug('remove all deployed modules'); |
||
| 231 | $this->getModuleManager()->updateInstalledPackages(array()); |
||
| 232 | } |
||
| 233 | 2 | $this->writeDebug('start magento module deploy via moduleManager'); |
|
| 234 | 2 | $this->getModuleManager()->updateInstalledPackages($magentoModules); |
|
| 235 | 2 | $this->deployLibraries(); |
|
| 236 | |||
| 237 | 2 | $patcher = Bootstrap::fromConfig($this->config); |
|
| 238 | 2 | $patcher->setIo($this->io); |
|
| 239 | try { |
||
| 240 | 2 | $patcher->patch(); |
|
| 241 | 2 | } catch (\DomainException $e) { |
|
| 242 | 2 | $this->io->write('<comment>'.$e->getMessage().'</comment>'); |
|
| 243 | } |
||
| 244 | 2 | } |
|
| 245 | |||
| 246 | /** |
||
| 247 | * test configured repositories and give message about adding recommended ones |
||
| 248 | */ |
||
| 249 | 4 | protected function suggestComposerRepositories() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * deploy Libraries |
||
| 280 | */ |
||
| 281 | 2 | protected function deployLibraries() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * return the autoload generator binary path or false if not found |
||
| 331 | * |
||
| 332 | * @return bool|string |
||
| 333 | */ |
||
| 334 | protected function getTheseerAutoloadExecutable() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * get Theseer Autoload Generator Params |
||
| 358 | * |
||
| 359 | * @param string $libraryPath |
||
| 360 | * @param array $autoloadDirectories |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | protected function getTheseerAutoloadParams($libraryPath, $autoloadDirectories) |
||
| 365 | { |
||
| 366 | // @todo --blacklist 'test\\\\*' |
||
| 367 | return " -b {$libraryPath} -o {$libraryPath}/autoload.php " . implode(' ', $autoloadDirectories); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Copy then delete is a non-atomic version of {@link rename}. |
||
| 372 | * |
||
| 373 | * Some systems can't rename and also don't have proc_open, |
||
| 374 | * which requires this solution. |
||
| 375 | * |
||
| 376 | * copied from \Composer\Util\Filesystem::copyThenRemove and removed the remove part |
||
| 377 | * |
||
| 378 | * @param string $source |
||
| 379 | * @param string $target |
||
| 380 | */ |
||
| 381 | protected function copyRecursive($source, $target) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * print Debug Message |
||
| 399 | * |
||
| 400 | * @param $message |
||
| 401 | */ |
||
| 402 | 5 | private function writeDebug($message, $varDump = null) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @param PackageInterface $package |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getPackageInstallPath(PackageInterface $package) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return EventManager |
||
| 425 | */ |
||
| 426 | protected function getEventManager() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return ModuleManager |
||
| 437 | */ |
||
| 438 | protected function getModuleManager() |
||
| 455 | } |
||
| 456 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.