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 YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
| 33 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var PackageInterface[] the array of active composer packages |
||
| 37 | */ |
||
| 38 | protected $packages; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string absolute path to the package base directory |
||
| 42 | */ |
||
| 43 | protected $baseDir; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string absolute path to vendor directory |
||
| 47 | */ |
||
| 48 | protected $vendorDir; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Filesystem utility |
||
| 52 | */ |
||
| 53 | protected $filesystem; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array config name => list of files |
||
| 57 | */ |
||
| 58 | protected $files = [ |
||
| 59 | 'dotenv' => [], |
||
| 60 | 'defines' => [], |
||
| 61 | 'params' => [], |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array package name => configs as listed in `composer.json` |
||
| 66 | */ |
||
| 67 | protected $originalFiles = []; |
||
| 68 | |||
| 69 | protected $aliases = []; |
||
| 70 | |||
| 71 | protected $extensions = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array array of not yet merged params |
||
| 75 | */ |
||
| 76 | protected $rawParams = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Composer instance |
||
| 80 | */ |
||
| 81 | protected $composer; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var IOInterface |
||
| 85 | */ |
||
| 86 | public $io; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Initializes the plugin object with the passed $composer and $io. |
||
| 90 | * @param Composer $composer |
||
| 91 | * @param IOInterface $io |
||
| 92 | 2 | */ |
|
| 93 | public function activate(Composer $composer, IOInterface $io) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns list of events the plugin is subscribed to. |
||
| 101 | * @return array list of events |
||
| 102 | 1 | */ |
|
| 103 | public static function getSubscribedEvents() |
||
| 104 | { |
||
| 105 | 1 | return [ |
|
| 106 | 1 | ScriptEvents::POST_AUTOLOAD_DUMP => [ |
|
| 107 | 1 | ['onPostAutoloadDump', 0], |
|
| 108 | 1 | ], |
|
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * This is the main function. |
||
| 114 | * @param Event $event |
||
| 115 | */ |
||
| 116 | public function onPostAutoloadDump(Event $event) |
||
| 131 | |||
| 132 | protected function initAutoload() |
||
| 136 | |||
| 137 | protected function scanPackages() |
||
| 138 | { |
||
| 139 | foreach ($this->getPackages() as $package) { |
||
| 140 | if ($package instanceof CompletePackageInterface) { |
||
| 141 | $this->processPackage($package); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Scans the given package and collects extensions data. |
||
| 148 | * @param PackageInterface $package |
||
| 149 | */ |
||
| 150 | protected function processPackage(CompletePackageInterface $package) |
||
| 151 | { |
||
| 152 | $extra = $package->getExtra(); |
||
| 153 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
| 154 | $this->originalFiles[$package->getPrettyName()] = $files; |
||
| 155 | |||
| 156 | if ($package->getType() !== self::YII2_PACKAGE_TYPE && is_null($files)) { |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (is_array($files)) { |
||
| 161 | $this->addFiles($package, $files); |
||
| 162 | } |
||
| 163 | if ($package instanceof RootPackageInterface) { |
||
| 164 | $this->loadDotEnv($package); |
||
| 165 | } |
||
| 166 | |||
| 167 | $aliases = $this->collectAliases($package); |
||
| 168 | $this->aliases = array_merge($this->aliases, $aliases); |
||
| 169 | |||
| 170 | $this->extensions[$package->getPrettyName()] = array_filter([ |
||
| 171 | 'name' => $package->getPrettyName(), |
||
| 172 | 'version' => $package->getVersion(), |
||
| 173 | 'reference' => $package->getSourceReference() ?: $package->getDistReference(), |
||
| 174 | 'aliases' => $aliases, |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function loadDotEnv(RootPackageInterface $package) |
||
| 179 | { |
||
| 180 | $path = $this->preparePath($package, '.env'); |
||
| 181 | if (file_exists($path) && class_exists('Dotenv\Dotenv')) { |
||
| 182 | array_push($this->files['dotenv'], $path); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Adds given files to the list of files to be processed. |
||
| 188 | * Prepares `defines` in reversed order (outer package first) because |
||
| 189 | * constants cannot be redefined. |
||
| 190 | * @param CompletePackageInterface $package |
||
| 191 | * @param array $files |
||
| 192 | */ |
||
| 193 | protected function addFiles(CompletePackageInterface $package, array $files) |
||
| 194 | { |
||
| 195 | foreach ($files as $name => $paths) { |
||
| 196 | $paths = (array) $paths; |
||
| 197 | if ($name === 'defines') { |
||
| 198 | $paths = array_reverse($paths); |
||
| 199 | } |
||
| 200 | foreach ($paths as $path) { |
||
| 201 | if (!isset($this->files[$name])) { |
||
| 202 | $this->files[$name] = []; |
||
| 203 | } |
||
| 204 | $path = $this->preparePath($package, $path); |
||
| 205 | if ($name === 'defines') { |
||
| 206 | array_unshift($this->files[$name], $path); |
||
| 207 | } else { |
||
| 208 | array_push($this->files[$name], $path); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Collects package aliases. |
||
| 216 | * @param CompletePackageInterface $package |
||
| 217 | * @return array collected aliases |
||
| 218 | */ |
||
| 219 | protected function collectAliases(CompletePackageInterface $package) |
||
| 220 | { |
||
| 221 | $aliases = array_merge( |
||
| 222 | $this->prepareAliases($package, 'psr-0'), |
||
| 223 | $this->prepareAliases($package, 'psr-4') |
||
| 224 | ); |
||
| 225 | if ($package instanceof RootPackageInterface) { |
||
| 226 | $aliases = array_merge($aliases, |
||
| 227 | $this->prepareAliases($package, 'psr-0', true), |
||
| 228 | $this->prepareAliases($package, 'psr-4', true) |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $aliases; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Prepare aliases. |
||
| 237 | * @param PackageInterface $package |
||
| 238 | * @param string 'psr-0' or 'psr-4' |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
| 242 | { |
||
| 243 | $autoload = $dev ? $package->getDevAutoload() : $package->getAutoload(); |
||
| 244 | if (empty($autoload[$psr])) { |
||
| 245 | return []; |
||
| 246 | } |
||
| 247 | |||
| 248 | $aliases = []; |
||
| 249 | foreach ($autoload[$psr] as $name => $path) { |
||
| 250 | if (is_array($path)) { |
||
| 251 | // ignore psr-4 autoload specifications with multiple search paths |
||
| 252 | // we can not convert them into aliases as they are ambiguous |
||
| 253 | continue; |
||
| 254 | } |
||
| 255 | $name = str_replace('\\', '/', trim($name, '\\')); |
||
| 256 | $path = $this->preparePath($package, $path); |
||
| 257 | if ('psr-0' === $psr) { |
||
| 258 | $path .= '/' . $name; |
||
| 259 | } |
||
| 260 | $aliases["@$name"] = $path; |
||
| 261 | } |
||
| 262 | |||
| 263 | return $aliases; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Builds path inside of a package. |
||
| 268 | * @param PackageInterface $package |
||
| 269 | * @param mixed $path can be absolute or relative |
||
| 270 | * @return string absolute paths will stay untouched |
||
| 271 | */ |
||
| 272 | public function preparePath(PackageInterface $package, $path) |
||
| 273 | { |
||
| 274 | if (strncmp($path, '$', 1) === 0) { |
||
| 275 | return $path; |
||
| 276 | } |
||
| 277 | |||
| 278 | $skippable = strncmp($path, '?', 1) === 0 ? '?' : ''; |
||
| 279 | if ($skippable) { |
||
| 280 | $path = substr($path, 1); |
||
| 281 | } |
||
| 282 | |||
| 283 | if (!$this->getFilesystem()->isAbsolutePath($path)) { |
||
| 284 | $prefix = $package instanceof RootPackageInterface |
||
| 285 | ? $this->getBaseDir() |
||
| 286 | : $this->getVendorDir() . '/' . $package->getPrettyName(); |
||
| 287 | $path = $prefix . '/' . $path; |
||
| 288 | } |
||
| 289 | |||
| 290 | return $skippable . $this->getFilesystem()->normalizePath($path); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets [[packages]]. |
||
| 295 | * @param PackageInterface[] $packages |
||
| 296 | 2 | */ |
|
| 297 | public function setPackages(array $packages) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Gets [[packages]]. |
||
| 304 | * @return \Composer\Package\PackageInterface[] |
||
| 305 | 1 | */ |
|
| 306 | public function getPackages() |
||
| 307 | 1 | { |
|
| 308 | if ($this->packages === null) { |
||
| 309 | $this->packages = $this->findPackages(); |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
| 317 | * The list is unordered (chaotic, can be different after every update). |
||
| 318 | */ |
||
| 319 | protected $plainList = []; |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Ordered list of package in form: package => depth |
||
| 323 | * For order description @see findPackages. |
||
| 324 | */ |
||
| 325 | protected $orderedList = []; |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns ordered list of packages: |
||
| 329 | * - listed earlier in the composer.json will get earlier in the list |
||
| 330 | * - childs before parents. |
||
| 331 | * @return \Composer\Package\PackageInterface[] |
||
| 332 | */ |
||
| 333 | public function findPackages() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Iterates through package dependencies. |
||
| 353 | * @param PackageInterface $package to iterate |
||
| 354 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 355 | */ |
||
| 356 | protected function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Iterates dependencies of the given package. |
||
| 385 | * @param PackageInterface $package |
||
| 386 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 387 | */ |
||
| 388 | protected function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 404 | |||
| 405 | protected function showDepsTree() |
||
| 420 | |||
| 421 | protected $colors = ['red', 'green', 'yellow', 'cyan', 'magenta', 'blue']; |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get absolute path to package base dir. |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getBaseDir() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get absolute path to composer vendor dir. |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getVendorDir() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Getter for filesystem utility. |
||
| 452 | * @return Filesystem |
||
| 453 | */ |
||
| 454 | public function getFilesystem() |
||
| 462 | } |
||
| 463 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.