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 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var Composer instance |
||
66 | */ |
||
67 | protected $composer; |
||
68 | |||
69 | /** |
||
70 | * @var IOInterface |
||
71 | */ |
||
72 | public $io; |
||
73 | |||
74 | /** |
||
75 | * Initializes the plugin object with the passed $composer and $io. |
||
76 | * @param Composer $composer |
||
77 | * @param IOInterface $io |
||
78 | */ |
||
79 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
84 | |||
85 | /** |
||
86 | * Returns list of events the plugin is subscribed to. |
||
87 | * @return array list of events |
||
88 | */ |
||
89 | 1 | public static function getSubscribedEvents() |
|
97 | |||
98 | /** |
||
99 | * Simply rewrites extensions file from scratch. |
||
100 | * @param Event $event |
||
101 | */ |
||
102 | public function onPostAutoloadDump(Event $event) |
||
116 | |||
117 | public function buildOutputPath($name) |
||
121 | |||
122 | /** |
||
123 | * Writes file. |
||
124 | * @param string $file |
||
125 | * @param array $data |
||
126 | */ |
||
127 | protected function saveFile($file, array $data) |
||
136 | |||
137 | /** |
||
138 | * Scans the given package and collects extensions data. |
||
139 | * @param PackageInterface $package |
||
140 | */ |
||
141 | public function processPackage(PackageInterface $package) |
||
171 | |||
172 | /** |
||
173 | * Merges two or more arrays into one recursively. |
||
174 | * Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
||
175 | * @param array $a array to be merged to |
||
176 | * @param array $b array to be merged from |
||
177 | * @return array the merged array |
||
178 | */ |
||
179 | public static function mergeConfig($a, $b) |
||
204 | |||
205 | /** |
||
206 | * Read extra config. |
||
207 | * @param string $file |
||
208 | * @return array |
||
209 | */ |
||
210 | protected function readExtensionConfig(PackageInterface $package, $file) |
||
219 | |||
220 | /** |
||
221 | * Prepare aliases. |
||
222 | * |
||
223 | * @param PackageInterface $package |
||
224 | * @param string 'psr-0' or 'psr-4' |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function prepareAliases(PackageInterface $package, $psr) |
||
252 | |||
253 | /** |
||
254 | * Substitute path with alias if applicable. |
||
255 | * @param string $path |
||
256 | * @param string $dir |
||
257 | * @param string $alias |
||
258 | * @return string |
||
259 | */ |
||
260 | public function substitutePath($path, $dir, $alias) |
||
264 | |||
265 | public function preparePath(PackageInterface $package, $path) |
||
274 | |||
275 | /** |
||
276 | * Sets [[packages]]. |
||
277 | * @param PackageInterface[] $packages |
||
278 | */ |
||
279 | 2 | public function setPackages(array $packages) |
|
283 | |||
284 | /** |
||
285 | * Gets [[packages]]. |
||
286 | * @return \Composer\Package\PackageInterface[] |
||
287 | */ |
||
288 | 1 | public function getPackages() |
|
296 | |||
297 | /** |
||
298 | * Get absolute path to package base dir. |
||
299 | * @return string |
||
300 | */ |
||
301 | public function getBaseDir() |
||
309 | |||
310 | /** |
||
311 | * Get absolute path to composer vendor dir. |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getVendorDir() |
||
323 | |||
324 | /** |
||
325 | * Getter for filesystem utility. |
||
326 | * @return Filesystem |
||
327 | */ |
||
328 | public function getFilesystem() |
||
336 | } |
||
337 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.