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) |
||
128 | |||
129 | /** |
||
130 | * Scans the given package and collects extensions data. |
||
131 | * @param PackageInterface $package |
||
132 | */ |
||
133 | public function processPackage(PackageInterface $package) |
||
178 | |||
179 | public function assembleParams() |
||
183 | |||
184 | public function assembleConfigs() |
||
216 | |||
217 | protected function assembleFile($name, array $configs) |
||
222 | |||
223 | /** |
||
224 | * Read extra config. |
||
225 | * @param string $file |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function readConfigFile(PackageInterface $__package, $__file) |
||
247 | |||
248 | /** |
||
249 | * Prepare aliases. |
||
250 | * |
||
251 | * @param PackageInterface $package |
||
252 | * @param string 'psr-0' or 'psr-4' |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function prepareAliases(PackageInterface $package, $psr) |
||
280 | |||
281 | /** |
||
282 | * Substitute path with alias if applicable. |
||
283 | * @param string $path |
||
284 | * @param string $dir |
||
285 | * @param string $alias |
||
286 | * @return string |
||
287 | */ |
||
288 | public function substitutePath($path, $dir, $alias) |
||
292 | |||
293 | /** |
||
294 | * Builds path inside of a package. |
||
295 | * @param PackageInterface $package |
||
296 | * @param mixed $path can be absolute or relative |
||
297 | * @return string absolute pathes will stay untouched |
||
298 | */ |
||
299 | public function preparePath(PackageInterface $package, $path) |
||
308 | |||
309 | /** |
||
310 | * Get output dir. |
||
311 | * @return string |
||
312 | */ |
||
313 | public function getOutputDir() |
||
317 | |||
318 | /** |
||
319 | * Build full path to write file for a given filename. |
||
320 | * @param string $filename |
||
321 | * @return string |
||
322 | */ |
||
323 | public function buildOutputPath($filename) |
||
327 | |||
328 | /** |
||
329 | * Writes config file. |
||
330 | * @param string $filename |
||
331 | * @param array $data |
||
332 | */ |
||
333 | protected function writeFile($filename, array $data) |
||
342 | |||
343 | /** |
||
344 | * Sets [[packages]]. |
||
345 | * @param PackageInterface[] $packages |
||
346 | */ |
||
347 | 2 | public function setPackages(array $packages) |
|
351 | |||
352 | /** |
||
353 | * Gets [[packages]]. |
||
354 | * @return \Composer\Package\PackageInterface[] |
||
355 | */ |
||
356 | 1 | public function getPackages() |
|
364 | |||
365 | protected $plainList = []; |
||
366 | protected $orderedList = []; |
||
367 | |||
368 | /** |
||
369 | * Returns ordered list of packages: |
||
370 | * - listed earlier in the composer.json will get earlier in the list |
||
371 | * - childs before parents. |
||
372 | * @return \Composer\Package\PackageInterface[] |
||
373 | */ |
||
374 | public function findPackages() |
||
391 | |||
392 | /** |
||
393 | * Iterates through package dependencies. |
||
394 | * @param PackageInterface $package to iterate |
||
395 | * @param bool $includingDev process development dependencies, defaults to not process |
||
396 | */ |
||
397 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
408 | |||
409 | /** |
||
410 | * Iterates dependencies of the given package. |
||
411 | * @param PackageInterface $package |
||
412 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
413 | */ |
||
414 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
430 | |||
431 | /** |
||
432 | * Get absolute path to package base dir. |
||
433 | * @return string |
||
434 | */ |
||
435 | public function getBaseDir() |
||
443 | |||
444 | /** |
||
445 | * Get absolute path to composer vendor dir. |
||
446 | * @return string |
||
447 | */ |
||
448 | public function getVendorDir() |
||
457 | |||
458 | /** |
||
459 | * Getter for filesystem utility. |
||
460 | * @return Filesystem |
||
461 | */ |
||
462 | public function getFilesystem() |
||
470 | } |
||
471 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.