Complex classes like InputTransformer 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 InputTransformer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class InputTransformer |
||
| 15 | { |
||
| 16 | const SEPARATOR = '#'; |
||
| 17 | |||
| 18 | const KEY_PACKAGE_NAME = 'package-name'; |
||
| 19 | const KEY_TYPE = 'type'; |
||
| 20 | const KEY_LICENSE = 'license'; |
||
| 21 | const KEY_PACKAGE_VERSION = 'package-version'; |
||
| 22 | const KEY_DESCRIPTION = 'description'; |
||
| 23 | const KEY_KEYWORD = 'keyword'; |
||
| 24 | const KEY_AUTHOR = 'author'; |
||
| 25 | const KEY_PROVIDED_PACKAGE = 'provide'; |
||
| 26 | const KEY_SUGGESTED_PACKAGE = 'suggest'; |
||
| 27 | const KEY_SUPPORT = 'support'; |
||
| 28 | const KEY_AUTOLOAD_PSR0 = 'autoload-psr0'; |
||
| 29 | const KEY_AUTOLOAD_PSR4 = 'autoload-psr4'; |
||
| 30 | const KEY_AUTOLOAD_DEV_PSR0 = 'autoload-dev-psr0'; |
||
| 31 | const KEY_AUTOLOAD_DEV_PSR4 = 'autoload-dev-psr4'; |
||
| 32 | const KEY_REQUIRE = 'require'; |
||
| 33 | const KEY_REQUIRE_DEV = 'require-dev'; |
||
| 34 | const KEY_SCRIPT = 'script'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $inputList |
||
| 38 | * |
||
| 39 | * @return ConfigurationFile|null |
||
| 40 | */ |
||
| 41 | 10 | public function fromCommandLine($inputList) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param array $inputList |
||
| 48 | * |
||
| 49 | * @return ConfigurationFile|null |
||
| 50 | */ |
||
| 51 | 10 | protected function createConfigurationFile(array $inputList) |
|
| 52 | { |
||
| 53 | $defaultKeyList = [ |
||
| 54 | 10 | self::KEY_PACKAGE_NAME, |
|
| 55 | 10 | self::KEY_DESCRIPTION, |
|
| 56 | 10 | self::KEY_PACKAGE_VERSION, |
|
| 57 | 10 | self::KEY_TYPE, |
|
| 58 | 10 | self::KEY_KEYWORD, |
|
| 59 | 10 | self::KEY_LICENSE, |
|
| 60 | 10 | self::KEY_AUTHOR, |
|
| 61 | 10 | self::KEY_SUPPORT, |
|
| 62 | 10 | self::KEY_REQUIRE, |
|
| 63 | 10 | self::KEY_REQUIRE_DEV, |
|
| 64 | 10 | self::KEY_PROVIDED_PACKAGE, |
|
| 65 | 10 | self::KEY_SUGGESTED_PACKAGE, |
|
| 66 | 10 | self::KEY_AUTOLOAD_PSR0, |
|
| 67 | 10 | self::KEY_AUTOLOAD_PSR4, |
|
| 68 | 10 | self::KEY_AUTOLOAD_DEV_PSR0, |
|
| 69 | 10 | self::KEY_AUTOLOAD_DEV_PSR4, |
|
| 70 | 10 | self::KEY_SCRIPT, |
|
| 71 | 10 | ]; |
|
| 72 | $defaultNormalizedFileKeyList = [ |
||
| 73 | 10 | ConfigurationFile::KEY_NAME => [self::KEY_PACKAGE_NAME], |
|
| 74 | 10 | ConfigurationFile::KEY_DESCRIPTION => [self::KEY_DESCRIPTION], |
|
| 75 | 10 | ConfigurationFile::KEY_VERSION => [self::KEY_PACKAGE_VERSION], |
|
| 76 | 10 | ConfigurationFile::KEY_TYPE => [self::KEY_TYPE], |
|
| 77 | 10 | ConfigurationFile::KEY_KEYWORDS => [self::KEY_KEYWORD], |
|
| 78 | 10 | ConfigurationFile::KEY_LICENSE => [self::KEY_LICENSE], |
|
| 79 | 10 | ConfigurationFile::KEY_AUTHORS => [self::KEY_AUTHOR], |
|
| 80 | 10 | ConfigurationFile::KEY_SUPPORT => [self::KEY_SUPPORT], |
|
| 81 | 10 | ConfigurationFile::KEY_REQUIRE => [self::KEY_REQUIRE], |
|
| 82 | 10 | ConfigurationFile::KEY_REQUIRE_DEV => [self::KEY_REQUIRE_DEV], |
|
| 83 | 10 | ConfigurationFile::KEY_PROVIDE => [self::KEY_PROVIDED_PACKAGE], |
|
| 84 | 10 | ConfigurationFile::KEY_SUGGEST => [self::KEY_SUGGESTED_PACKAGE], |
|
| 85 | 10 | ConfigurationFile::KEY_AUTOLOAD => [self::KEY_AUTOLOAD_PSR0, self::KEY_AUTOLOAD_PSR4], |
|
| 86 | 10 | ConfigurationFile::KEY_AUTOLOAD_DEV => [self::KEY_AUTOLOAD_DEV_PSR0, self::KEY_AUTOLOAD_DEV_PSR4], |
|
| 87 | 10 | ConfigurationFile::KEY_SCRIPTS => [self::KEY_SCRIPT], |
|
| 88 | 10 | ]; |
|
| 89 | 10 | if (0 === count(array_intersect($defaultKeyList, array_keys($inputList)))) { |
|
| 90 | 1 | return null; |
|
| 91 | } |
||
| 92 | 9 | $fileKeyList = []; |
|
| 93 | 9 | foreach ($defaultNormalizedFileKeyList as $fileKey => $inputKeyList) { |
|
| 94 | 9 | foreach ($inputKeyList as $inputKey) { |
|
| 95 | 9 | if (isset($inputList[$inputKey])) { |
|
| 96 | 9 | $fileKeyList[] = $fileKey; |
|
| 97 | 9 | break; |
|
| 98 | } |
||
| 99 | 9 | } |
|
| 100 | 9 | } |
|
| 101 | |||
| 102 | 9 | return new ConfigurationFile( |
|
| 103 | 9 | new Configuration( |
|
| 104 | 9 | $this->getValue($inputList, self::KEY_PACKAGE_NAME, null), |
|
| 105 | 9 | $this->getValue($inputList, self::KEY_TYPE, null), |
|
| 106 | 9 | $this->getValue($inputList, self::KEY_LICENSE, null), |
|
| 107 | 9 | $this->getValue($inputList, self::KEY_PACKAGE_VERSION, null), |
|
| 108 | 9 | $this->getValue($inputList, self::KEY_DESCRIPTION, null), |
|
| 109 | 9 | $this->extractKeywords($inputList), |
|
| 110 | 9 | $this->extractAuthors($inputList), |
|
| 111 | 9 | $this->extractProvidedPackages($inputList), |
|
| 112 | 9 | $this->extractSuggestedPackages($inputList), |
|
| 113 | 9 | $this->extractSupports($inputList), |
|
| 114 | 9 | $this->extractAutoloads($inputList), |
|
| 115 | 9 | $this->extractAutoloadsDev($inputList), |
|
| 116 | 9 | $this->extractRequiredPackages($inputList), |
|
| 117 | 9 | $this->extractRequiredDevPackages($inputList), |
|
| 118 | 9 | $this->extractScripts($inputList) |
|
| 119 | 9 | ), |
|
| 120 | $fileKeyList |
||
| 121 | 9 | ); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param array $inputList |
||
| 126 | * @param string $key |
||
| 127 | * @param string $defaultValue |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 9 | protected function getValue(array $inputList, $key, $defaultValue) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param array $inputList |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | 9 | protected function extractKeywords(array $inputList) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param array $inputList |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 9 | protected function extractAuthors(array $inputList) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $inputList |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | 9 | protected function extractProvidedPackages(array $inputList) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param array $inputList |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 9 | protected function extractSuggestedPackages(array $inputList) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param array $inputList |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | 9 | protected function extractSupports(array $inputList) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param array $inputList |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | 9 | protected function extractAutoloads(array $inputList) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $inputList |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | 9 | protected function extractAutoloadsDev(array $inputList) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $inputList |
||
| 277 | * |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | 9 | protected function extractRequiredPackages(array $inputList) |
|
| 292 | /** |
||
| 293 | * @param array $inputList |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | 9 | protected function extractRequiredDevPackages(array $inputList) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param array $inputList |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 9 | protected function extractScripts(array $inputList) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $value |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | 9 | protected function extractDataFromValue($value) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param array $inputList |
||
| 340 | * @param string $optionKey |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | 9 | protected function extractAutoloadList(array $inputList, $optionKey) |
|
| 356 | } |
||
| 357 |