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 YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
| 32 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var PackageInterface[] the array of active composer packages |
||
| 36 | */ |
||
| 37 | protected $packages; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string absolute path to the package base directory |
||
| 41 | */ |
||
| 42 | protected $baseDir; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string absolute path to vendor directory |
||
| 46 | */ |
||
| 47 | protected $vendorDir; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Filesystem utility |
||
| 51 | */ |
||
| 52 | protected $filesystem; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array config name => list of files |
||
| 56 | */ |
||
| 57 | protected $files = [ |
||
| 58 | 'dotenv' => [], |
||
| 59 | 'defines' => [], |
||
| 60 | 'params' => [], |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array package name => configs as listed in `composer.json` |
||
| 65 | */ |
||
| 66 | protected $originalFiles = []; |
||
| 67 | |||
| 68 | protected $aliases = []; |
||
| 69 | |||
| 70 | protected $extensions = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array array of not yet merged params |
||
| 74 | */ |
||
| 75 | protected $rawParams = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Composer instance |
||
| 79 | */ |
||
| 80 | protected $composer; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var IOInterface |
||
| 84 | */ |
||
| 85 | public $io; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Initializes the plugin object with the passed $composer and $io. |
||
| 89 | * @param Composer $composer |
||
| 90 | * @param IOInterface $io |
||
| 91 | */ |
||
| 92 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Returns list of events the plugin is subscribed to. |
||
| 100 | * @return array list of events |
||
| 101 | */ |
||
| 102 | 1 | public static function getSubscribedEvents() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * This is the main function. |
||
| 113 | * @param Event $event |
||
| 114 | */ |
||
| 115 | public function onPostAutoloadDump(Event $event) |
||
| 130 | |||
| 131 | protected function initAutoload() |
||
| 142 | |||
| 143 | protected function scanPackages() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Scans the given package and collects extensions data. |
||
| 154 | * @param PackageInterface $package |
||
| 155 | */ |
||
| 156 | protected function processPackage(CompletePackageInterface $package) |
||
| 183 | |||
| 184 | protected function getExtra(CompletePackageInterface $package) |
||
| 190 | |||
| 191 | protected function loadDotEnv(RootPackageInterface $package) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Adds given files to the list of files to be processed. |
||
| 201 | * Prepares `defines` in reversed order (outer package first) because |
||
| 202 | * constants cannot be redefined. |
||
| 203 | * @param CompletePackageInterface $package |
||
| 204 | * @param array $files |
||
| 205 | */ |
||
| 206 | protected function addFiles(CompletePackageInterface $package, array $files) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Collects package aliases. |
||
| 229 | * @param CompletePackageInterface $package |
||
| 230 | * @return array collected aliases |
||
| 231 | */ |
||
| 232 | protected function collectAliases(CompletePackageInterface $package) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Prepare aliases. |
||
| 250 | * @param PackageInterface $package |
||
| 251 | * @param string 'psr-0' or 'psr-4' |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Builds path inside of a package. |
||
| 281 | * @param PackageInterface $package |
||
| 282 | * @param mixed $path can be absolute or relative |
||
| 283 | * @return string absolute paths will stay untouched |
||
| 284 | */ |
||
| 285 | public function preparePath(PackageInterface $package, $path) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sets [[packages]]. |
||
| 308 | * @param PackageInterface[] $packages |
||
| 309 | */ |
||
| 310 | 2 | public function setPackages(array $packages) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Gets [[packages]]. |
||
| 317 | * @return \Composer\Package\PackageInterface[] |
||
| 318 | */ |
||
| 319 | 1 | public function getPackages() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
| 330 | * The list is unordered (chaotic, can be different after every update). |
||
| 331 | */ |
||
| 332 | protected $plainList = []; |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Ordered list of package in form: package => depth |
||
| 336 | * For order description @see findPackages. |
||
| 337 | */ |
||
| 338 | protected $orderedList = []; |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns ordered list of packages: |
||
| 342 | * - listed earlier in the composer.json will get earlier in the list |
||
| 343 | * - childs before parents. |
||
| 344 | * @return \Composer\Package\PackageInterface[] |
||
| 345 | */ |
||
| 346 | public function findPackages() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Iterates through package dependencies. |
||
| 366 | * @param PackageInterface $package to iterate |
||
| 367 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 368 | */ |
||
| 369 | protected function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Iterates dependencies of the given package. |
||
| 398 | * @param PackageInterface $package |
||
| 399 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 400 | */ |
||
| 401 | protected function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 417 | |||
| 418 | protected function showDepsTree() |
||
| 433 | |||
| 434 | protected $colors = ['red', 'green', 'yellow', 'cyan', 'magenta', 'blue']; |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get absolute path to package base dir. |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getBaseDir() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get absolute path to composer vendor dir. |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getVendorDir() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Getter for filesystem utility. |
||
| 465 | * @return Filesystem |
||
| 466 | */ |
||
| 467 | public function getFilesystem() |
||
| 475 | } |
||
| 476 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.