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 = 'extension-plugin'; |
||
| 33 | const OUTPUT_PATH = 'hiqdev'; |
||
| 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 whole data |
||
| 59 | */ |
||
| 60 | protected $data = [ |
||
| 61 | 'aliases' => [], |
||
| 62 | 'extensions' => [], |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Composer instance |
||
| 67 | */ |
||
| 68 | protected $composer; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var IOInterface |
||
| 72 | */ |
||
| 73 | public $io; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initializes the plugin object with the passed $composer and $io. |
||
| 77 | * @param Composer $composer |
||
| 78 | * @param IOInterface $io |
||
| 79 | */ |
||
| 80 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
| 81 | { |
||
| 82 | 2 | $this->composer = $composer; |
|
| 83 | 2 | $this->io = $io; |
|
| 84 | 2 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Returns list of events the plugin is subscribed to. |
||
| 88 | * @return array list of events |
||
| 89 | */ |
||
| 90 | 1 | public static function getSubscribedEvents() |
|
| 91 | { |
||
| 92 | return [ |
||
| 93 | 1 | ScriptEvents::POST_AUTOLOAD_DUMP => [ |
|
| 94 | 1 | ['onPostAutoloadDump', 0], |
|
| 95 | 1 | ], |
|
| 96 | 1 | ]; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Simply rewrites extensions file from scratch. |
||
| 101 | * @param Event $event |
||
| 102 | */ |
||
| 103 | public function onPostAutoloadDump(Event $event) |
||
|
|
|||
| 104 | { |
||
| 105 | $this->io->writeError('<info>Generating extensions files</info>'); |
||
| 106 | $this->processPackage($this->composer->getPackage()); |
||
| 107 | foreach ($this->getPackages() as $package) { |
||
| 108 | if ($package instanceof \Composer\Package\CompletePackageInterface) { |
||
| 109 | $this->processPackage($package); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | foreach ($this->data as $name => $data) { |
||
| 114 | $this->saveFile($this->buildOutputPath($name), $data); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | public function buildOutputPath($name) |
||
| 119 | { |
||
| 120 | return static::OUTPUT_PATH . DIRECTORY_SEPARATOR . $name . '.php'; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Writes file. |
||
| 125 | * @param string $file |
||
| 126 | * @param array $data |
||
| 127 | */ |
||
| 128 | protected function saveFile($file, array $data) |
||
| 129 | { |
||
| 130 | $path = $this->getVendorDir() . '/' . $file; |
||
| 131 | if (!file_exists(dirname($path))) { |
||
| 132 | mkdir(dirname($path), 0777, true); |
||
| 133 | } |
||
| 134 | $array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', var_export($data, true)); |
||
| 135 | file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(__DIR__));\n\nreturn $array;\n"); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Scans the given package and collects extensions data. |
||
| 140 | * @param PackageInterface $package |
||
| 141 | */ |
||
| 142 | public function processPackage(PackageInterface $package) |
||
| 143 | { |
||
| 144 | $extra = $package->getExtra(); |
||
| 145 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
| 146 | if ($package->getType() !== self::PACKAGE_TYPE && is_null($files)) { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | $extension = [ |
||
| 151 | 'name' => $package->getName(), |
||
| 152 | 'version' => $package->getVersion(), |
||
| 153 | ]; |
||
| 154 | if ($package->getVersion() === '9999999-dev') { |
||
| 155 | $reference = $package->getSourceReference() ?: $package->getDistReference(); |
||
| 156 | if ($reference) { |
||
| 157 | $extension['reference'] = $reference; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | $this->data['extensions'][$package->getName()] = $extension; |
||
| 161 | |||
| 162 | $aliases = array_merge( |
||
| 163 | $this->prepareAliases($package, 'psr-0'), |
||
| 164 | $this->prepareAliases($package, 'psr-4') |
||
| 165 | ); |
||
| 166 | $this->data['aliases'] = array_merge($this->data['aliases'], $aliases); |
||
| 167 | foreach ((array) $files as $name => $path) { |
||
| 168 | $config = $this->readExtensionConfig($package, $path); |
||
| 169 | $config['aliases'] = array_merge( |
||
| 170 | $aliases, |
||
| 171 | isset($config['aliases']) ? (array) $config['aliases'] : [] |
||
| 172 | ); |
||
| 173 | $this->data['aliases'] = array_merge($this->data['aliases'], $config['aliases']); |
||
| 174 | $this->data[$name] = isset($this->data[$name]) ? static::mergeConfig($this->data[$name], $config) : $config; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Merges two or more arrays into one recursively. |
||
| 180 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
| 181 | * @param array $a array to be merged to |
||
| 182 | * @param array $b array to be merged from |
||
| 183 | * @return array the merged array |
||
| 184 | */ |
||
| 185 | public static function mergeConfig($a, $b) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Read extra config. |
||
| 213 | * @param string $file |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | protected function readExtensionConfig(PackageInterface $package, $file) |
||
| 217 | { |
||
| 218 | $path = $this->preparePath($package, $file); |
||
| 219 | if (!file_exists($path)) { |
||
| 220 | $this->io->writeError('<error>Non existent extension config file</error> ' . $file . ' in ' . $package->getName()); |
||
| 221 | exit(1); |
||
| 222 | } |
||
| 223 | return require $path; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Prepare aliases. |
||
| 228 | * |
||
| 229 | * @param PackageInterface $package |
||
| 230 | * @param string 'psr-0' or 'psr-4' |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Substitute path with alias if applicable. |
||
| 261 | * @param string $path |
||
| 262 | * @param string $dir |
||
| 263 | * @param string $alias |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function substitutePath($path, $dir, $alias) |
||
| 270 | |||
| 271 | public function preparePath(PackageInterface $package, $path) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Sets [[packages]]. |
||
| 283 | * @param PackageInterface[] $packages |
||
| 284 | */ |
||
| 285 | 2 | public function setPackages(array $packages) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Gets [[packages]]. |
||
| 292 | * @return \Composer\Package\PackageInterface[] |
||
| 293 | */ |
||
| 294 | 1 | public function getPackages() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get absolute path to package base dir. |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function getBaseDir() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get absolute path to composer vendor dir. |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function getVendorDir() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Getter for filesystem utility. |
||
| 332 | * @return Filesystem |
||
| 333 | */ |
||
| 334 | public function getFilesystem() |
||
| 342 | } |
||
| 343 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.