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 |
||
32 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var string the filename of a lock file. Defaults to `composer-asset-plugin.lock` |
||
36 | */ |
||
37 | public $lockFile = 'composer-asset-plugin.lock'; |
||
38 | |||
39 | /** |
||
40 | * @var Composer instance |
||
41 | */ |
||
42 | protected $composer; |
||
43 | |||
44 | /** |
||
45 | * @var IOInterface |
||
46 | */ |
||
47 | public $io; |
||
48 | |||
49 | /** |
||
50 | * @var Pool |
||
51 | */ |
||
52 | protected $pool; |
||
53 | |||
54 | /** |
||
55 | * List of the available package managers/ |
||
56 | * Initialized at activate. |
||
57 | * @var array|PackageManager[] |
||
58 | * @see activate |
||
59 | */ |
||
60 | protected $managers = [ |
||
61 | 'bower' => 'hiqdev\composerassetplugin\Bower', |
||
62 | 'npm' => 'hiqdev\composerassetplugin\Npm', |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var PackageInterface[] the array of active composer packages |
||
67 | */ |
||
68 | protected $packages; |
||
69 | |||
70 | /** |
||
71 | * @var string absolute path to vendor directory. |
||
72 | */ |
||
73 | protected $vendorDir; |
||
74 | |||
75 | /** |
||
76 | * |
||
77 | */ |
||
78 | protected $requires = []; |
||
79 | |||
80 | /** |
||
81 | * Initializes the plugin object with the passed $composer and $io. |
||
82 | * Also initializes package managers. |
||
83 | * |
||
84 | * @param Composer $composer |
||
85 | * @param IOInterface $io |
||
86 | * @void |
||
87 | */ |
||
88 | 3 | public function activate(Composer $composer, IOInterface $io) |
|
103 | |||
104 | public function getComposer() |
||
108 | |||
109 | public function hasManager($name) |
||
113 | |||
114 | public function getManager($name) |
||
118 | |||
119 | /** |
||
120 | * Returns list of events the plugin is subscribed to. |
||
121 | * |
||
122 | * @return array list of events |
||
123 | */ |
||
124 | 1 | public static function getSubscribedEvents() |
|
125 | { |
||
126 | return [ |
||
127 | # InstallerEvents::PRE_DEPENDENCIES_SOLVING => array( |
||
128 | # array('onPreDependenciesSolving', 0), |
||
129 | # ), |
||
130 | # PluginEvents::COMMAND => array( |
||
131 | # ['onCommand', 0], |
||
132 | # ), |
||
133 | 1 | ScriptEvents::POST_INSTALL_CMD => [ |
|
134 | 1 | ['onPostInstall', 0], |
|
135 | 1 | ], |
|
136 | 1 | ScriptEvents::POST_UPDATE_CMD => [ |
|
137 | 1 | ['onPostUpdate', 0], |
|
138 | 1 | ], |
|
139 | 1 | ]; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param InstallerEvent $event |
||
144 | */ |
||
145 | public function onPreDependenciesSolving(InstallerEvent $event) |
||
153 | |||
154 | public function scanAssetDependencies(PackageInterface $package) |
||
187 | |||
188 | /** |
||
189 | * @param CommandEvent $event |
||
190 | */ |
||
191 | public function onCommand(CommandEvent $event) |
||
200 | |||
201 | /** |
||
202 | * Perform install. Called by composer after install. |
||
203 | * |
||
204 | * @param Event $event |
||
205 | * @void |
||
206 | */ |
||
207 | 1 | public function onPostInstall(Event $event) |
|
217 | |||
218 | /** |
||
219 | * Perform update. Called by composer after update. |
||
220 | * |
||
221 | * @param Event $event |
||
222 | */ |
||
223 | public function onPostUpdate(Event $event) |
||
228 | |||
229 | /** |
||
230 | * Sets [[packages]]. |
||
231 | * |
||
232 | * @param PackageInterface[] $packages |
||
233 | */ |
||
234 | 3 | public function setPackages(array $packages) |
|
238 | |||
239 | /** |
||
240 | * Gets [[packages]]. |
||
241 | * @return \Composer\Package\PackageInterface[] |
||
242 | */ |
||
243 | 2 | public function getPackages() |
|
252 | |||
253 | /** |
||
254 | * Returns package with given name if exists. |
||
255 | * @param string $name package name |
||
256 | * @return \Composer\Package\PackageInterface|null |
||
257 | */ |
||
258 | public function findPackage($name) |
||
266 | |||
267 | /** |
||
268 | * Scan packages from the composer objects. |
||
269 | * @void |
||
270 | */ |
||
271 | 1 | protected function scanPackages() |
|
296 | |||
297 | /** |
||
298 | * Load packages from given lock file. |
||
299 | * |
||
300 | * @param JsonFile $lockFile |
||
301 | * @void |
||
302 | */ |
||
303 | protected function loadPackages(JsonFile $lockFile) |
||
310 | |||
311 | /** |
||
312 | * Install packages after loading/scanning. |
||
313 | * @param string $action |
||
314 | * @void |
||
315 | */ |
||
316 | 1 | protected function runAction($action) |
|
327 | |||
328 | /** |
||
329 | * Get absolute path to composer vendor dir. |
||
330 | * @return string |
||
331 | */ |
||
332 | 1 | public function getVendorDir() |
|
340 | } |
||
341 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.