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) |
||
|
|
|||
| 114 | { |
||
| 115 | $this->io->writeError('<info>Assembling config files</info>'); |
||
| 116 | |||
| 117 | /// scan packages |
||
| 118 | foreach ($this->getPackages() as $package) { |
||
| 119 | if ($package instanceof \Composer\Package\CompletePackageInterface) { |
||
| 120 | $this->processPackage($package); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | $this->processPackage($this->composer->getPackage()); |
||
| 124 | |||
| 125 | $this->assembleParams(); |
||
| 126 | $this->assembleConfigs(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Scans the given package and collects extensions data. |
||
| 131 | * @param PackageInterface $package |
||
| 132 | */ |
||
| 133 | public function processPackage(PackageInterface $package) |
||
| 134 | { |
||
| 135 | $extra = $package->getExtra(); |
||
| 136 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
| 137 | if ($package->getType() !== self::PACKAGE_TYPE && is_null($files)) { |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | $extension = [ |
||
| 142 | 'name' => $package->getPrettyName(), |
||
| 143 | 'version' => $package->getVersion(), |
||
| 144 | ]; |
||
| 145 | if ($package->getVersion() === '9999999-dev') { |
||
| 146 | $reference = $package->getSourceReference() ?: $package->getDistReference(); |
||
| 147 | if ($reference) { |
||
| 148 | $extension['reference'] = $reference; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $aliases = array_merge( |
||
| 153 | $this->prepareAliases($package, 'psr-0'), |
||
| 154 | $this->prepareAliases($package, 'psr-4') |
||
| 155 | ); |
||
| 156 | |||
| 157 | if (isset($files['defines'])) { |
||
| 158 | foreach ((array) $files['defines'] as $file) { |
||
| 159 | $this->readConfigFile($package, $file); |
||
| 160 | } |
||
| 161 | unset($files['defines']); |
||
| 162 | } |
||
| 163 | |||
| 164 | if (isset($files['params'])) { |
||
| 165 | foreach ((array) $files['params'] as $file) { |
||
| 166 | $this->rawParams[] = $this->readConfigFile($package, $file); |
||
| 167 | } |
||
| 168 | unset($files['params']); |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->raw[$package->getPrettyName()] = [ |
||
| 172 | 'package' => $package, |
||
| 173 | 'extension' => $extension, |
||
| 174 | 'aliases' => $aliases, |
||
| 175 | 'files' => (array) $files, |
||
| 176 | ]; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function assembleParams() |
||
| 180 | { |
||
| 181 | $this->assembleFile('params', $this->rawParams); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function assembleConfigs() |
||
| 185 | { |
||
| 186 | $rawConfigs = [ |
||
| 187 | 'aliases' => [], |
||
| 188 | 'extensions' => [], |
||
| 189 | ]; |
||
| 190 | |||
| 191 | foreach ($this->raw as $name => $info) { |
||
| 192 | $rawConfigs['extensions'][] = [ |
||
| 193 | $name => $info['extension'], |
||
| 194 | ]; |
||
| 195 | |||
| 196 | $aliases = $info['aliases']; |
||
| 197 | $rawConfigs['aliases'][] = $aliases; |
||
| 198 | |||
| 199 | foreach ($info['files'] as $name => $pathes) { |
||
| 200 | foreach ((array) $pathes as $path) { |
||
| 201 | $rawConfigs[$name][] = $this->readConfigFile($info['package'], $path); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | foreach ($rawConfigs as $name => $configs) { |
||
| 207 | if (!in_array($name, ['params', 'aliases', 'extensions'], true)) { |
||
| 208 | $configs[] = [ |
||
| 209 | 'params' => $this->data['params'], |
||
| 210 | 'aliases' => $this->data['aliases'], |
||
| 211 | ]; |
||
| 212 | } |
||
| 213 | $this->assembleFile($name, $configs); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | protected function assembleFile($name, array $configs) |
||
| 218 | { |
||
| 219 | $this->data[$name] = call_user_func_array([Helper::class, 'mergeConfig'], $configs); |
||
| 220 | $this->writeFile($name, (array) $this->data[$name]); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Reads extra config. |
||
| 225 | * @param PackageInterface $__package |
||
| 226 | * @param string $__file |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function readConfigFile(PackageInterface $__package, $__file) |
||
| 230 | { |
||
| 231 | $__skippable = false; |
||
| 232 | if (strncmp($__file, '?', 1) === 0) { |
||
| 233 | $__skippable = true; |
||
| 234 | $__file = substr($__file, 1); |
||
| 235 | } |
||
| 236 | $__path = $this->preparePath($__package, $__file); |
||
| 237 | if (!file_exists($__path)) { |
||
| 238 | if ($__skippable) { |
||
| 239 | return []; |
||
| 240 | } else { |
||
| 241 | $this->io->writeError('<error>Non existent config file</error> ' . $__file . ' in ' . $__package->getPrettyName()); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | extract($this->data); |
||
| 245 | |||
| 246 | return (array) require $__path; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Prepare aliases. |
||
| 251 | * |
||
| 252 | * @param PackageInterface $package |
||
| 253 | * @param string 'psr-0' or 'psr-4' |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Substitute path with alias if applicable. |
||
| 284 | * @param string $path |
||
| 285 | * @param string $dir |
||
| 286 | * @param string $alias |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function substitutePath($path, $dir, $alias) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Builds path inside of a package. |
||
| 296 | * @param PackageInterface $package |
||
| 297 | * @param mixed $path can be absolute or relative |
||
| 298 | * @return string absolute pathes will stay untouched |
||
| 299 | */ |
||
| 300 | public function preparePath(PackageInterface $package, $path) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get output dir. |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getOutputDir() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Build full path to write file for a given filename. |
||
| 321 | * @param string $filename |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function buildOutputPath($filename) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Writes config file. |
||
| 331 | * @param string $filename |
||
| 332 | * @param array $data |
||
| 333 | */ |
||
| 334 | protected function writeFile($filename, array $data) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Sets [[packages]]. |
||
| 346 | * @param PackageInterface[] $packages |
||
| 347 | */ |
||
| 348 | 2 | public function setPackages(array $packages) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Gets [[packages]]. |
||
| 355 | * @return \Composer\Package\PackageInterface[] |
||
| 356 | */ |
||
| 357 | 1 | public function getPackages() |
|
| 365 | |||
| 366 | protected $plainList = []; |
||
| 367 | protected $orderedList = []; |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns ordered list of packages: |
||
| 371 | * - listed earlier in the composer.json will get earlier in the list |
||
| 372 | * - childs before parents. |
||
| 373 | * @return \Composer\Package\PackageInterface[] |
||
| 374 | */ |
||
| 375 | public function findPackages() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Iterates through package dependencies. |
||
| 399 | * @param PackageInterface $package to iterate |
||
| 400 | * @param bool $includingDev process development dependencies, defaults to not process |
||
| 401 | */ |
||
| 402 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Iterates dependencies of the given package. |
||
| 416 | * @param PackageInterface $package |
||
| 417 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
| 418 | */ |
||
| 419 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
| 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.