Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Generator |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Wsdl |
||
| 21 | * @var Wsdl |
||
| 22 | */ |
||
| 23 | private $wsdl; |
||
| 24 | /** |
||
| 25 | * @var GeneratorOptions |
||
| 26 | */ |
||
| 27 | private $options; |
||
| 28 | /** |
||
| 29 | * Used parsers |
||
| 30 | * @var GeneratorParsers |
||
| 31 | */ |
||
| 32 | private $parsers; |
||
| 33 | /** |
||
| 34 | * Used files |
||
| 35 | * @var GeneratorFiles |
||
| 36 | */ |
||
| 37 | private $files; |
||
| 38 | /** |
||
| 39 | * Used containers |
||
| 40 | * @var GeneratorContainers |
||
| 41 | */ |
||
| 42 | private $containers; |
||
| 43 | /** |
||
| 44 | * Used SoapClient |
||
| 45 | * @var GeneratorSoapClient |
||
| 46 | */ |
||
| 47 | private $soapClient; |
||
| 48 | /** |
||
| 49 | * Constructor |
||
| 50 | * @param GeneratorOptions $options |
||
| 51 | */ |
||
| 52 | 504 | public function __construct(GeneratorOptions $options) |
|
| 58 | /** |
||
| 59 | * @return Generator |
||
| 60 | */ |
||
| 61 | 504 | protected function initialize() |
|
| 70 | /** |
||
| 71 | * @throws \InvalidArgumentException |
||
| 72 | * @return Generator |
||
| 73 | */ |
||
| 74 | 504 | protected function initSoapClient() |
|
| 81 | /** |
||
| 82 | * @return Generator |
||
| 83 | */ |
||
| 84 | 504 | protected function initContainers() |
|
| 91 | /** |
||
| 92 | * @return Generator |
||
| 93 | */ |
||
| 94 | 504 | protected function initParsers() |
|
| 101 | /** |
||
| 102 | * @return GeneratorParsers |
||
| 103 | */ |
||
| 104 | 180 | public function getParsers() |
|
| 105 | { |
||
| 106 | 180 | return $this->parsers; |
|
| 107 | } |
||
| 108 | /** |
||
| 109 | * @return Generator |
||
| 110 | */ |
||
| 111 | 504 | protected function initFiles() |
|
| 118 | /** |
||
| 119 | * @return GeneratorFiles |
||
| 120 | */ |
||
| 121 | 44 | public function getFiles() |
|
| 125 | /** |
||
| 126 | * @throws \InvalidArgumentException |
||
| 127 | * @return Generator |
||
| 128 | */ |
||
| 129 | 24 | protected function initDirectory() |
|
| 137 | /** |
||
| 138 | * @return Generator |
||
| 139 | */ |
||
| 140 | 504 | protected function initWsdl() |
|
| 145 | /** |
||
| 146 | * @return Generator |
||
| 147 | */ |
||
| 148 | 32 | protected function doSanityChecks() |
|
| 160 | /** |
||
| 161 | * @return Generator |
||
| 162 | */ |
||
| 163 | 20 | protected function doParse() |
|
| 168 | /** |
||
| 169 | * @return Generator |
||
| 170 | */ |
||
| 171 | 20 | protected function doGenerate() |
|
| 176 | /** |
||
| 177 | * Generates all classes based on options |
||
| 178 | * @return Generator |
||
| 179 | */ |
||
| 180 | 32 | public function generatePackage() |
|
| 188 | /** |
||
| 189 | * Gets the struct by its name |
||
| 190 | * @uses Generator::getStructs() |
||
| 191 | * @param string $structName the original struct name |
||
| 192 | * @return Struct|null |
||
| 193 | */ |
||
| 194 | 376 | public function getStruct($structName) |
|
| 198 | /** |
||
| 199 | * Gets a service by its name |
||
| 200 | * @param string $serviceName the service name |
||
| 201 | * @return Service|null |
||
| 202 | */ |
||
| 203 | 332 | public function getService($serviceName) |
|
| 207 | /** |
||
| 208 | * Returns the method |
||
| 209 | * @uses Generator::getServiceName() |
||
| 210 | * @uses Generator::getService() |
||
| 211 | * @uses Service::getMethod() |
||
| 212 | * @param string $methodName the original function name |
||
| 213 | * @return Method|null |
||
| 214 | */ |
||
| 215 | 328 | public function getServiceMethod($methodName) |
|
| 219 | /** |
||
| 220 | * @return ServiceContainer |
||
| 221 | */ |
||
| 222 | 464 | public function getServices() |
|
| 226 | /** |
||
| 227 | * @return StructContainer |
||
| 228 | */ |
||
| 229 | 444 | public function getStructs() |
|
| 233 | /** |
||
| 234 | * Sets the optionCategory value |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 280 | public function getOptionCategory() |
|
| 241 | /** |
||
| 242 | * Sets the optionCategory value |
||
| 243 | * @param string $category |
||
| 244 | * @return Generator |
||
| 245 | */ |
||
| 246 | 272 | public function setOptionCategory($category) |
|
| 251 | /** |
||
| 252 | * Sets the optionGatherMethods value |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 472 | public function getOptionGatherMethods() |
|
| 259 | /** |
||
| 260 | * Sets the optionGatherMethods value |
||
| 261 | * @param string $gatherMethods |
||
| 262 | * @return Generator |
||
| 263 | */ |
||
| 264 | 272 | public function setOptionGatherMethods($gatherMethods) |
|
| 269 | /** |
||
| 270 | * Gets the optionGenericConstantsNames value |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 60 | public function getOptionGenericConstantsNames() |
|
| 277 | /** |
||
| 278 | * Sets the optionGenericConstantsNames value |
||
| 279 | * @param bool $genericConstantsNames |
||
| 280 | * @return Generator |
||
| 281 | */ |
||
| 282 | 8 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
| 287 | /** |
||
| 288 | * Gets the optionGenerateTutorialFile value |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 28 | public function getOptionGenerateTutorialFile() |
|
| 295 | /** |
||
| 296 | * Sets the optionGenerateTutorialFile value |
||
| 297 | * @param bool $generateTutorialFile |
||
| 298 | * @return Generator |
||
| 299 | */ |
||
| 300 | 4 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
| 305 | /** |
||
| 306 | * Gets the optionNamespacePrefix value |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 256 | public function getOptionNamespacePrefix() |
|
| 313 | /** |
||
| 314 | * Sets the optionGenerateTutorialFile value |
||
| 315 | * @param string $namespace |
||
| 316 | * @return Generator |
||
| 317 | */ |
||
| 318 | 20 | public function setOptionNamespacePrefix($namespace) |
|
| 323 | /** |
||
| 324 | * Gets the optionAddComments value |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 204 | public function getOptionAddComments() |
|
| 331 | /** |
||
| 332 | * Sets the optionAddComments value |
||
| 333 | * @param array $addComments |
||
| 334 | * @return Generator |
||
| 335 | */ |
||
| 336 | 272 | public function setOptionAddComments($addComments) |
|
| 341 | /** |
||
| 342 | * Gets the optionStandalone value |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 60 | public function getOptionStandalone() |
|
| 349 | /** |
||
| 350 | * Sets the optionStandalone value |
||
| 351 | * @param bool $standalone |
||
| 352 | * @return Generator |
||
| 353 | */ |
||
| 354 | 8 | public function setOptionStandalone($standalone) |
|
| 359 | /** |
||
| 360 | * Gets the optionValidation value |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | 112 | public function getOptionValidation() |
|
| 364 | { |
||
| 365 | 112 | return $this->options->getValidation(); |
|
| 366 | } |
||
| 367 | /** |
||
| 368 | * Sets the optionValidation value |
||
| 369 | * @param bool $validation |
||
| 370 | * @return Generator |
||
| 371 | */ |
||
| 372 | 12 | public function setOptionValidation($validation) |
|
| 373 | { |
||
| 374 | 12 | $this->options->setValidation($validation); |
|
| 375 | 12 | return $this; |
|
| 376 | } |
||
| 377 | /** |
||
| 378 | * Gets the optionStructClass value |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | 92 | public function getOptionStructClass() |
|
| 385 | /** |
||
| 386 | * Sets the optionStructClass value |
||
| 387 | * @param string $structClass |
||
| 388 | * @return Generator |
||
| 389 | */ |
||
| 390 | 12 | public function setOptionStructClass($structClass) |
|
| 395 | /** |
||
| 396 | * Gets the optionStructArrayClass value |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 36 | public function getOptionStructArrayClass() |
|
| 403 | /** |
||
| 404 | * Sets the optionStructArrayClass value |
||
| 405 | * @param string $structArrayClass |
||
| 406 | * @return Generator |
||
| 407 | */ |
||
| 408 | 4 | public function setOptionStructArrayClass($structArrayClass) |
|
| 413 | /** |
||
| 414 | * Gets the optionSoapClientClass value |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 92 | public function getOptionSoapClientClass() |
|
| 421 | /** |
||
| 422 | * Sets the optionSoapClientClass value |
||
| 423 | * @param string $soapClientClass |
||
| 424 | * @return Generator |
||
| 425 | */ |
||
| 426 | 4 | public function setOptionSoapClientClass($soapClientClass) |
|
| 431 | /** |
||
| 432 | * Gets the package name prefix |
||
| 433 | * @param bool $ucFirst ucfirst package name prefix or not |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | 536 | public function getOptionPrefix($ucFirst = true) |
|
| 440 | /** |
||
| 441 | * Sets the package name prefix |
||
| 442 | * @param string $optionPrefix |
||
| 443 | * @return Generator |
||
| 444 | */ |
||
| 445 | 296 | public function setOptionPrefix($optionPrefix) |
|
| 450 | /** |
||
| 451 | * Gets the package name suffix |
||
| 452 | * @param bool $ucFirst ucfirst package name suffix or not |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | 520 | public function getOptionSuffix($ucFirst = true) |
|
| 459 | /** |
||
| 460 | * Sets the package name suffix |
||
| 461 | * @param string $optionSuffix |
||
| 462 | * @return Generator |
||
| 463 | */ |
||
| 464 | 28 | public function setOptionSuffix($optionSuffix) |
|
| 469 | /** |
||
| 470 | * Gets the optionBasicLogin value |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 520 | public function getOptionBasicLogin() |
|
| 477 | /** |
||
| 478 | * Sets the optionBasicLogin value |
||
| 479 | * @param string $optionBasicLogin |
||
| 480 | * @return Generator |
||
| 481 | */ |
||
| 482 | 4 | public function setOptionBasicLogin($optionBasicLogin) |
|
| 487 | /** |
||
| 488 | * Gets the optionBasicPassword value |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | 520 | public function getOptionBasicPassword() |
|
| 495 | /** |
||
| 496 | * Sets the optionBasicPassword value |
||
| 497 | * @param string $optionBasicPassword |
||
| 498 | * @return Generator |
||
| 499 | */ |
||
| 500 | 4 | public function setOptionBasicPassword($optionBasicPassword) |
|
| 505 | /** |
||
| 506 | * Gets the optionProxyHost value |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | 520 | public function getOptionProxyHost() |
|
| 513 | /** |
||
| 514 | * Sets the optionProxyHost value |
||
| 515 | * @param string $optionProxyHost |
||
| 516 | * @return Generator |
||
| 517 | */ |
||
| 518 | 4 | public function setOptionProxyHost($optionProxyHost) |
|
| 523 | /** |
||
| 524 | * Gets the optionProxyPort value |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | 520 | public function getOptionProxyPort() |
|
| 531 | /** |
||
| 532 | * Sets the optionProxyPort value |
||
| 533 | * @param string $optionProxyPort |
||
| 534 | * @return Generator |
||
| 535 | */ |
||
| 536 | 4 | public function setOptionProxyPort($optionProxyPort) |
|
| 541 | /** |
||
| 542 | * Gets the optionProxyLogin value |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | 520 | public function getOptionProxyLogin() |
|
| 549 | /** |
||
| 550 | * Sets the optionProxyLogin value |
||
| 551 | * @param string $optionProxyLogin |
||
| 552 | * @return Generator |
||
| 553 | */ |
||
| 554 | 4 | public function setOptionProxyLogin($optionProxyLogin) |
|
| 559 | /** |
||
| 560 | * Gets the optionProxyPassword value |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | 520 | public function getOptionProxyPassword() |
|
| 567 | /** |
||
| 568 | * Sets the optionProxyPassword value |
||
| 569 | * @param string $optionProxyPassword |
||
| 570 | * @return Generator |
||
| 571 | */ |
||
| 572 | 4 | public function setOptionProxyPassword($optionProxyPassword) |
|
| 577 | /** |
||
| 578 | * Gets the optionOrigin value |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | 516 | public function getOptionOrigin() |
|
| 585 | /** |
||
| 586 | * Sets the optionOrigin value |
||
| 587 | * @param string $optionOrigin |
||
| 588 | * @return Generator |
||
| 589 | */ |
||
| 590 | 4 | public function setOptionOrigin($optionOrigin) |
|
| 596 | /** |
||
| 597 | * Gets the optionDestination value |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | 296 | public function getOptionDestination() |
|
| 608 | /** |
||
| 609 | * Sets the optionDestination value |
||
| 610 | * @param string $optionDestination |
||
| 611 | * @return Generator |
||
| 612 | */ |
||
| 613 | 12 | public function setOptionDestination($optionDestination) |
|
| 622 | /** |
||
| 623 | * Gets the optionSoapOptions value |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | 512 | public function getOptionSoapOptions() |
|
| 630 | /** |
||
| 631 | * Sets the optionSoapOptions value |
||
| 632 | * @param array $optionSoapOptions |
||
| 633 | * @return Generator |
||
| 634 | */ |
||
| 635 | 4 | public function setOptionSoapOptions($optionSoapOptions) |
|
| 643 | /** |
||
| 644 | * Gets the optionComposerName value |
||
| 645 | * @return string |
||
| 646 | */ |
||
| 647 | 40 | public function getOptionComposerName() |
|
| 651 | /** |
||
| 652 | * Sets the optionComposerName value |
||
| 653 | * @param string $optionComposerName |
||
| 654 | * @return Generator |
||
| 655 | */ |
||
| 656 | 20 | public function setOptionComposerName($optionComposerName) |
|
| 665 | /** |
||
| 666 | * Gets the optionComposerSettings value |
||
| 667 | * @return array |
||
| 668 | */ |
||
| 669 | 28 | public function getOptionComposerSettings() |
|
| 673 | /** |
||
| 674 | * Sets the optionComposerSettings value |
||
| 675 | * @param array $optionComposerSettings |
||
| 676 | * @return Generator |
||
| 677 | */ |
||
| 678 | 8 | public function setOptionComposerSettings(array $optionComposerSettings = array()) |
|
| 683 | /** |
||
| 684 | * Gets the optionStructsFolder value |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | 260 | public function getOptionStructsFolder() |
|
| 691 | /** |
||
| 692 | * Sets the optionStructsFolder value |
||
| 693 | * @param string $optionStructsFolder |
||
| 694 | * @return Generator |
||
| 695 | */ |
||
| 696 | 4 | public function setOptionStructsFolder($optionStructsFolder) |
|
| 701 | /** |
||
| 702 | * Gets the optionArraysFolder value |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | 52 | public function getOptionArraysFolder() |
|
| 709 | /** |
||
| 710 | * Sets the optionArraysFolder value |
||
| 711 | * @param string $optionArraysFolder |
||
| 712 | * @return Generator |
||
| 713 | */ |
||
| 714 | 4 | public function setOptionArraysFolder($optionArraysFolder) |
|
| 719 | /** |
||
| 720 | * Gets the optionEnumsFolder value |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | 92 | public function getOptionEnumsFolder() |
|
| 727 | /** |
||
| 728 | * Sets the optionEnumsFolder value |
||
| 729 | * @param string $optionEnumsFolder |
||
| 730 | * @return Generator |
||
| 731 | */ |
||
| 732 | 4 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
| 737 | /** |
||
| 738 | * Gets the optionServicesFolder value |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | 512 | public function getOptionServicesFolder() |
|
| 745 | /** |
||
| 746 | * Sets the optionServicesFolder value |
||
| 747 | * @param string $optionServicesFolder |
||
| 748 | * @return Generator |
||
| 749 | */ |
||
| 750 | 4 | public function setOptionServicesFolder($optionServicesFolder) |
|
| 755 | /** |
||
| 756 | * Gets the WSDL |
||
| 757 | * @return Wsdl|null |
||
| 758 | */ |
||
| 759 | 456 | public function getWsdl() |
|
| 763 | /** |
||
| 764 | * Sets the WSDLs |
||
| 765 | * @param Wsdl $wsdl |
||
| 766 | * @return Generator |
||
| 767 | */ |
||
| 768 | 500 | protected function setWsdl(Wsdl $wsdl) |
|
| 773 | /** |
||
| 774 | * Adds Wsdl location |
||
| 775 | * @param Wsdl $wsdl |
||
| 776 | * @param string $schemaLocation |
||
| 777 | * @return Generator |
||
| 778 | */ |
||
| 779 | 180 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
| 786 | /** |
||
| 787 | * Gets gather name class |
||
| 788 | * @param AbstractModel $model the model for which we generate the folder |
||
| 789 | * @return string |
||
| 790 | */ |
||
| 791 | 448 | private function getGather(AbstractModel $model) |
|
| 795 | /** |
||
| 796 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
| 797 | * @uses Generator::getGather() |
||
| 798 | * @param string $methodName original operation/method name |
||
| 799 | * @return string |
||
| 800 | */ |
||
| 801 | 464 | public function getServiceName($methodName) |
|
| 805 | /** |
||
| 806 | * @param GeneratorOptions $options |
||
| 807 | * @return Generator |
||
| 808 | */ |
||
| 809 | 504 | protected function setOptions(GeneratorOptions $options = null) |
|
| 814 | /** |
||
| 815 | * @return GeneratorOptions |
||
| 816 | */ |
||
| 817 | 548 | public function getOptions() |
|
| 821 | /** |
||
| 822 | * @return GeneratorSoapClient |
||
| 823 | */ |
||
| 824 | 484 | public function getSoapClient() |
|
| 828 | /** |
||
| 829 | * @param string $url |
||
| 830 | * @return string |
||
| 831 | */ |
||
| 832 | 508 | public function getUrlContent($url) |
|
| 841 | } |
||
| 842 |