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 | 'defines' => [], |
||
60 | 'params' => [], |
||
61 | ]; |
||
62 | |||
63 | protected $aliases = []; |
||
64 | |||
65 | protected $extensions = []; |
||
66 | |||
67 | /** |
||
68 | * @var array array of not yet merged params |
||
69 | */ |
||
70 | protected $rawParams = []; |
||
71 | |||
72 | /** |
||
73 | * @var Composer instance |
||
74 | */ |
||
75 | protected $composer; |
||
76 | |||
77 | /** |
||
78 | * @var IOInterface |
||
79 | */ |
||
80 | public $io; |
||
81 | |||
82 | /** |
||
83 | * Initializes the plugin object with the passed $composer and $io. |
||
84 | * @param Composer $composer |
||
85 | * @param IOInterface $io |
||
86 | */ |
||
87 | public function activate(Composer $composer, IOInterface $io) |
||
92 | 2 | ||
93 | 2 | /** |
|
94 | 2 | * Returns list of events the plugin is subscribed to. |
|
95 | * @return array list of events |
||
96 | */ |
||
97 | public static function getSubscribedEvents() |
||
105 | 1 | ||
106 | 1 | /** |
|
107 | * This is the main function. |
||
108 | * @param Event $event |
||
109 | */ |
||
110 | public function onPostAutoloadDump(Event $event) |
||
123 | |||
124 | protected function scanPackages() |
||
132 | |||
133 | /** |
||
134 | * Scans the given package and collects extensions data. |
||
135 | * @param PackageInterface $package |
||
136 | */ |
||
137 | protected function processPackage(CompletePackageInterface $package) |
||
163 | |||
164 | protected function processFiles(CompletePackageInterface $package, array $files) |
||
175 | |||
176 | /** |
||
177 | * Prepare aliases. |
||
178 | * @param PackageInterface $package |
||
179 | * @param string 'psr-0' or 'psr-4' |
||
180 | * @return array |
||
181 | */ |
||
182 | protected function prepareAliases(PackageInterface $package, $psr) |
||
206 | |||
207 | /** |
||
208 | * Builds path inside of a package. |
||
209 | * @param PackageInterface $package |
||
210 | * @param mixed $path can be absolute or relative |
||
211 | * @return string absolute pathes will stay untouched |
||
212 | */ |
||
213 | public function preparePath(PackageInterface $package, $path) |
||
230 | |||
231 | /** |
||
232 | * Sets [[packages]]. |
||
233 | * @param PackageInterface[] $packages |
||
234 | */ |
||
235 | public function setPackages(array $packages) |
||
239 | |||
240 | /** |
||
241 | * Gets [[packages]]. |
||
242 | * @return \Composer\Package\PackageInterface[] |
||
243 | */ |
||
244 | public function getPackages() |
||
252 | |||
253 | /** |
||
254 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
255 | * The list is unordered (chaotic, can be different after every update). |
||
256 | */ |
||
257 | protected $plainList = []; |
||
258 | |||
259 | /** |
||
260 | * Ordered list of package. Order @see findPackages |
||
261 | */ |
||
262 | protected $orderedList = []; |
||
263 | |||
264 | /** |
||
265 | * Returns ordered list of packages: |
||
266 | * - listed earlier in the composer.json will get earlier in the list |
||
267 | * - childs before parents. |
||
268 | * @return \Composer\Package\PackageInterface[] |
||
269 | */ |
||
270 | public function findPackages() |
||
292 | |||
293 | /** |
||
294 | * Iterates through package dependencies. |
||
295 | * @param PackageInterface $package to iterate |
||
296 | * @param bool $includingDev process development dependencies, defaults to not process |
||
297 | */ |
||
298 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
318 | |||
319 | /** |
||
320 | * Iterates dependencies of the given package. |
||
321 | * @param PackageInterface $package |
||
322 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
323 | */ |
||
324 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
340 | |||
341 | /** |
||
342 | * Get absolute path to package base dir. |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getBaseDir() |
||
353 | |||
354 | /** |
||
355 | * Get absolute path to composer vendor dir. |
||
356 | * @return string |
||
357 | 1 | */ |
|
358 | public function getVendorDir() |
||
367 | |||
368 | /** |
||
369 | * Getter for filesystem utility. |
||
370 | * @return Filesystem |
||
371 | */ |
||
372 | public function getFilesystem() |
||
380 | } |
||
381 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.