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 = 'extension-plugin'; |
||
33 | const OUTPUT_PATH = 'hiqdev'; |
||
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 whole data |
||
59 | */ |
||
60 | protected $data = [ |
||
61 | 'extensions' => [], |
||
62 | 'common' => [ |
||
63 | 'aliases' => [ |
||
64 | '@vendor' => self::VENDOR_DIR_SAMPLE, |
||
65 | ], |
||
66 | ], |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @var Composer instance |
||
71 | */ |
||
72 | protected $composer; |
||
73 | |||
74 | /** |
||
75 | * @var IOInterface |
||
76 | */ |
||
77 | public $io; |
||
78 | |||
79 | /** |
||
80 | * Initializes the plugin object with the passed $composer and $io. |
||
81 | * @param Composer $composer |
||
82 | * @param IOInterface $io |
||
83 | */ |
||
84 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
89 | |||
90 | /** |
||
91 | * Returns list of events the plugin is subscribed to. |
||
92 | * @return array list of events |
||
93 | */ |
||
94 | 1 | public static function getSubscribedEvents() |
|
102 | |||
103 | /** |
||
104 | * Simply rewrites extensions file from scratch. |
||
105 | * @param Event $event |
||
106 | */ |
||
107 | public function onPostAutoloadDump(Event $event) |
||
121 | |||
122 | public function buildOutputPath($name) |
||
126 | |||
127 | /** |
||
128 | * Writes file. |
||
129 | * @param string $file |
||
130 | * @param array $data |
||
131 | */ |
||
132 | protected function saveFile($file, array $data) |
||
141 | |||
142 | /** |
||
143 | * Scans the given package and collects extensions data. |
||
144 | * @param PackageInterface $package |
||
145 | */ |
||
146 | public function processPackage(PackageInterface $package) |
||
177 | |||
178 | /** |
||
179 | * Merges two or more arrays into one recursively. |
||
180 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
181 | * @param array $a array to be merged to |
||
182 | * @param array $b array to be merged from |
||
183 | * @return array the merged array |
||
184 | */ |
||
185 | public static function mergeConfig($a, $b) |
||
210 | |||
211 | /** |
||
212 | * Read extra config. |
||
213 | * @param string $file |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function readExtraConfig(PackageInterface $package, $file) |
||
225 | |||
226 | /** |
||
227 | * Prepare aliases. |
||
228 | * |
||
229 | * @param PackageInterface $package |
||
230 | * @param string 'psr-0' or 'psr-4' |
||
231 | * @return array |
||
232 | */ |
||
233 | protected function prepareAliases(PackageInterface $package, $psr) |
||
258 | |||
259 | /** |
||
260 | * Substitute path with alias if applicable. |
||
261 | * @param string $path |
||
262 | * @param string $dir |
||
263 | * @param string $alias |
||
264 | * @return string |
||
265 | */ |
||
266 | public function substitutePath($path, $dir, $alias) |
||
270 | |||
271 | public function preparePath(PackageInterface $package, $path) |
||
280 | |||
281 | /** |
||
282 | * Sets [[packages]]. |
||
283 | * @param PackageInterface[] $packages |
||
284 | */ |
||
285 | 2 | public function setPackages(array $packages) |
|
289 | |||
290 | /** |
||
291 | * Gets [[packages]]. |
||
292 | * @return \Composer\Package\PackageInterface[] |
||
293 | */ |
||
294 | 1 | public function getPackages() |
|
302 | |||
303 | /** |
||
304 | * Get absolute path to package base dir. |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getBaseDir() |
||
315 | |||
316 | /** |
||
317 | * Get absolute path to composer vendor dir. |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getVendorDir() |
||
329 | |||
330 | /** |
||
331 | * Getter for filesystem utility. |
||
332 | * @return Filesystem |
||
333 | */ |
||
334 | public function getFilesystem() |
||
342 | } |
||
343 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.