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 | */ |
||
| 30 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 31 | { |
||
| 32 | const OUTPUT_DIR = 'output'; |
||
| 33 | |||
| 34 | const PACKAGE_TYPE = 'yii2-extension'; |
||
| 35 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
| 36 | const VENDOR_DIR_SAMPLE = '<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 assembled config data |
||
| 60 | */ |
||
| 61 | protected $data = [ |
||
| 62 | 'aliases' => [], |
||
| 63 | 'extensions' => [], |
||
| 64 | ]; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array raw collected data |
||
| 68 | */ |
||
| 69 | protected $raw = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array array of not yet merged params |
||
| 73 | */ |
||
| 74 | protected $rawParams = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var Composer instance |
||
| 78 | */ |
||
| 79 | protected $composer; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var IOInterface |
||
| 83 | */ |
||
| 84 | public $io; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Initializes the plugin object with the passed $composer and $io. |
||
| 88 | * @param Composer $composer |
||
| 89 | * @param IOInterface $io |
||
| 90 | 2 | */ |
|
| 91 | public function activate(Composer $composer, IOInterface $io) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns list of events the plugin is subscribed to. |
||
| 99 | * @return array list of events |
||
| 100 | 1 | */ |
|
| 101 | public static function getSubscribedEvents() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * This is the main function. |
||
| 112 | * @param Event $event |
||
| 113 | */ |
||
| 114 | public function onPostAutoloadDump(Event $event) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Scans the given package and collects extensions data. |
||
| 135 | * @param PackageInterface $package |
||
| 136 | */ |
||
| 137 | public function processPackage(PackageInterface $package) |
||
| 182 | |||
| 183 | public function assembleParams() |
||
| 187 | |||
| 188 | public function assembleConfigs() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Reads extra config. |
||
| 223 | * @param PackageInterface $__package |
||
| 224 | * @param string $__file |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | protected function readConfigFile(PackageInterface $__package, $__file) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Prepare aliases. |
||
| 249 | * |
||
| 250 | * @param PackageInterface $package |
||
| 251 | * @param string 'psr-0' or 'psr-4' |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Substitute path with alias if applicable. |
||
| 282 | * @param string $path |
||
| 283 | * @param string $dir |
||
| 284 | * @param string $alias |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function substitutePath($path, $dir, $alias) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Builds path inside of a package. |
||
| 294 | * @param PackageInterface $package |
||
| 295 | * @param mixed $path can be absolute or relative |
||
| 296 | * @return string absolute pathes will stay untouched |
||
| 297 | */ |
||
| 298 | public function preparePath(PackageInterface $package, $path) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get output dir. |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getOutputDir() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns full path to assembled config file. |
||
| 322 | * @param string $filename name of config |
||
| 323 | * @return string absolute path |
||
| 324 | */ |
||
| 325 | public static function path($filename) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sets [[packages]]. |
||
| 332 | * @param PackageInterface[] $packages |
||
| 333 | */ |
||
| 334 | public function setPackages(array $packages) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets [[packages]]. |
||
| 341 | * @return \Composer\Package\PackageInterface[] |
||
| 342 | */ |
||
| 343 | public function getPackages() |
||
| 351 | 2 | ||
| 352 | /** |
||
| 353 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
| 354 | * The list is unordered (chaotic, can be different after every update). |
||
| 355 | */ |
||
| 356 | protected $plainList = []; |
||
| 357 | 1 | ||
| 358 | /** |
||
| 359 | 1 | * Ordered list of package. Order @see findPackages |
|
| 360 | */ |
||
| 361 | protected $orderedList = []; |
||
| 362 | |||
| 363 | 1 | /** |
|
| 364 | * Returns ordered list of packages: |
||
| 365 | * - listed earlier in the composer.json will get earlier in the list |
||
| 366 | * - childs before parents. |
||
| 367 | * @return \Composer\Package\PackageInterface[] |
||
| 368 | */ |
||
| 369 | public function findPackages() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Iterates through package dependencies. |
||
| 393 | * @param PackageInterface $package to iterate |
||
| 394 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 395 | */ |
||
| 396 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Iterates dependencies of the given package. |
||
| 419 | * @param PackageInterface $package |
||
| 420 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 421 | */ |
||
| 422 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get absolute path to package base dir. |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function getBaseDir() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get absolute path to composer vendor dir. |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function getVendorDir() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Getter for filesystem utility. |
||
| 468 | * @return Filesystem |
||
| 469 | */ |
||
| 470 | public function getFilesystem() |
||
| 478 | } |
||
| 479 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.