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 EXTRA_OPTION_NAME = 'config-plugin'; |
||
| 33 | const OUTPUT_PATH = 'hiqdev/config'; |
||
| 34 | const BASE_DIR_SAMPLE = '<base-dir>'; |
||
| 35 | const VENDOR_DIR_SAMPLE = '<base-dir>/vendor'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var PackageInterface[] the array of active composer packages |
||
| 39 | */ |
||
| 40 | protected $packages; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string absolute path to the package base directory |
||
| 44 | */ |
||
| 45 | protected $baseDir; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string absolute path to vendor directory |
||
| 49 | */ |
||
| 50 | protected $vendorDir; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Filesystem utility |
||
| 54 | */ |
||
| 55 | protected $filesystem; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array assembled config data |
||
| 59 | */ |
||
| 60 | protected $data = [ |
||
| 61 | 'aliases' => [], |
||
| 62 | 'extensions' => [], |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array raw collected data |
||
| 67 | */ |
||
| 68 | protected $raw = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array array of not yet merged params |
||
| 72 | */ |
||
| 73 | protected $rawParams = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Composer instance |
||
| 77 | */ |
||
| 78 | protected $composer; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var IOInterface |
||
| 82 | */ |
||
| 83 | public $io; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initializes the plugin object with the passed $composer and $io. |
||
| 87 | * @param Composer $composer |
||
| 88 | * @param IOInterface $io |
||
| 89 | */ |
||
| 90 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Returns list of events the plugin is subscribed to. |
||
| 98 | * @return array list of events |
||
| 99 | */ |
||
| 100 | 1 | public static function getSubscribedEvents() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * This is the main function. |
||
| 111 | * @param Event $event |
||
| 112 | */ |
||
| 113 | public function onPostAutoloadDump(Event $event) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Scans the given package and collects extensions data. |
||
| 132 | * @param PackageInterface $package |
||
| 133 | */ |
||
| 134 | public function processPackage(PackageInterface $package) |
||
| 135 | { |
||
| 136 | $extra = $package->getExtra(); |
||
| 137 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
| 138 | if ($package->getType() !== self::PACKAGE_TYPE && is_null($files)) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | $extension = [ |
||
| 143 | 'name' => $package->getPrettyName(), |
||
| 144 | 'version' => $package->getVersion(), |
||
| 145 | ]; |
||
| 146 | if ($package->getVersion() === '9999999-dev') { |
||
| 147 | $reference = $package->getSourceReference() ?: $package->getDistReference(); |
||
| 148 | if ($reference) { |
||
| 149 | $extension['reference'] = $reference; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $aliases = array_merge( |
||
| 154 | $this->prepareAliases($package, 'psr-0'), |
||
| 155 | $this->prepareAliases($package, 'psr-4') |
||
| 156 | ); |
||
| 157 | |||
| 158 | if (isset($files['defines'])) { |
||
| 159 | foreach ((array) $files['defines'] as $file) { |
||
| 160 | $this->readConfigFile($package, $file); |
||
| 161 | } |
||
| 162 | unset($files['defines']); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (isset($files['params'])) { |
||
| 166 | foreach ((array) $files['params'] as $file) { |
||
| 167 | $this->rawParams[] = $this->readConfigFile($package, $file); |
||
| 168 | } |
||
| 169 | unset($files['params']); |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->raw[$package->getPrettyName()] = [ |
||
| 173 | 'package' => $package, |
||
| 174 | 'extension' => $extension, |
||
| 175 | 'aliases' => $aliases, |
||
| 176 | 'files' => (array) $files, |
||
| 177 | ]; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function assembleParams() |
||
| 184 | |||
| 185 | public function assembleConfigs() |
||
| 186 | { |
||
| 187 | $allAliases = []; |
||
| 188 | $extensions = []; |
||
| 189 | $rawConfigs = [ |
||
| 190 | 'aliases' => [], |
||
| 191 | 'extensions' => [], |
||
| 192 | ]; |
||
| 193 | |||
| 194 | foreach ($this->raw as $name => $info) { |
||
| 195 | $rawConfigs['extensions'][] = [ |
||
| 196 | $name => $info['extension'], |
||
| 197 | ]; |
||
| 198 | |||
| 199 | $aliases = $info['aliases']; |
||
| 200 | $rawConfigs['aliases'][] = $aliases; |
||
| 201 | |||
| 202 | foreach ($info['files'] as $name => $pathes) { |
||
| 203 | foreach ((array) $pathes as $path) { |
||
| 204 | $rawConfigs[$name][] = $this->readConfigFile($info['package'], $path); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | foreach ($rawConfigs as $name => $configs) { |
||
| 210 | if (!in_array($name, ['params', 'aliases', 'extensions'], true)) { |
||
| 211 | $configs[] = [ |
||
| 212 | 'params' => $this->data['params'], |
||
| 213 | 'aliases' => $this->data['aliases'], |
||
| 214 | ]; |
||
| 215 | } |
||
| 216 | $this->assembleFile($name, $configs); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function assembleFile($name, array $configs) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Read extra config. |
||
| 228 | * @param string $file |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function readConfigFile(PackageInterface $package, $file) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Prepare aliases. |
||
| 254 | * |
||
| 255 | * @param PackageInterface $package |
||
| 256 | * @param string 'psr-0' or 'psr-4' |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Substitute path with alias if applicable. |
||
| 287 | * @param string $path |
||
| 288 | * @param string $dir |
||
| 289 | * @param string $alias |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function substitutePath($path, $dir, $alias) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Builds path inside of a package. |
||
| 299 | * @param PackageInterface $package |
||
| 300 | * @param mixed $path can be absolute or relative |
||
| 301 | * @return string absolute pathes will stay untouched |
||
| 302 | */ |
||
| 303 | public function preparePath(PackageInterface $package, $path) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get output dir. |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getOutputDir() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Build full path to write file for a given filename. |
||
| 324 | * @param string $filename |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function buildOutputPath($filename) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Writes config file. |
||
| 334 | * @param string $filename |
||
| 335 | * @param array $data |
||
| 336 | */ |
||
| 337 | protected function writeFile($filename, array $data) |
||
| 346 | |||
| 347 | 2 | /** |
|
| 348 | 2 | * Sets [[packages]]. |
|
| 349 | * @param PackageInterface[] $packages |
||
| 350 | */ |
||
| 351 | public function setPackages(array $packages) |
||
| 355 | |||
| 356 | 1 | /** |
|
| 357 | * Gets [[packages]]. |
||
| 358 | * @return \Composer\Package\PackageInterface[] |
||
| 359 | */ |
||
| 360 | 1 | public function getPackages() |
|
| 368 | |||
| 369 | protected $plainList = []; |
||
| 370 | protected $orderedList = []; |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns ordered list of packages: |
||
| 374 | * - listed earlier in the composer.json will get earlier in the list |
||
| 375 | * - childs before parents. |
||
| 376 | * @return \Composer\Package\PackageInterface[] |
||
| 377 | */ |
||
| 378 | public function findPackages() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Iterates through package dependencies. |
||
| 398 | * @param PackageInterface $package to iterate |
||
| 399 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 400 | */ |
||
| 401 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Iterates dependencies of the given package. |
||
| 415 | * @param PackageInterface $package |
||
| 416 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 417 | */ |
||
| 418 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get absolute path to package base dir. |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getBaseDir() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get absolute path to composer vendor dir. |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function getVendorDir() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Getter for filesystem utility. |
||
| 464 | * @return Filesystem |
||
| 465 | */ |
||
| 466 | public function getFilesystem() |
||
| 474 | } |
||
| 475 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.