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) |
|
| 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() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Simply rewrites extensions file from scratch. |
||
| 101 | * @param Event $event |
||
| 102 | */ |
||
| 103 | public function onPostAutoloadDump(Event $event) |
||
| 117 | |||
| 118 | public function buildOutputPath($name) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Writes file. |
||
| 125 | * @param string $file |
||
| 126 | * @param array $data |
||
| 127 | */ |
||
| 128 | protected function saveFile($file, array $data) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Scans the given package and collects extensions data. |
||
| 140 | * @param PackageInterface $package |
||
| 141 | */ |
||
| 142 | public function processPackage(PackageInterface $package) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Read extra config. |
||
| 180 | * @param string $file |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function readExtensionConfig(PackageInterface $package, $file) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Prepare aliases. |
||
| 195 | * |
||
| 196 | * @param PackageInterface $package |
||
| 197 | * @param string 'psr-0' or 'psr-4' |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function prepareAliases(PackageInterface $package, $psr) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Substitute path with alias if applicable. |
||
| 228 | * @param string $path |
||
| 229 | * @param string $dir |
||
| 230 | * @param string $alias |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function substitutePath($path, $dir, $alias) |
||
| 237 | |||
| 238 | public function preparePath(PackageInterface $package, $path) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sets [[packages]]. |
||
| 250 | * @param PackageInterface[] $packages |
||
| 251 | */ |
||
| 252 | 2 | public function setPackages(array $packages) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Gets [[packages]]. |
||
| 259 | * @return \Composer\Package\PackageInterface[] |
||
| 260 | */ |
||
| 261 | 1 | public function getPackages() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get absolute path to package base dir. |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getBaseDir() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get absolute path to composer vendor dir. |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getVendorDir() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Getter for filesystem utility. |
||
| 299 | * @return Filesystem |
||
| 300 | */ |
||
| 301 | public function getFilesystem() |
||
| 309 | } |
||
| 310 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.