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) |
||
129 | |||
130 | /** |
||
131 | * Scans the given package and collects extensions data. |
||
132 | * @param PackageInterface $package |
||
133 | */ |
||
134 | public function processPackage(PackageInterface $package) |
||
177 | |||
178 | public function assembleParams() |
||
182 | |||
183 | public function assembleConfigs() |
||
217 | |||
218 | protected function assembleFile($name, $configs) |
||
223 | |||
224 | /** |
||
225 | * Read extra config. |
||
226 | * @param string $file |
||
227 | * @return array |
||
228 | */ |
||
229 | protected function readConfigFile(PackageInterface $package, $file) |
||
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 | public function preparePath(PackageInterface $package, $path) |
||
303 | |||
304 | /** |
||
305 | * Get output dir. |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getOutputDir() |
||
312 | |||
313 | /** |
||
314 | * Build full path to write file for a given filename. |
||
315 | * @param string $filename |
||
316 | * @return string |
||
317 | */ |
||
318 | public function buildOutputPath($filename) |
||
322 | |||
323 | /** |
||
324 | * Writes file. |
||
325 | * @param string $filename |
||
326 | * @param array $data |
||
327 | */ |
||
328 | protected function writeFile($filename, array $data) |
||
337 | |||
338 | /** |
||
339 | * Sets [[packages]]. |
||
340 | * @param PackageInterface[] $packages |
||
341 | */ |
||
342 | 2 | public function setPackages(array $packages) |
|
346 | |||
347 | /** |
||
348 | * Gets [[packages]]. |
||
349 | * @return \Composer\Package\PackageInterface[] |
||
350 | */ |
||
351 | 1 | public function getPackages() |
|
359 | |||
360 | /** |
||
361 | * Get absolute path to package base dir. |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getBaseDir() |
||
372 | |||
373 | /** |
||
374 | * Get absolute path to composer vendor dir. |
||
375 | * @return string |
||
376 | */ |
||
377 | public function getVendorDir() |
||
386 | |||
387 | /** |
||
388 | * Getter for filesystem utility. |
||
389 | * @return Filesystem |
||
390 | */ |
||
391 | public function getFilesystem() |
||
399 | } |
||
400 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.