Complex classes like GeneratorOptions 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 GeneratorOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class GeneratorOptions extends AbstractYamlReader |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Common values used as option's value |
||
| 9 | */ |
||
| 10 | const VALUE_START = 'start'; |
||
| 11 | const VALUE_END = 'end'; |
||
| 12 | const VALUE_NONE = 'none'; |
||
| 13 | const VALUE_CAT = 'cat'; |
||
| 14 | const VALUE_TRUE = true; |
||
| 15 | const VALUE_FALSE = false; |
||
| 16 | /** |
||
| 17 | * Possible option keys |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | const SUFFIX = 'suffix'; |
||
| 21 | const PREFIX = 'prefix'; |
||
| 22 | const ORIGIN = 'origin'; |
||
| 23 | const CATEGORY = 'category'; |
||
| 24 | const VALIDATION = 'validation'; |
||
| 25 | const STANDALONE = 'standalone'; |
||
| 26 | const PROXY_HOST = 'proxy_host'; |
||
| 27 | const PROXY_PORT = 'proxy_port'; |
||
| 28 | const PROXY_LOGIN = 'proxy_login'; |
||
| 29 | const BASIC_LOGIN = 'basic_login'; |
||
| 30 | const DESTINATION = 'destination'; |
||
| 31 | const ADD_COMMENTS = 'add_comments'; |
||
| 32 | const STRUCT_CLASS = 'struct_class'; |
||
| 33 | const SOAP_OPTIONS = 'soap_options'; |
||
| 34 | const ENUMS_FOLDER = 'enums_folder'; |
||
| 35 | const ARRAYS_FOLDER = 'arrays_folder'; |
||
| 36 | const COMPOSER_NAME = 'composer_name'; |
||
| 37 | const STRUCTS_FOLDER = 'structs_folder'; |
||
| 38 | const PROXY_PASSWORD = 'proxy_password'; |
||
| 39 | const BASIC_PASSWORD = 'basic_password'; |
||
| 40 | const GATHER_METHODS = 'gather_methods'; |
||
| 41 | const SERVICES_FOLDER = 'services_folder'; |
||
| 42 | const NAMESPACE_PREFIX = 'namespace_prefix'; |
||
| 43 | const SOAP_CLIENT_CLASS = 'soap_client_class'; |
||
| 44 | const STRUCT_ARRAY_CLASS = 'struct_array_class'; |
||
| 45 | const GENERATE_TUTORIAL_FILE = 'generate_tutorial_file'; |
||
| 46 | const GENERIC_CONSTANTS_NAME = 'generic_constants_names'; |
||
| 47 | /** |
||
| 48 | * Generator's options |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $options; |
||
| 52 | /** |
||
| 53 | * @param string $filename |
||
| 54 | */ |
||
| 55 | 36 | protected function __construct($filename) |
|
| 56 | { |
||
| 57 | 36 | $this->options = array(); |
|
| 58 | 36 | $this->parseOptions($filename); |
|
| 59 | 32 | } |
|
| 60 | /** |
||
| 61 | * Parse options for generator |
||
| 62 | * @param string $filename options's file to parse |
||
| 63 | * @return GeneratorOptions |
||
| 64 | */ |
||
| 65 | 36 | protected function parseOptions($filename) |
|
| 66 | { |
||
| 67 | 36 | $options = $this->loadYaml($filename); |
|
| 68 | 36 | if (is_array($options)) { |
|
| 69 | 32 | $this->options = $options; |
|
| 70 | 32 | } else { |
|
| 71 | 4 | throw new \InvalidArgumentException(sprintf('Settings contained by "%s" are not valid as the settings are not contained by an array: "%s"', $filename, gettype($options)), __LINE__); |
|
| 72 | } |
||
| 73 | 32 | return $this; |
|
| 74 | } |
||
| 75 | /** |
||
| 76 | * Returns the option value |
||
| 77 | * @throws InvalidArgumentException |
||
| 78 | * @param string $optionName |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | 1052 | public function getOptionValue($optionName) |
|
| 82 | { |
||
| 83 | 1052 | if (!isset($this->options[$optionName])) { |
|
| 84 | 4 | throw new \InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__); |
|
| 85 | } |
||
| 86 | 1048 | return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default']; |
|
| 87 | } |
||
| 88 | /** |
||
| 89 | * Allows to add an option and set its value |
||
| 90 | * @throws InvalidArgumentException |
||
| 91 | * @param string $optionName |
||
| 92 | * @return GeneratorOptions |
||
| 93 | */ |
||
| 94 | 796 | public function setOptionValue($optionName, $optionValue, array $values = array()) |
|
| 95 | { |
||
| 96 | 796 | if (!isset($this->options[$optionName])) { |
|
| 97 | 4 | $this->options[$optionName] = array( |
|
| 98 | 4 | 'value' => $optionValue, |
|
| 99 | 4 | 'values' => $values, |
|
| 100 | ); |
||
| 101 | 796 | } elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) { |
|
| 102 | 8 | throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__); |
|
| 103 | } else { |
||
| 104 | 784 | $this->options[$optionName]['value'] = $optionValue; |
|
| 105 | } |
||
| 106 | 788 | return $this; |
|
| 107 | } |
||
| 108 | /** |
||
| 109 | * @throws \InvalidArgumentException |
||
| 110 | * @param string options's file to parse |
||
| 111 | * @return GeneratorOptions |
||
| 112 | */ |
||
| 113 | 780 | public static function instance($filename = null) |
|
| 114 | { |
||
| 115 | 780 | return parent::instance(empty($filename) ? self::getDefaultConfigurationPath() : $filename); |
|
| 116 | } |
||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 60 | public static function getDefaultConfigurationPath() |
|
| 121 | { |
||
| 122 | 60 | return dirname(__FILE__) . '/../resources/config/generator_options.yml'; |
|
| 123 | } |
||
| 124 | /** |
||
| 125 | * Get category option value |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 284 | public function getCategory() |
|
| 129 | { |
||
| 130 | 284 | return $this->getOptionValue(self::CATEGORY); |
|
| 131 | } |
||
| 132 | /** |
||
| 133 | * Set current category option value |
||
| 134 | * @throws \InvalidArgumentException |
||
| 135 | * @param string $category |
||
| 136 | * @return GeneratorOptions |
||
| 137 | */ |
||
| 138 | 300 | public function setCategory($category) |
|
| 139 | { |
||
| 140 | 300 | return $this->setOptionValue(self::CATEGORY, $category); |
|
| 141 | } |
||
| 142 | /** |
||
| 143 | * Get add comments option value |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | 216 | public function getAddComments() |
|
| 147 | { |
||
| 148 | 216 | return $this->getOptionValue(self::ADD_COMMENTS); |
|
| 149 | } |
||
| 150 | /** |
||
| 151 | * Set current add comments option value |
||
| 152 | * @throws \InvalidArgumentException |
||
| 153 | * @param array $addComments |
||
| 154 | * @return GeneratorOptions |
||
| 155 | */ |
||
| 156 | 340 | public function setAddComments(array $addComments = array()) |
|
| 157 | { |
||
| 158 | /** |
||
| 159 | * If array is type array("author:john Doe","Release:1",) |
||
| 160 | */ |
||
| 161 | 340 | $comments = array(); |
|
| 162 | 340 | foreach ($addComments as $index=>$value) { |
|
| 163 | 284 | if (is_numeric($index) && strpos($value, ':') > 0) { |
|
| 164 | 4 | list($tag, $val) = explode(':', $value); |
|
| 165 | 4 | $comments[$tag] = $val; |
|
| 166 | 4 | } else { |
|
| 167 | 280 | $comments[$index] = $value; |
|
| 168 | } |
||
| 169 | 340 | } |
|
| 170 | 340 | return $this->setOptionValue(self::ADD_COMMENTS, $comments); |
|
| 171 | } |
||
| 172 | /** |
||
| 173 | * Get gather methods option value |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 484 | public function getGatherMethods() |
|
| 177 | { |
||
| 178 | 484 | return $this->getOptionValue(self::GATHER_METHODS); |
|
| 179 | } |
||
| 180 | /** |
||
| 181 | * Set current gather methods option value |
||
| 182 | * @throws \InvalidArgumentException |
||
| 183 | * @param string $gatherMethods |
||
| 184 | * @return GeneratorOptions |
||
| 185 | */ |
||
| 186 | 300 | public function setGatherMethods($gatherMethods) |
|
| 187 | { |
||
| 188 | 300 | return $this->setOptionValue(self::GATHER_METHODS, $gatherMethods); |
|
| 189 | } |
||
| 190 | /** |
||
| 191 | * Get generate tutorial file option value |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 36 | public function getGenerateTutorialFile() |
|
| 195 | { |
||
| 196 | 36 | return $this->getOptionValue(self::GENERATE_TUTORIAL_FILE); |
|
| 197 | } |
||
| 198 | /** |
||
| 199 | * Set current generate tutorial file option value |
||
| 200 | * @throws \InvalidArgumentException |
||
| 201 | * @param bool $generateTutorialFile |
||
| 202 | * @return GeneratorOptions |
||
| 203 | */ |
||
| 204 | 56 | public function setGenerateTutorialFile($generateTutorialFile) |
|
| 205 | { |
||
| 206 | 56 | return $this->setOptionValue(self::GENERATE_TUTORIAL_FILE, $generateTutorialFile); |
|
| 207 | } |
||
| 208 | /** |
||
| 209 | * Get namespace option value |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 260 | public function getNamespace() |
|
| 213 | { |
||
| 214 | 260 | return $this->getOptionValue(self::NAMESPACE_PREFIX); |
|
| 215 | } |
||
| 216 | /** |
||
| 217 | * Set current namespace option value |
||
| 218 | * @throws \InvalidArgumentException |
||
| 219 | * @param string $namespace |
||
| 220 | * @return GeneratorOptions |
||
| 221 | */ |
||
| 222 | 44 | public function setNamespace($namespace) |
|
| 223 | { |
||
| 224 | 44 | return $this->setOptionValue(self::NAMESPACE_PREFIX, $namespace); |
|
| 225 | } |
||
| 226 | /** |
||
| 227 | * Get generic constants name option value |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 68 | public function getGenericConstantsName() |
|
| 231 | { |
||
| 232 | 68 | return $this->getOptionValue(self::GENERIC_CONSTANTS_NAME); |
|
| 233 | } |
||
| 234 | /** |
||
| 235 | * Set current generic constants name option value |
||
| 236 | * @throws \InvalidArgumentException |
||
| 237 | * @param bool $genericConstantsName |
||
| 238 | * @return GeneratorOptions |
||
| 239 | */ |
||
| 240 | 56 | public function setGenericConstantsName($genericConstantsName) |
|
| 241 | { |
||
| 242 | 56 | return $this->setOptionValue(self::GENERIC_CONSTANTS_NAME, $genericConstantsName); |
|
| 243 | } |
||
| 244 | /** |
||
| 245 | * Get standalone option value |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 68 | public function getStandalone() |
|
| 249 | { |
||
| 250 | 68 | return $this->getOptionValue(self::STANDALONE); |
|
| 251 | } |
||
| 252 | /** |
||
| 253 | * Set current standalone option value |
||
| 254 | * @throws \InvalidArgumentException |
||
| 255 | * @param bool $standalone |
||
| 256 | * @return GeneratorOptions |
||
| 257 | */ |
||
| 258 | 32 | public function setStandalone($standalone) |
|
| 259 | { |
||
| 260 | 32 | return $this->setOptionValue(self::STANDALONE, $standalone); |
|
| 261 | } |
||
| 262 | /** |
||
| 263 | * Get validation option value |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 120 | public function getValidation() |
|
| 270 | /** |
||
| 271 | * Set current validation option value |
||
| 272 | * @throws \InvalidArgumentException |
||
| 273 | * @param bool $validation |
||
| 274 | * @return GeneratorOptions |
||
| 275 | */ |
||
| 276 | 16 | public function setValidation($validation) |
|
| 280 | /** |
||
| 281 | * Get struct class option value |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 100 | public function getStructClass() |
|
| 285 | { |
||
| 286 | 100 | return $this->getOptionValue(self::STRUCT_CLASS); |
|
| 287 | } |
||
| 288 | /** |
||
| 289 | * Set current struct class option value |
||
| 290 | * @throws \InvalidArgumentException |
||
| 291 | * @param string $structClass |
||
| 292 | * @return GeneratorOptions |
||
| 293 | */ |
||
| 294 | 36 | public function setStructClass($structClass) |
|
| 295 | { |
||
| 296 | 36 | return $this->setOptionValue(self::STRUCT_CLASS, $structClass); |
|
| 297 | } |
||
| 298 | /** |
||
| 299 | * Get struct array class option value |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 44 | public function getStructArrayClass() |
|
| 306 | /** |
||
| 307 | * Set current struct array class option value |
||
| 308 | * @throws \InvalidArgumentException |
||
| 309 | * @param string $structArrayClass |
||
| 310 | * @return GeneratorOptions |
||
| 311 | */ |
||
| 312 | 28 | public function setStructArrayClass($structArrayClass) |
|
| 316 | /** |
||
| 317 | * Get struct array class option value |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 100 | public function getSoapClientClass() |
|
| 324 | /** |
||
| 325 | * Set current struct array class option value |
||
| 326 | * @throws \InvalidArgumentException |
||
| 327 | * @param string $soapClientClass |
||
| 328 | * @return GeneratorOptions |
||
| 329 | */ |
||
| 330 | 28 | public function setSoapClientClass($soapClientClass) |
|
| 334 | /** |
||
| 335 | * Get origin option value |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | 524 | public function getOrigin() |
|
| 342 | /** |
||
| 343 | * Set current origin option value |
||
| 344 | * @throws \InvalidArgumentException |
||
| 345 | * @param string $origin |
||
| 346 | * @return GeneratorOptions |
||
| 347 | */ |
||
| 348 | 532 | public function setOrigin($origin) |
|
| 352 | /** |
||
| 353 | * Get destination option value |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 300 | public function getDestination() |
|
| 360 | /** |
||
| 361 | * Set current destination option value |
||
| 362 | * @throws \InvalidArgumentException |
||
| 363 | * @param string $destination |
||
| 364 | * @return GeneratorOptions |
||
| 365 | */ |
||
| 366 | 536 | public function setDestination($destination) |
|
| 370 | /** |
||
| 371 | * Get prefix option value |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 540 | public function getPrefix() |
|
| 378 | /** |
||
| 379 | * Set current prefix option value |
||
| 380 | * @throws \InvalidArgumentException |
||
| 381 | * @param string $prefix |
||
| 382 | * @return GeneratorOptions |
||
| 383 | */ |
||
| 384 | 316 | public function setPrefix($prefix) |
|
| 388 | /** |
||
| 389 | * Get suffix option value |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | 528 | public function getSuffix() |
|
| 396 | /** |
||
| 397 | * Set current suffix option value |
||
| 398 | * @throws \InvalidArgumentException |
||
| 399 | * @param string $suffix |
||
| 400 | * @return GeneratorOptions |
||
| 401 | */ |
||
| 402 | 52 | public function setSuffix($suffix) |
|
| 406 | /** |
||
| 407 | * Get basic login option value |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | 528 | public function getBasicLogin() |
|
| 414 | /** |
||
| 415 | * Set current basic login option value |
||
| 416 | * @throws \InvalidArgumentException |
||
| 417 | * @param string $basicLogin |
||
| 418 | * @return GeneratorOptions |
||
| 419 | */ |
||
| 420 | 28 | public function setBasicLogin($basicLogin) |
|
| 424 | /** |
||
| 425 | * Get basic password option value |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 528 | public function getBasicPassword() |
|
| 432 | /** |
||
| 433 | * Set current basic password option value |
||
| 434 | * @throws \InvalidArgumentException |
||
| 435 | * @param string $basicPassword |
||
| 436 | * @return GeneratorOptions |
||
| 437 | */ |
||
| 438 | 28 | public function setBasicPassword($basicPassword) |
|
| 442 | /** |
||
| 443 | * Get basic proxy host option value |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | 528 | public function getProxyHost() |
|
| 450 | /** |
||
| 451 | * Set current proxy host option value |
||
| 452 | * @throws \InvalidArgumentException |
||
| 453 | * @param string $proxyHost |
||
| 454 | * @return GeneratorOptions |
||
| 455 | */ |
||
| 456 | 28 | public function setProxyHost($proxyHost) |
|
| 460 | /** |
||
| 461 | * Get basic proxy port option value |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | 528 | public function getProxyPort() |
|
| 468 | /** |
||
| 469 | * Set current proxy port option value |
||
| 470 | * @throws \InvalidArgumentException |
||
| 471 | * @param string $proxyPort |
||
| 472 | * @return GeneratorOptions |
||
| 473 | */ |
||
| 474 | 28 | public function setProxyPort($proxyPort) |
|
| 478 | /** |
||
| 479 | * Get basic proxy login option value |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 528 | public function getProxyLogin() |
|
| 486 | /** |
||
| 487 | * Set current proxy login option value |
||
| 488 | * @throws \InvalidArgumentException |
||
| 489 | * @param string $proxyLogin |
||
| 490 | * @return GeneratorOptions |
||
| 491 | */ |
||
| 492 | 28 | public function setProxyLogin($proxyLogin) |
|
| 496 | /** |
||
| 497 | * Get basic proxy password option value |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | 528 | public function getProxyPassword() |
|
| 504 | /** |
||
| 505 | * Set current proxy password option value |
||
| 506 | * @throws \InvalidArgumentException |
||
| 507 | * @param string $proxyPassword |
||
| 508 | * @return GeneratorOptions |
||
| 509 | */ |
||
| 510 | 28 | public function setProxyPassword($proxyPassword) |
|
| 514 | /** |
||
| 515 | * Get basic soap options option value |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | 520 | public function getSoapOptions() |
|
| 522 | /** |
||
| 523 | * Set current soap options option value |
||
| 524 | * @throws \InvalidArgumentException |
||
| 525 | * @param array $soapOptions |
||
| 526 | * @return GeneratorOptions |
||
| 527 | */ |
||
| 528 | 32 | public function setSoapOptions(array $soapOptions) |
|
| 532 | /** |
||
| 533 | * Get composer name option value |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | 44 | public function getComposerName() |
|
| 540 | /** |
||
| 541 | * Set current composer name option value |
||
| 542 | * @throws \InvalidArgumentException |
||
| 543 | * @param string $composerName |
||
| 544 | * @return GeneratorOptions |
||
| 545 | */ |
||
| 546 | 68 | public function setComposerName($composerName) |
|
| 550 | /** |
||
| 551 | * Get structs folder option value |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | 268 | public function getStructsFolder() |
|
| 558 | /** |
||
| 559 | * Set current structs folder option value |
||
| 560 | * @throws \InvalidArgumentException |
||
| 561 | * @param string $structsFolder |
||
| 562 | * @return GeneratorOptions |
||
| 563 | */ |
||
| 564 | 28 | public function setStructsFolder($structsFolder) |
|
| 568 | /** |
||
| 569 | * Get arrays folder option value |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | 60 | public function getArraysFolder() |
|
| 576 | /** |
||
| 577 | * Set current arrays folder option value |
||
| 578 | * @throws \InvalidArgumentException |
||
| 579 | * @param string $arraysFolder |
||
| 580 | * @return GeneratorOptions |
||
| 581 | */ |
||
| 582 | 28 | public function setArraysFolder($arraysFolder) |
|
| 586 | /** |
||
| 587 | * Get enums folder option value |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | 100 | public function getEnumsFolder() |
|
| 594 | /** |
||
| 595 | * Set current enums folder option value |
||
| 596 | * @throws \InvalidArgumentException |
||
| 597 | * @param string $enumsFolder |
||
| 598 | * @return GeneratorOptions |
||
| 599 | */ |
||
| 600 | 28 | public function setEnumsFolder($enumsFolder) |
|
| 604 | /** |
||
| 605 | * Get services folder option value |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | 520 | public function getServicesFolder() |
|
| 612 | /** |
||
| 613 | * Set current services folder option value |
||
| 614 | * @throws \InvalidArgumentException |
||
| 615 | * @param string $servicesFolder |
||
| 616 | * @return GeneratorOptions |
||
| 617 | */ |
||
| 618 | 28 | public function setServicesFolder($servicesFolder) |
|
| 622 | /** |
||
| 623 | * @return string[] |
||
| 624 | */ |
||
| 625 | 28 | public function toArray() |
|
| 633 | } |
||
| 634 |