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 $packagesListResorted = array(); |
||
| 39 | |||
| 40 | private $packagesListExtra = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Add available vendor |
||
| 44 | * @param $vendor Available vendor |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | public function vendor($vendor) |
||
| 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) |
||
| 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) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Add ignored package |
||
| 80 | * @param $vendor Ignored package |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function ignorePackage($package) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create sorted packages list |
||
| 93 | * @return array Packages list ('package name'=>'rating') |
||
| 94 | */ |
||
| 95 | public function create( & $packages, $systemPath, $parameters = array() ) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Provide creating sorting list |
||
| 122 | * @return array list of sorted packages |
||
| 123 | */ |
||
| 124 | public function sort() |
||
| 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) |
||
| 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) |
||
| 253 | |||
| 254 | private function readFile($path) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Create list of of required packages |
||
| 274 | * @param null $includeModule Dependent package |
||
| 275 | * @param array $ignoreModules |
||
| 276 | * |
||
| 277 | * @return array required packages |
||
| 278 | */ |
||
| 279 | private function getRequiredList($includeModule = null, $ignoreModules = array()) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Recursive function that get list of required packages |
||
| 304 | * @param $list List of packages |
||
| 305 | * @param array $result |
||
| 306 | * |
||
| 307 | * @return array required packages |
||
| 308 | */ |
||
| 309 | private function getReqList($list, $result = array()) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Clear object parameters |
||
| 325 | */ |
||
| 326 | public function clear() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Insert a key after a specific key in an array. If key doesn't exist, value is appended |
||
| 338 | * to the end of the array. |
||
| 339 | * |
||
| 340 | * @param array $array |
||
| 341 | * @param string $key |
||
| 342 | * @param integer $newKey |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function arrayInsertAfter( array $array, $key, $newKey ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Insert a key before a specific key in an array. If key doesn't exist, value is prepended |
||
| 355 | * to the beginning of the array. |
||
| 356 | * |
||
| 357 | * @param array $array |
||
| 358 | * @param string $key |
||
| 359 | * @param integer $newKey |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public function arrayInsertBefore( array $array, $key, $newKey ) { |
||
| 368 | |||
| 369 | } |
||
| 370 |