Complex classes like PackageManager 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 PackageManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class PackageManager |
||
23 | { |
||
24 | /** |
||
25 | * @var Plugin the plugin instance |
||
26 | */ |
||
27 | protected $plugin; |
||
28 | |||
29 | /** |
||
30 | * @var string Package manager name: `bower` or `npm` |
||
31 | */ |
||
32 | protected $name; |
||
33 | |||
34 | /** |
||
35 | * @var string Package config file name: `bower.json` or `package.json` |
||
36 | */ |
||
37 | public $file; |
||
38 | |||
39 | /** |
||
40 | * @var string Package RC config file name: `.bowerrc` or `.npmrc` |
||
41 | */ |
||
42 | public $rcfile; |
||
43 | |||
44 | /** |
||
45 | * @var string Path to package manager binary |
||
46 | */ |
||
47 | public $bin; |
||
48 | |||
49 | /** |
||
50 | * @var string Package name of the PHP version of the package manager |
||
51 | */ |
||
52 | public $phpPackage; |
||
53 | |||
54 | /** |
||
55 | * @var string Binary name of PHP version of package manager |
||
56 | */ |
||
57 | protected $phpBin; |
||
58 | |||
59 | /** |
||
60 | * @var array Package config. Initially holds default config |
||
61 | */ |
||
62 | protected $config = []; |
||
63 | |||
64 | /** |
||
65 | * @var array RC config: .bowerrc or .npmrc |
||
66 | */ |
||
67 | protected $rc = []; |
||
68 | |||
69 | /** |
||
70 | * @var array List of keys holding dependencies |
||
71 | */ |
||
72 | protected $dependencies = ['dependencies', 'devDependencies']; |
||
73 | |||
74 | /** |
||
75 | * array known deps collected from requirements. |
||
76 | */ |
||
77 | protected $knownDeps = []; |
||
78 | |||
79 | /** |
||
80 | * Reads config file or dist config if exists, merges with default config. |
||
81 | * @param Plugin $plugin |
||
82 | * @void |
||
83 | */ |
||
84 | 3 | public function __construct(Plugin $plugin) |
|
94 | |||
95 | public function getName() |
||
99 | |||
100 | public function packageFullName($package) |
||
104 | |||
105 | public function setKnownDeps(PackageInterface $package, $type, $name, $constraint) |
||
114 | |||
115 | public function getKnownDeps(PackageInterface $package) |
||
120 | |||
121 | public function getConfig() |
||
125 | |||
126 | abstract public function setDestination($dir); |
||
127 | /** |
||
128 | * Reads the JSON config from the $path. |
||
129 | * |
||
130 | * @param string $path path to the Json file |
||
131 | * @return array|mixed |
||
132 | */ |
||
133 | 3 | public function readConfig($path) |
|
144 | |||
145 | /** |
||
146 | * Saves JSON config to the given path. |
||
147 | * |
||
148 | * @param string $path |
||
149 | * @param array $config |
||
150 | * @throws \Exception |
||
151 | */ |
||
152 | public function writeJson($path, array $config) |
||
157 | |||
158 | public function prepareConfig(array $config) |
||
175 | |||
176 | /** |
||
177 | * Fixes constraint for the package manager. |
||
178 | * Does nothing for NPM. Redefined in Bower. |
||
179 | * @param string $constraint |
||
180 | * @return string |
||
181 | */ |
||
182 | public function fixConstraint($constraint) |
||
186 | |||
187 | /** |
||
188 | * Scans the $package and extracts dependencies to the [[config]]. |
||
189 | * @param PackageInterface $package |
||
190 | */ |
||
191 | public function scanPackage(PackageInterface $package) |
||
208 | |||
209 | /** |
||
210 | * Merges the $config over the [[config]]. |
||
211 | * @param array $config |
||
212 | */ |
||
213 | protected function mergeConfig(array $config) |
||
221 | |||
222 | public function addDependency($type, $name, $constraint) |
||
230 | |||
231 | /** |
||
232 | * Set config. |
||
233 | * @param array $config |
||
234 | */ |
||
235 | public function setConfig(array $config) |
||
239 | |||
240 | /** |
||
241 | * Returns if the package manager has nonempty dependency list. |
||
242 | * @return bool |
||
243 | */ |
||
244 | 1 | public function hasDependencies() |
|
254 | |||
255 | /** |
||
256 | * Run the given action: show notice, write config and run `perform`. |
||
257 | * @param string $action the action name |
||
258 | */ |
||
259 | public function runAction($action) |
||
266 | |||
267 | public function saveConfigs() |
||
276 | |||
277 | abstract public function writeRc($path, $data); |
||
278 | |||
279 | /** |
||
280 | * Run installation. Specific for every package manager. |
||
281 | * @param string $action the action name |
||
282 | * @void |
||
283 | */ |
||
284 | protected function perform($action) |
||
291 | |||
292 | /** |
||
293 | * Prepares arguments and runs the command with [[passthru()]]. |
||
294 | * @param array $arguments |
||
295 | * @return integer the exit code |
||
296 | */ |
||
297 | public function passthru(array $arguments = []) |
||
302 | |||
303 | /** |
||
304 | * Prepares given command arguments. |
||
305 | * @param array $arguments |
||
306 | * @return string |
||
307 | */ |
||
308 | public function prepareCommand(array $arguments = []) |
||
317 | |||
318 | /** |
||
319 | * Set path to binary executable file. |
||
320 | * @param $bin |
||
321 | * @internal param string $value |
||
322 | */ |
||
323 | public function setBin($bin) |
||
327 | |||
328 | /** |
||
329 | * Get path to the binary executable file. |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getBin() |
||
340 | |||
341 | /** |
||
342 | * Find path to the binary. |
||
343 | * @return string |
||
344 | */ |
||
345 | public function detectBin() |
||
359 | |||
360 | public static function buildPath($parts) |
||
364 | } |
||
365 |
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.