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 YII2_EXTENSIONS_FILE = 'yiisoft/extensions.php'; |
||
| 34 | const OUTPUT_PATH = 'hiqdev'; |
||
| 35 | const BASE_DIR_SAMPLE = '<base-dir>'; |
||
| 36 | const VENDOR_DIR_SAMPLE = '<base-dir>/vendor'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var PackageInterface[] the array of active composer packages |
||
| 40 | */ |
||
| 41 | protected $packages; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string absolute path to the package base directory. |
||
| 45 | */ |
||
| 46 | protected $baseDir; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string absolute path to vendor directory. |
||
| 50 | */ |
||
| 51 | protected $vendorDir; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Filesystem utility |
||
| 55 | */ |
||
| 56 | protected $filesystem; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array whole data |
||
| 60 | */ |
||
| 61 | protected $data = [ |
||
| 62 | 'extensions' => [], |
||
| 63 | 'aliases' => [ |
||
| 64 | '@vendor' => self::VENDOR_DIR_SAMPLE, |
||
| 65 | ], |
||
| 66 | ]; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Composer instance |
||
| 70 | */ |
||
| 71 | protected $composer; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var IOInterface |
||
| 75 | */ |
||
| 76 | public $io; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initializes the plugin object with the passed $composer and $io. |
||
| 80 | * @param Composer $composer |
||
| 81 | * @param IOInterface $io |
||
| 82 | */ |
||
| 83 | public function activate(Composer $composer, IOInterface $io) |
||
| 88 | 2 | ||
| 89 | /** |
||
| 90 | 2 | * Returns list of events the plugin is subscribed to. |
|
| 91 | 2 | * @return array list of events |
|
| 92 | 2 | */ |
|
| 93 | public static function getSubscribedEvents() |
||
| 101 | 1 | ||
| 102 | 1 | /** |
|
| 103 | 1 | * Simply rewrites extensions file from scratch. |
|
| 104 | 1 | * @param Event $event |
|
| 105 | */ |
||
| 106 | public function onPostAutoloadDump(Event $event) |
||
| 122 | |||
| 123 | public function buildOutputPath($name) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Writes file. |
||
| 130 | * @param string $file |
||
| 131 | * @param array $data |
||
| 132 | */ |
||
| 133 | protected function saveFile($file, array $data) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Scans the given package and collects extensions data. |
||
| 145 | * @param PackageInterface $package |
||
| 146 | */ |
||
| 147 | public function processPackage(PackageInterface $package) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Merges two or more arrays into one recursively. |
||
| 181 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
| 182 | * @param array $a array to be merged to |
||
| 183 | * @param array $b array to be merged from |
||
| 184 | * @return array the merged array |
||
| 185 | */ |
||
| 186 | public static function mergeConfig($a, $b) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Read extra config. |
||
| 214 | * @param string $file |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected function readExtraConfig(PackageInterface $package, $file) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Prepare aliases. |
||
| 229 | * |
||
| 230 | * @param PackageInterface $package |
||
| 231 | * @param string 'psr-0' or 'psr-4' |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Substitute path with alias if applicable. |
||
| 262 | * @param string $path |
||
| 263 | * @param string $dir |
||
| 264 | * @param string $alias |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function substitutePath($path, $dir, $alias) |
||
| 271 | |||
| 272 | public function preparePath(PackageInterface $package, $path) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Sets [[packages]]. |
||
| 284 | * @param PackageInterface[] $packages |
||
| 285 | */ |
||
| 286 | public function setPackages(array $packages) |
||
| 290 | |||
| 291 | 2 | /** |
|
| 292 | 2 | * Gets [[packages]]. |
|
| 293 | * @return \Composer\Package\PackageInterface[] |
||
| 294 | */ |
||
| 295 | public function getPackages() |
||
| 303 | |||
| 304 | 1 | /** |
|
| 305 | * Get absolute path to package base dir. |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getBaseDir() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get absolute path to composer vendor dir. |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getVendorDir() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Getter for filesystem utility. |
||
| 333 | * @return Filesystem |
||
| 334 | */ |
||
| 335 | public function getFilesystem() |
||
| 343 | } |
||
| 344 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.