Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Composer 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 Composer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Composer |
||
16 | { |
||
17 | /** @var string Path to current web-application */ |
||
18 | private $systemPath; |
||
19 | |||
20 | /** @var string composer lock file name */ |
||
21 | private $lockFileName = 'composer.lock'; |
||
22 | |||
23 | /** @var array List of available vendors */ |
||
24 | private $vendorsList = array(); |
||
25 | |||
26 | /** @var string $ignoreKey */ |
||
27 | private $ignoreKey; |
||
28 | |||
29 | /** @var string $includeKey */ |
||
30 | private $includeKey; |
||
31 | |||
32 | /** @var array List of ignored packages */ |
||
33 | private $ignorePackages = array(); |
||
34 | |||
35 | /** @var array Packages list with require packages*/ |
||
36 | private $packagesList = array(); |
||
37 | |||
38 | private $packagesListExtra = array(); |
||
39 | |||
40 | /** |
||
41 | * Add available vendor |
||
42 | * @param $vendor Available vendor |
||
43 | * @return $this |
||
44 | */ |
||
45 | public function vendor($vendor) |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Set name of composer extra parameter to ignore package |
||
56 | * @param $ignoreKey Name |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function ignoreKey($ignoreKey) |
||
64 | |||
65 | /** |
||
66 | * Set name of composer extra parameter to include package |
||
67 | * @param $includeKey Name |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function includeKey($includeKey) |
||
75 | |||
76 | /** |
||
77 | * Add ignored package |
||
78 | * @param $vendor Ignored package |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function ignorePackage($package) |
||
88 | |||
89 | /** |
||
90 | * Create sorted packages list |
||
91 | * @return array Packages list ('package name'=>'rating') |
||
92 | */ |
||
93 | public function create(& $packages, $systemPath, $parameters = array()) |
||
117 | |||
118 | /** |
||
119 | * Provide creating sorting list |
||
120 | * @return array list of sorted packages |
||
121 | */ |
||
122 | public function sort() |
||
147 | |||
148 | /** |
||
149 | *Check result of sorting |
||
150 | * @param $list final list of packages |
||
151 | * |
||
152 | * @return bool result |
||
153 | */ |
||
154 | public function checkSort($list) |
||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * Create list of relevant packages |
||
179 | * @param $packages Composer lock list of packages |
||
180 | * @return array List of relevant packages |
||
181 | */ |
||
182 | private function includeList($packages) |
||
194 | |||
195 | /** |
||
196 | * Is package include |
||
197 | * @param $package Composer package |
||
198 | * @return bool - is package include |
||
199 | */ |
||
200 | private function isInclude($package) |
||
212 | |||
213 | /** |
||
214 | * Is package ignored |
||
215 | * @param $package Composer package |
||
216 | * @return bool - is package ignored |
||
217 | */ |
||
218 | private function isIgnore($package) |
||
229 | |||
230 | /** |
||
231 | * Fill list of relevant packages with there require packages |
||
232 | * @param $packages Composer lock file object |
||
233 | */ |
||
234 | private function packagesFill($packages) |
||
251 | |||
252 | private function readFile($path) |
||
269 | |||
270 | /** |
||
271 | * Create list of of required packages |
||
272 | * @param null $includeModule Dependent package |
||
273 | * @param array $ignoreModules |
||
274 | * |
||
275 | * @return array required packages |
||
276 | */ |
||
277 | private function getRequiredList($includeModule = null, $ignoreModules = array()) |
||
299 | |||
300 | /** |
||
301 | * Recursive function that get list of required packages |
||
302 | * @param $list List of packages |
||
303 | * @param array $result |
||
304 | * |
||
305 | * @return array required packages |
||
306 | */ |
||
307 | private function getReqList($list, $result = array()) { |
||
320 | |||
321 | /** |
||
322 | * Clear object parameters |
||
323 | */ |
||
324 | public function clear() |
||
332 | |||
333 | /** |
||
334 | * Insert a key after a specific key in an array. If key doesn't exist, value is appended |
||
335 | * to the end of the array. |
||
336 | * |
||
337 | * @param array $array |
||
338 | * @param string $key |
||
339 | * @param integer $newKey |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | public function insertKeyAfter( array $list, $key, $newKey ) |
||
350 | |||
351 | /** |
||
352 | * Insert a key before a specific key in an array. If key doesn't exist, value is prepended |
||
353 | * to the beginning of the array. |
||
354 | * |
||
355 | * @param array $array |
||
356 | * @param string $key |
||
357 | * @param integer $newKey |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | public function insertKeyBefore(array $list, $key, $newKey) |
||
367 | |||
368 | } |
||
369 |