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 | private $projectRequireDev = array(); |
||
41 | |||
42 | /** |
||
43 | * Add available vendor |
||
44 | * @param $vendor Available vendor |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function vendor($vendor) |
||
48 | { |
||
49 | if (!in_array($vendor, $this->vendorsList)) { |
||
50 | $this->vendorsList[] = $vendor.'/'; |
||
51 | } |
||
52 | return $this; |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Set name of composer extra parameter to ignore package |
||
58 | * @param $ignoreKey Name |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function ignoreKey($ignoreKey) |
||
62 | { |
||
63 | $this->ignoreKey = $ignoreKey; |
||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Set name of composer extra parameter to include package |
||
69 | * @param $includeKey Name |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function includeKey($includeKey) |
||
73 | { |
||
74 | $this->includeKey = $includeKey; |
||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Add ignored package |
||
80 | * @param $vendor Ignored package |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function ignorePackage($package) |
||
84 | { |
||
85 | if (!in_array($package, $this->ignorePackages)) { |
||
86 | $this->ignorePackages[] = $package; |
||
87 | } |
||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Create sorted packages list |
||
93 | * @return array Packages list ('package name'=>'rating') |
||
94 | */ |
||
95 | public function create(& $packages, $systemPath, $parameters = array()) |
||
96 | { |
||
97 | $class_vars = get_class_vars(get_class($this)); |
||
98 | |||
99 | foreach ($class_vars as $name => $value) { |
||
100 | if (isset($parameters[$name])) { |
||
101 | $this->$name = $parameters[$name]; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // Create list of relevant packages with there require packages |
||
106 | $this->packagesFill($this->readFile($systemPath)); |
||
107 | |||
108 | $resultList = $this->sort(); |
||
109 | |||
110 | foreach ($resultList as $package => $rating) { |
||
111 | $required = $this->getRequiredList($package); |
||
112 | $packages[$package] = $this->packagesListExtra[$package]; |
||
113 | $packages[$package]['required'] = $required; |
||
114 | $packages[$package]['composerName'] =$package; |
||
115 | $packages[$package]['projectRequireDev'] = |
||
116 | in_array($package, $this->projectRequireDev)?true:false; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Provide creating sorting list |
||
122 | * @return array list of sorted packages |
||
123 | */ |
||
124 | public function sort() |
||
125 | { |
||
126 | $list = array(); |
||
127 | foreach ($this->packagesList as $package => $requiredList) { |
||
128 | if (!sizeof($list)||!isset($list[$package])) { |
||
129 | $list[$package] = 1; |
||
130 | } |
||
131 | foreach ($requiredList as $requiredPackage) { |
||
132 | if (isset($list[$requiredPackage])) { |
||
133 | $packagePos = array_search($package, array_keys($list)); |
||
134 | $requiredPackagePos = array_search($requiredPackage, array_keys($list)); |
||
135 | if ($packagePos < $requiredPackagePos) { |
||
136 | unset($list[$requiredPackage]); |
||
137 | $list = $this->insertKeyBefore($list, $package, $requiredPackage); |
||
138 | } |
||
139 | } else { |
||
140 | $list = $this->insertKeyBefore($list, $package, $requiredPackage); |
||
141 | } |
||
142 | |||
|
|||
143 | } |
||
144 | } |
||
145 | //$this->checkSort($list); |
||
146 | |||
147 | return $list; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | *Check result of sorting |
||
152 | * @param $list final list of packages |
||
153 | * |
||
154 | * @return bool result |
||
155 | */ |
||
156 | public function checkSort($list) |
||
157 | { |
||
158 | $status = true; |
||
159 | foreach ($this->packagesList as $package => $requiredList) { |
||
160 | foreach ($requiredList as $requiredPackage) { |
||
161 | if (isset($list[$requiredPackage])) { |
||
162 | $packagePos = array_search($package, array_keys($list)); |
||
163 | $requiredPackagePos = array_search($requiredPackage, array_keys($list)); |
||
164 | if ($packagePos < $requiredPackagePos) { |
||
165 | trace('error pos - '.$packagePos.' < '.$requiredPackagePos); |
||
166 | $status = false; |
||
167 | } |
||
168 | } else { |
||
169 | $status = false; |
||
170 | trace('error not isset!!!!! - '.$requiredPackage); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | return $status; |
||
175 | } |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Create list of relevant packages |
||
181 | * @param $packages Composer lock list of packages |
||
182 | * @return array List of relevant packages |
||
183 | */ |
||
184 | private function includeList($packages) |
||
196 | |||
197 | /** |
||
198 | * Is package include |
||
199 | * @param $package Composer package |
||
200 | * @return bool - is package include |
||
201 | */ |
||
202 | private function isInclude($package) |
||
214 | |||
215 | /** |
||
216 | * Is package ignored |
||
217 | * @param $package Composer package |
||
218 | * @return bool - is package ignored |
||
219 | */ |
||
220 | private function isIgnore($package) |
||
231 | |||
232 | /** |
||
233 | * Fill list of relevant packages with there require packages |
||
234 | * @param $packages Composer lock file object |
||
235 | */ |
||
236 | private function packagesFill($packages) |
||
237 | { |
||
238 | // Get included packages list |
||
253 | |||
254 | private function readFile($systemPath) |
||
280 | |||
281 | /** |
||
282 | * Create list of of required packages |
||
283 | * @param null $includeModule Dependent package |
||
284 | * @param array $ignoreModules |
||
285 | * |
||
286 | * @return array required packages |
||
287 | */ |
||
288 | private function getRequiredList($includeModule = null, $ignoreModules = array()) |
||
310 | |||
311 | /** |
||
312 | * Recursive function that get list of required packages |
||
313 | * @param $list List of packages |
||
314 | * @param array $result |
||
315 | * |
||
316 | * @return array required packages |
||
317 | */ |
||
318 | private function getReqList($list, $result = array()) { |
||
331 | |||
332 | /** |
||
333 | * Clear object parameters |
||
334 | */ |
||
335 | public function clear() |
||
343 | |||
344 | /** |
||
345 | * Insert a key after a specific key in an array. If key doesn't exist, value is appended |
||
346 | * to the end of the array. |
||
347 | * |
||
348 | * @param array $array |
||
349 | * @param string $key |
||
350 | * @param integer $newKey |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function insertKeyAfter( array $list, $key, $newKey ) |
||
361 | |||
362 | /** |
||
363 | * Insert a key before a specific key in an array. If key doesn't exist, value is prepended |
||
364 | * to the beginning of the array. |
||
365 | * |
||
366 | * @param array $array |
||
367 | * @param string $key |
||
368 | * @param integer $newKey |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | public function insertKeyBefore(array $list, $key, $newKey) |
||
378 | |||
379 | } |
||
380 |