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 |
||
30 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
31 | { |
||
32 | const YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
33 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
34 | |||
35 | /** |
||
36 | * @var PackageInterface[] the array of active composer packages |
||
37 | */ |
||
38 | protected $packages; |
||
39 | |||
40 | /** |
||
41 | * @var string absolute path to the package base directory |
||
42 | */ |
||
43 | protected $baseDir; |
||
44 | |||
45 | /** |
||
46 | * @var string absolute path to vendor directory |
||
47 | */ |
||
48 | protected $vendorDir; |
||
49 | |||
50 | /** |
||
51 | * @var Filesystem utility |
||
52 | */ |
||
53 | protected $filesystem; |
||
54 | |||
55 | /** |
||
56 | * @var array config name => list of files |
||
57 | */ |
||
58 | protected $files = [ |
||
59 | 'dotenv' => [], |
||
60 | 'defines' => [], |
||
61 | 'params' => [], |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var array package name => configs as listed in `composer.json` |
||
66 | */ |
||
67 | protected $originalFiles = []; |
||
68 | |||
69 | protected $aliases = []; |
||
70 | |||
71 | protected $extensions = []; |
||
72 | |||
73 | /** |
||
74 | * @var array array of not yet merged params |
||
75 | */ |
||
76 | protected $rawParams = []; |
||
77 | |||
78 | /** |
||
79 | * @var Composer instance |
||
80 | */ |
||
81 | protected $composer; |
||
82 | |||
83 | /** |
||
84 | * @var IOInterface |
||
85 | */ |
||
86 | public $io; |
||
87 | |||
88 | /** |
||
89 | * Initializes the plugin object with the passed $composer and $io. |
||
90 | * @param Composer $composer |
||
91 | * @param IOInterface $io |
||
92 | 2 | */ |
|
93 | public function activate(Composer $composer, IOInterface $io) |
||
98 | |||
99 | /** |
||
100 | * Returns list of events the plugin is subscribed to. |
||
101 | * @return array list of events |
||
102 | 1 | */ |
|
103 | public static function getSubscribedEvents() |
||
111 | |||
112 | /** |
||
113 | * This is the main function. |
||
114 | * @param Event $event |
||
115 | */ |
||
116 | public function onPostAutoloadDump(Event $event) |
||
131 | |||
132 | protected function initAutoload() |
||
136 | |||
137 | protected function scanPackages() |
||
145 | |||
146 | /** |
||
147 | * Scans the given package and collects extensions data. |
||
148 | * @param PackageInterface $package |
||
149 | */ |
||
150 | protected function processPackage(CompletePackageInterface $package) |
||
177 | |||
178 | protected function loadDotEnv(RootPackageInterface $package) |
||
185 | |||
186 | /** |
||
187 | * Adds given files to the list of files to be processed. |
||
188 | * Prepares `defines` in reversed order (outer package first) because |
||
189 | * constants cannot be redefined. |
||
190 | * @param CompletePackageInterface $package |
||
191 | * @param array $files |
||
192 | */ |
||
193 | protected function addFiles(CompletePackageInterface $package, array $files) |
||
213 | |||
214 | /** |
||
215 | * Collects package aliases. |
||
216 | * @param CompletePackageInterface $package |
||
217 | * @return array collected aliases |
||
218 | */ |
||
219 | protected function collectAliases(CompletePackageInterface $package) |
||
234 | |||
235 | /** |
||
236 | * Prepare aliases. |
||
237 | * @param PackageInterface $package |
||
238 | * @param string 'psr-0' or 'psr-4' |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
265 | |||
266 | /** |
||
267 | * Builds path inside of a package. |
||
268 | * @param PackageInterface $package |
||
269 | * @param mixed $path can be absolute or relative |
||
270 | * @return string absolute paths will stay untouched |
||
271 | */ |
||
272 | public function preparePath(PackageInterface $package, $path) |
||
292 | |||
293 | /** |
||
294 | * Sets [[packages]]. |
||
295 | * @param PackageInterface[] $packages |
||
296 | 2 | */ |
|
297 | public function setPackages(array $packages) |
||
301 | |||
302 | /** |
||
303 | * Gets [[packages]]. |
||
304 | * @return \Composer\Package\PackageInterface[] |
||
305 | 1 | */ |
|
306 | public function getPackages() |
||
314 | |||
315 | /** |
||
316 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
317 | * The list is unordered (chaotic, can be different after every update). |
||
318 | */ |
||
319 | protected $plainList = []; |
||
320 | |||
321 | /** |
||
322 | * Ordered list of package in form: package => depth |
||
323 | * For order description @see findPackages. |
||
324 | */ |
||
325 | protected $orderedList = []; |
||
326 | |||
327 | /** |
||
328 | * Returns ordered list of packages: |
||
329 | * - listed earlier in the composer.json will get earlier in the list |
||
330 | * - childs before parents. |
||
331 | * @return \Composer\Package\PackageInterface[] |
||
332 | */ |
||
333 | public function findPackages() |
||
350 | |||
351 | /** |
||
352 | * Iterates through package dependencies. |
||
353 | * @param PackageInterface $package to iterate |
||
354 | * @param bool $includingDev process development dependencies, defaults to not process |
||
355 | */ |
||
356 | protected function iteratePackage(PackageInterface $package, $includingDev = false) |
||
382 | |||
383 | /** |
||
384 | * Iterates dependencies of the given package. |
||
385 | * @param PackageInterface $package |
||
386 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
387 | */ |
||
388 | protected function iterateDependencies(PackageInterface $package, $dev = false) |
||
404 | |||
405 | protected function showDepsTree() |
||
420 | |||
421 | protected $colors = ['red', 'green', 'yellow', 'cyan', 'magenta', 'blue']; |
||
422 | |||
423 | /** |
||
424 | * Get absolute path to package base dir. |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getBaseDir() |
||
435 | |||
436 | /** |
||
437 | * Get absolute path to composer vendor dir. |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getVendorDir() |
||
449 | |||
450 | /** |
||
451 | * Getter for filesystem utility. |
||
452 | * @return Filesystem |
||
453 | */ |
||
454 | public function getFilesystem() |
||
462 | } |
||
463 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.