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 YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
32 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
33 | |||
34 | /** |
||
35 | * @var PackageInterface[] the array of active composer packages |
||
36 | */ |
||
37 | protected $packages; |
||
38 | |||
39 | /** |
||
40 | * @var string absolute path to the package base directory |
||
41 | */ |
||
42 | protected $baseDir; |
||
43 | |||
44 | /** |
||
45 | * @var string absolute path to vendor directory |
||
46 | */ |
||
47 | protected $vendorDir; |
||
48 | |||
49 | /** |
||
50 | * @var Filesystem utility |
||
51 | */ |
||
52 | protected $filesystem; |
||
53 | |||
54 | /** |
||
55 | * @var array config name => list of files |
||
56 | */ |
||
57 | protected $files = [ |
||
58 | 'defines' => [], |
||
59 | 'params' => [], |
||
60 | ]; |
||
61 | |||
62 | protected $aliases = []; |
||
63 | |||
64 | protected $extensions = []; |
||
65 | |||
66 | /** |
||
67 | * @var array array of not yet merged params |
||
68 | */ |
||
69 | protected $rawParams = []; |
||
70 | |||
71 | /** |
||
72 | * @var Composer instance |
||
73 | */ |
||
74 | protected $composer; |
||
75 | |||
76 | /** |
||
77 | * @var IOInterface |
||
78 | */ |
||
79 | public $io; |
||
80 | |||
81 | /** |
||
82 | * Initializes the plugin object with the passed $composer and $io. |
||
83 | * @param Composer $composer |
||
84 | * @param IOInterface $io |
||
85 | */ |
||
86 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
91 | |||
92 | /** |
||
93 | * Returns list of events the plugin is subscribed to. |
||
94 | * @return array list of events |
||
95 | */ |
||
96 | 1 | public static function getSubscribedEvents() |
|
104 | |||
105 | /** |
||
106 | * This is the main function. |
||
107 | * @param Event $event |
||
108 | */ |
||
109 | public function onPostAutoloadDump(Event $event) |
||
123 | |||
124 | protected function initAutoload() |
||
128 | |||
129 | protected function scanPackages() |
||
137 | |||
138 | /** |
||
139 | * Scans the given package and collects extensions data. |
||
140 | * @param PackageInterface $package |
||
141 | */ |
||
142 | protected function processPackage(CompletePackageInterface $package) |
||
165 | |||
166 | protected function processFiles(CompletePackageInterface $package, array $files) |
||
177 | |||
178 | /** |
||
179 | * Collects package aliases. |
||
180 | * @param CompletePackageInterface $package |
||
181 | * @return array collected aliases |
||
182 | */ |
||
183 | protected function collectAliases(CompletePackageInterface $package) |
||
198 | |||
199 | /** |
||
200 | * Prepare aliases. |
||
201 | * @param PackageInterface $package |
||
202 | * @param string 'psr-0' or 'psr-4' |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
229 | |||
230 | /** |
||
231 | * Builds path inside of a package. |
||
232 | * @param PackageInterface $package |
||
233 | * @param mixed $path can be absolute or relative |
||
234 | * @return string absolute paths will stay untouched |
||
235 | */ |
||
236 | public function preparePath(PackageInterface $package, $path) |
||
256 | |||
257 | 2 | /** |
|
258 | 2 | * Sets [[packages]]. |
|
259 | * @param PackageInterface[] $packages |
||
260 | */ |
||
261 | public function setPackages(array $packages) |
||
265 | |||
266 | 1 | /** |
|
267 | * Gets [[packages]]. |
||
268 | * @return \Composer\Package\PackageInterface[] |
||
269 | */ |
||
270 | 1 | public function getPackages() |
|
278 | |||
279 | /** |
||
280 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
281 | * The list is unordered (chaotic, can be different after every update). |
||
282 | */ |
||
283 | protected $plainList = []; |
||
284 | |||
285 | /** |
||
286 | * Ordered list of package. Order @see findPackages. |
||
287 | */ |
||
288 | protected $orderedList = []; |
||
289 | |||
290 | /** |
||
291 | * Returns ordered list of packages: |
||
292 | * - listed earlier in the composer.json will get earlier in the list |
||
293 | * - childs before parents. |
||
294 | * @return \Composer\Package\PackageInterface[] |
||
295 | */ |
||
296 | public function findPackages() |
||
318 | |||
319 | /** |
||
320 | * Iterates through package dependencies. |
||
321 | * @param PackageInterface $package to iterate |
||
322 | * @param bool $includingDev process development dependencies, defaults to not process |
||
323 | */ |
||
324 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
344 | |||
345 | /** |
||
346 | * Iterates dependencies of the given package. |
||
347 | * @param PackageInterface $package |
||
348 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
349 | */ |
||
350 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
366 | |||
367 | /** |
||
368 | * Get absolute path to package base dir. |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getBaseDir() |
||
379 | |||
380 | /** |
||
381 | * Get absolute path to composer vendor dir. |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getVendorDir() |
||
393 | |||
394 | /** |
||
395 | * Getter for filesystem utility. |
||
396 | * @return Filesystem |
||
397 | */ |
||
398 | public function getFilesystem() |
||
406 | } |
||
407 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.