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 |
||
| 30 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 31 | { |
||
| 32 | const OUTPUT_DIR = 'config'; |
||
| 33 | const YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
| 34 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var PackageInterface[] the array of active composer packages |
||
| 38 | */ |
||
| 39 | protected $packages; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string absolute path to the package base directory |
||
| 43 | */ |
||
| 44 | protected $baseDir; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string absolute path to vendor directory |
||
| 48 | */ |
||
| 49 | protected $vendorDir; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Filesystem utility |
||
| 53 | */ |
||
| 54 | protected $filesystem; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array config name => list of files |
||
| 58 | */ |
||
| 59 | protected $files = [ |
||
| 60 | 'defines' => [], |
||
| 61 | 'params' => [], |
||
| 62 | ]; |
||
| 63 | |||
| 64 | protected $aliases = []; |
||
| 65 | |||
| 66 | protected $extensions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array array of not yet merged params |
||
| 70 | */ |
||
| 71 | protected $rawParams = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Composer instance |
||
| 75 | */ |
||
| 76 | protected $composer; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var IOInterface |
||
| 80 | */ |
||
| 81 | public $io; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Initializes the plugin object with the passed $composer and $io. |
||
| 85 | * @param Composer $composer |
||
| 86 | * @param IOInterface $io |
||
| 87 | */ |
||
| 88 | public function activate(Composer $composer, IOInterface $io) |
||
| 93 | 2 | ||
| 94 | 2 | /** |
|
| 95 | * Returns list of events the plugin is subscribed to. |
||
| 96 | * @return array list of events |
||
| 97 | */ |
||
| 98 | public static function getSubscribedEvents() |
||
| 106 | 1 | ||
| 107 | /** |
||
| 108 | * This is the main function. |
||
| 109 | * @param Event $event |
||
| 110 | */ |
||
| 111 | public function onPostAutoloadDump(Event $event) |
||
| 123 | |||
| 124 | public static function rebuild() |
||
| 130 | |||
| 131 | protected function scanPackages() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Scans the given package and collects extensions data. |
||
| 142 | * @param PackageInterface $package |
||
| 143 | */ |
||
| 144 | protected function processPackage(PackageInterface $package) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Prepare aliases. |
||
| 177 | * @param PackageInterface $package |
||
| 178 | * @param string 'psr-0' or 'psr-4' |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Substitute path with alias if applicable. |
||
| 209 | * @param string $path |
||
| 210 | * @param string $dir |
||
| 211 | * @param string $alias |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected function substitutePath($path, $dir, $alias) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Builds path inside of a package. |
||
| 221 | * @param PackageInterface $package |
||
| 222 | * @param mixed $path can be absolute or relative |
||
| 223 | * @return string absolute pathes will stay untouched |
||
| 224 | */ |
||
| 225 | public function preparePath(PackageInterface $package, $path) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get output dir. |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public static function getOutputDir() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns full path to assembled config file. |
||
| 254 | * @param string $filename name of config |
||
| 255 | * @return string absolute path |
||
| 256 | */ |
||
| 257 | public static function path($filename) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets [[packages]]. |
||
| 264 | * @param PackageInterface[] $packages |
||
| 265 | */ |
||
| 266 | public function setPackages(array $packages) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Gets [[packages]]. |
||
| 273 | * @return \Composer\Package\PackageInterface[] |
||
| 274 | */ |
||
| 275 | public function getPackages() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
| 286 | * The list is unordered (chaotic, can be different after every update). |
||
| 287 | */ |
||
| 288 | protected $plainList = []; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Ordered list of package. Order @see findPackages |
||
| 292 | */ |
||
| 293 | protected $orderedList = []; |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns ordered list of packages: |
||
| 297 | * - listed earlier in the composer.json will get earlier in the list |
||
| 298 | * - childs before parents. |
||
| 299 | * @return \Composer\Package\PackageInterface[] |
||
| 300 | */ |
||
| 301 | public function findPackages() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Iterates through package dependencies. |
||
| 326 | * @param PackageInterface $package to iterate |
||
| 327 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 328 | */ |
||
| 329 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 349 | |||
| 350 | 2 | /** |
|
| 351 | 2 | * Iterates dependencies of the given package. |
|
| 352 | * @param PackageInterface $package |
||
| 353 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 354 | */ |
||
| 355 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get absolute path to package base dir. |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getBaseDir() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get absolute path to composer vendor dir. |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function getVendorDir() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Getter for filesystem utility. |
||
| 401 | * @return Filesystem |
||
| 402 | */ |
||
| 403 | public function getFilesystem() |
||
| 411 | } |
||
| 412 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.