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 |
||
| 29 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 30 | { |
||
| 31 | const PACKAGE_TYPE = 'yii2-extension'; |
||
| 32 | const CONFIG_PATH_OPTION = 'extension-config'; |
||
| 33 | const YII2_EXTENSIONS_FILE = 'yiisoft/extensions.php'; |
||
| 34 | const MERGED_CONFIG_FILE = 'hiqdev/extensions-config.php'; |
||
| 35 | const BASE_DIR_ALIAS = '<base-dir>'; |
||
| 36 | const VENDOR_DIR_ALIAS = '<base-dir>/vendor'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var PackageInterface[] the array of active composer packages |
||
| 40 | */ |
||
| 41 | protected $packages; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string absolute path to the package base directory. |
||
| 45 | */ |
||
| 46 | protected $baseDir; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string absolute path to vendor directory. |
||
| 50 | */ |
||
| 51 | protected $vendorDir; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Filesystem utility |
||
| 55 | */ |
||
| 56 | protected $filesystem; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array extra configuration |
||
| 60 | */ |
||
| 61 | protected $extraconfig = [ |
||
| 62 | 'aliases' => [ |
||
| 63 | '@vendor' => self::VENDOR_DIR_ALIAS, |
||
| 64 | ], |
||
| 65 | ]; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array extensions |
||
| 69 | */ |
||
| 70 | protected $extensions = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Composer instance |
||
| 74 | */ |
||
| 75 | protected $composer; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var IOInterface |
||
| 79 | */ |
||
| 80 | public $io; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Initializes the plugin object with the passed $composer and $io. |
||
| 84 | * @param Composer $composer |
||
| 85 | * @param IOInterface $io |
||
| 86 | * @void |
||
| 87 | */ |
||
| 88 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns list of events the plugin is subscribed to. |
||
| 96 | * @return array list of events |
||
| 97 | */ |
||
| 98 | 1 | public static function getSubscribedEvents() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Simply rewrites extensions file from scratch. |
||
| 109 | * @param Event $event |
||
| 110 | * @void |
||
| 111 | */ |
||
| 112 | public function onPostAutoloadDump(Event $event) |
||
|
|
|||
| 113 | { |
||
| 114 | $this->io->writeError('<info>Generating yii2 config files</info>'); |
||
| 115 | $this->processPackage($this->composer->getPackage()); |
||
| 116 | foreach ($this->getPackages() as $package) { |
||
| 117 | if ($package instanceof \Composer\Package\CompletePackageInterface |
||
| 118 | && ($package->getType() === self::PACKAGE_TYPE || $this->extractConfigPath($package))) { |
||
| 119 | $this->processPackage($package); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // $this->saveFile(static::YII2_EXTENSIONS_FILE, $this->extensions); |
||
| 124 | $this->saveFile(static::YII2_EXTENSIONS_FILE, []); |
||
| 125 | $this->saveFile(static::MERGED_CONFIG_FILE, $this->extraconfig); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Writes file. |
||
| 130 | * @param string $file |
||
| 131 | * @param array $data |
||
| 132 | * @void |
||
| 133 | */ |
||
| 134 | protected function saveFile($file, array $data) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Scans the given package and collects extensions data. |
||
| 146 | * @param PackageInterface $package |
||
| 147 | * @void |
||
| 148 | */ |
||
| 149 | public function processPackage(PackageInterface $package) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Merges two or more arrays into one recursively. |
||
| 177 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
| 178 | * @param array $a array to be merged to |
||
| 179 | * @param array $b array to be merged from |
||
| 180 | * @return array the merged array |
||
| 181 | */ |
||
| 182 | public static function mergeConfig($a, $b) |
||
| 207 | |||
| 208 | public function extractConfigPath(PackageInterface $package) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Read extra config. |
||
| 216 | * @param string $file |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function readExtraConfig(PackageInterface $package, $file) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Prepare aliases. |
||
| 231 | * |
||
| 232 | * @param PackageInterface $package |
||
| 233 | * @param string 'psr-0' or 'psr-4' |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Substitute path with alias if applicable. |
||
| 264 | * @param string $path |
||
| 265 | * @param string $dir |
||
| 266 | * @param string $alias |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function substitutePath($path, $dir, $alias) |
||
| 273 | |||
| 274 | public function preparePath(PackageInterface $package, $path) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Sets [[packages]]. |
||
| 286 | * @param PackageInterface[] $packages |
||
| 287 | * @void |
||
| 288 | */ |
||
| 289 | 2 | public function setPackages(array $packages) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets [[packages]]. |
||
| 296 | * @return \Composer\Package\PackageInterface[] |
||
| 297 | */ |
||
| 298 | 1 | public function getPackages() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get absolute path to package base dir. |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getBaseDir() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get absolute path to composer vendor dir. |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getVendorDir() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Getter for filesystem utility. |
||
| 336 | * @return Filesystem |
||
| 337 | */ |
||
| 338 | public function getFilesystem() |
||
| 346 | } |
||
| 347 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.