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) |
||
135 | { |
||
136 | $extra = $package->getExtra(); |
||
137 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
138 | if ($package->getType() !== self::PACKAGE_TYPE && is_null($files)) { |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | $extension = [ |
||
143 | 'name' => $package->getName(), |
||
144 | 'version' => $package->getVersion(), |
||
145 | ]; |
||
146 | if ($package->getVersion() === '9999999-dev') { |
||
147 | $reference = $package->getSourceReference() ?: $package->getDistReference(); |
||
148 | if ($reference) { |
||
149 | $extension['reference'] = $reference; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $aliases = array_merge( |
||
154 | $this->prepareAliases($package, 'psr-0'), |
||
155 | $this->prepareAliases($package, 'psr-4') |
||
156 | ); |
||
157 | |||
158 | if (isset($files['defines'])) { |
||
159 | foreach ((array) $files['defines'] as $file) { |
||
160 | $this->readConfigFile($package, $file); |
||
161 | } |
||
162 | unset($files['defines']); |
||
163 | } |
||
164 | |||
165 | if (isset($files['params'])) { |
||
166 | foreach ((array) $files['params'] as $file) { |
||
167 | $this->rawParams[] = $this->readConfigFile($package, $file); |
||
168 | } |
||
169 | unset($files['params']); |
||
170 | } |
||
171 | |||
172 | $this->raw[$package->getName()] = [ |
||
173 | 'package' => $package, |
||
174 | 'extension' => $extension, |
||
175 | 'aliases' => $aliases, |
||
176 | 'files' => (array) $files, |
||
177 | ]; |
||
178 | } |
||
179 | |||
180 | public function assembleParams() |
||
184 | |||
185 | public function assembleConfigs() |
||
186 | { |
||
187 | $allAliases = []; |
||
188 | $extensions = []; |
||
189 | $rawConfigs = [ |
||
190 | 'aliases' => [], |
||
191 | 'extensions' => [], |
||
192 | ]; |
||
193 | |||
194 | foreach ($this->raw as $name => $info) { |
||
195 | $rawConfigs['extensions'][] = [ |
||
196 | $name => $info['extension'], |
||
197 | ]; |
||
198 | |||
199 | $aliases = $info['aliases']; |
||
200 | $rawConfigs['aliases'][] = $aliases; |
||
201 | |||
202 | foreach ($info['files'] as $name => $pathes) { |
||
203 | foreach ((array) $pathes as $path) { |
||
204 | $rawConfigs[$name][] = $this->readConfigFile($info['package'], $path); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | |||
209 | foreach ($rawConfigs as $name => $configs) { |
||
210 | if (!in_array($name, ['params', 'aliases', 'extensions'], true)) { |
||
211 | $configs[] = [ |
||
212 | 'params' => $this->data['params'], |
||
213 | 'aliases' => $this->data['aliases'], |
||
214 | ]; |
||
215 | } |
||
216 | $this->assembleFile($name, $configs); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | protected function assembleFile($name, array $configs) |
||
225 | |||
226 | /** |
||
227 | * Read extra config. |
||
228 | * @param string $file |
||
229 | * @return array |
||
230 | */ |
||
231 | protected function readConfigFile(PackageInterface $package, $file) |
||
251 | |||
252 | /** |
||
253 | * Prepare aliases. |
||
254 | * |
||
255 | * @param PackageInterface $package |
||
256 | * @param string 'psr-0' or 'psr-4' |
||
257 | * @return array |
||
258 | */ |
||
259 | protected function prepareAliases(PackageInterface $package, $psr) |
||
284 | |||
285 | /** |
||
286 | * Substitute path with alias if applicable. |
||
287 | * @param string $path |
||
288 | * @param string $dir |
||
289 | * @param string $alias |
||
290 | * @return string |
||
291 | */ |
||
292 | public function substitutePath($path, $dir, $alias) |
||
296 | |||
297 | public function preparePath(PackageInterface $package, $path) |
||
306 | |||
307 | /** |
||
308 | * Get output dir. |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getOutputDir() |
||
315 | |||
316 | /** |
||
317 | * Build full path to write file for a given filename. |
||
318 | * @param string $filename |
||
319 | * @return string |
||
320 | */ |
||
321 | public function buildOutputPath($filename) |
||
325 | |||
326 | /** |
||
327 | * Writes config file. |
||
328 | * @param string $filename |
||
329 | * @param array $data |
||
330 | */ |
||
331 | protected function writeFile($filename, array $data) |
||
340 | |||
341 | /** |
||
342 | * Sets [[packages]]. |
||
343 | * @param PackageInterface[] $packages |
||
344 | */ |
||
345 | 2 | public function setPackages(array $packages) |
|
349 | |||
350 | /** |
||
351 | * Gets [[packages]]. |
||
352 | * @return \Composer\Package\PackageInterface[] |
||
353 | */ |
||
354 | 1 | public function getPackages() |
|
362 | |||
363 | protected $plainList = []; |
||
364 | protected $orderedList = []; |
||
365 | |||
366 | /** |
||
367 | * Returns ordered list of packages: |
||
368 | * - listed earlier in the composer.json will get earlier in the list |
||
369 | * - childs before parents. |
||
370 | * @return \Composer\Package\PackageInterface[] |
||
371 | */ |
||
372 | public function findPackages() |
||
388 | |||
389 | /** |
||
390 | * Iterates through package dependencies. |
||
391 | * @param PackageInterface $package to iterate |
||
392 | * @param bool $includingDev process development dependencies, defaults to not process |
||
393 | */ |
||
394 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
405 | |||
406 | /** |
||
407 | * Iterates given list of dependencies. |
||
408 | * @param array $links |
||
409 | */ |
||
410 | public function iterateLinks(array $links) |
||
419 | |||
420 | /** |
||
421 | * Get absolute path to package base dir. |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getBaseDir() |
||
432 | |||
433 | /** |
||
434 | * Get absolute path to composer vendor dir. |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getVendorDir() |
||
446 | |||
447 | /** |
||
448 | * Getter for filesystem utility. |
||
449 | * @return Filesystem |
||
450 | */ |
||
451 | public function getFilesystem() |
||
459 | } |
||
460 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.