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 | 452 | public function __construct(GeneratorOptions $options) |
|
| 58 | /** |
||
| 59 | * @return Generator |
||
| 60 | */ |
||
| 61 | 452 | protected function initialize() |
|
| 70 | /** |
||
| 71 | * @throws \InvalidArgumentException |
||
| 72 | * @return Generator |
||
| 73 | */ |
||
| 74 | 452 | protected function initSoapClient() |
|
| 81 | /** |
||
| 82 | * @return Generator |
||
| 83 | */ |
||
| 84 | 452 | protected function initContainers() |
|
| 91 | /** |
||
| 92 | * @return Generator |
||
| 93 | */ |
||
| 94 | 452 | protected function initParsers() |
|
| 101 | /** |
||
| 102 | * @return GeneratorParsers |
||
| 103 | */ |
||
| 104 | 148 | public function getParsers() |
|
| 108 | /** |
||
| 109 | * @return Generator |
||
| 110 | */ |
||
| 111 | 452 | 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 | 452 | protected function initWsdl() |
|
| 145 | /** |
||
| 146 | * @return Generator |
||
| 147 | */ |
||
| 148 | 32 | protected function doSanityChecks() |
|
| 149 | { |
||
| 150 | 32 | $destination = $this->getOptionDestination(); |
|
| 151 | 32 | if (empty($destination)) { |
|
| 152 | 4 | throw new \InvalidArgumentException('Package\'s destination must be defined', __LINE__); |
|
| 153 | } |
||
| 154 | 28 | $composerName = $this->getOptionComposerName(); |
|
| 155 | 28 | if ($this->getOptionStandalone() && empty($composerName)) { |
|
| 156 | 4 | throw new \InvalidArgumentException('Package\'s composer name must be defined', __LINE__); |
|
| 157 | } |
||
| 158 | 24 | return $this; |
|
| 159 | } |
||
| 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 | 324 | 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 | 288 | 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 | 284 | public function getServiceMethod($methodName) |
|
| 219 | /** |
||
| 220 | * @return ServiceContainer |
||
| 221 | */ |
||
| 222 | 412 | public function getServices() |
|
| 226 | /** |
||
| 227 | * @return StructContainer |
||
| 228 | */ |
||
| 229 | 392 | public function getStructs() |
|
| 233 | /** |
||
| 234 | * Sets the optionCategory value |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 232 | public function getOptionCategory() |
|
| 241 | /** |
||
| 242 | * Sets the optionCategory value |
||
| 243 | * @param string $category |
||
| 244 | * @return Generator |
||
| 245 | */ |
||
| 246 | 228 | public function setOptionCategory($category) |
|
| 251 | /** |
||
| 252 | * Sets the optionGatherMethods value |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 420 | public function getOptionGatherMethods() |
|
| 259 | /** |
||
| 260 | * Sets the optionGatherMethods value |
||
| 261 | * @param string $gatherMethods |
||
| 262 | * @return Generator |
||
| 263 | */ |
||
| 264 | 228 | 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 | 208 | public function getOptionNamespacePrefix() |
|
| 313 | /** |
||
| 314 | * Sets the optionGenerateTutorialFile value |
||
| 315 | * @param string $namespace |
||
| 316 | * @return Generator |
||
| 317 | */ |
||
| 318 | 12 | public function setOptionNamespacePrefix($namespace) |
|
| 323 | /** |
||
| 324 | * Gets the optionAddComments value |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 168 | public function getOptionAddComments() |
|
| 331 | /** |
||
| 332 | * Sets the optionAddComments value |
||
| 333 | * @param array $addComments |
||
| 334 | * @return Generator |
||
| 335 | */ |
||
| 336 | 228 | 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 optionStructClass value |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | 68 | public function getOptionStructClass() |
|
| 367 | /** |
||
| 368 | * Sets the optionStructClass value |
||
| 369 | * @param string $structClass |
||
| 370 | * @return Generator |
||
| 371 | */ |
||
| 372 | 12 | public function setOptionStructClass($structClass) |
|
| 377 | /** |
||
| 378 | * Gets the optionStructArrayClass value |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | 36 | public function getOptionStructArrayClass() |
|
| 385 | /** |
||
| 386 | * Sets the optionStructArrayClass value |
||
| 387 | * @param string $structArrayClass |
||
| 388 | * @return Generator |
||
| 389 | */ |
||
| 390 | 4 | public function setOptionStructArrayClass($structArrayClass) |
|
| 395 | /** |
||
| 396 | * Gets the optionSoapClientClass value |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 80 | public function getOptionSoapClientClass() |
|
| 403 | /** |
||
| 404 | * Sets the optionSoapClientClass value |
||
| 405 | * @param string $soapClientClass |
||
| 406 | * @return Generator |
||
| 407 | */ |
||
| 408 | 4 | public function setOptionSoapClientClass($soapClientClass) |
|
| 413 | /** |
||
| 414 | * Gets the package name prefix |
||
| 415 | * @param bool $ucFirst ucfirst package name prefix or not |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | 476 | public function getOptionPrefix($ucFirst = true) |
|
| 422 | /** |
||
| 423 | * Sets the package name prefix |
||
| 424 | * @param string $optionPrefix |
||
| 425 | * @return Generator |
||
| 426 | */ |
||
| 427 | 248 | public function setOptionPrefix($optionPrefix) |
|
| 432 | /** |
||
| 433 | * Gets the package name suffix |
||
| 434 | * @param bool $ucFirst ucfirst package name suffix or not |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 464 | public function getOptionSuffix($ucFirst = true) |
|
| 441 | /** |
||
| 442 | * Sets the package name suffix |
||
| 443 | * @param string $optionSuffix |
||
| 444 | * @return Generator |
||
| 445 | */ |
||
| 446 | 28 | public function setOptionSuffix($optionSuffix) |
|
| 451 | /** |
||
| 452 | * Gets the optionBasicLogin value |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | 468 | public function getOptionBasicLogin() |
|
| 459 | /** |
||
| 460 | * Sets the optionBasicLogin value |
||
| 461 | * @param string $optionBasicLogin |
||
| 462 | * @return Generator |
||
| 463 | */ |
||
| 464 | 4 | public function setOptionBasicLogin($optionBasicLogin) |
|
| 469 | /** |
||
| 470 | * Gets the optionBasicPassword value |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 468 | public function getOptionBasicPassword() |
|
| 477 | /** |
||
| 478 | * Sets the optionBasicPassword value |
||
| 479 | * @param string $optionBasicPassword |
||
| 480 | * @return Generator |
||
| 481 | */ |
||
| 482 | 4 | public function setOptionBasicPassword($optionBasicPassword) |
|
| 487 | /** |
||
| 488 | * Gets the optionProxyHost value |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | 468 | public function getOptionProxyHost() |
|
| 495 | /** |
||
| 496 | * Sets the optionProxyHost value |
||
| 497 | * @param string $optionProxyHost |
||
| 498 | * @return Generator |
||
| 499 | */ |
||
| 500 | 4 | public function setOptionProxyHost($optionProxyHost) |
|
| 505 | /** |
||
| 506 | * Gets the optionProxyPort value |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | 468 | public function getOptionProxyPort() |
|
| 513 | /** |
||
| 514 | * Sets the optionProxyPort value |
||
| 515 | * @param string $optionProxyPort |
||
| 516 | * @return Generator |
||
| 517 | */ |
||
| 518 | 4 | public function setOptionProxyPort($optionProxyPort) |
|
| 523 | /** |
||
| 524 | * Gets the optionProxyLogin value |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | 468 | public function getOptionProxyLogin() |
|
| 531 | /** |
||
| 532 | * Sets the optionProxyLogin value |
||
| 533 | * @param string $optionProxyLogin |
||
| 534 | * @return Generator |
||
| 535 | */ |
||
| 536 | 4 | public function setOptionProxyLogin($optionProxyLogin) |
|
| 541 | /** |
||
| 542 | * Gets the optionProxyPassword value |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | 468 | public function getOptionProxyPassword() |
|
| 549 | /** |
||
| 550 | * Sets the optionProxyPassword value |
||
| 551 | * @param string $optionProxyPassword |
||
| 552 | * @return Generator |
||
| 553 | */ |
||
| 554 | 4 | public function setOptionProxyPassword($optionProxyPassword) |
|
| 559 | /** |
||
| 560 | * Gets the optionOrigin value |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | 464 | public function getOptionOrigin() |
|
| 567 | /** |
||
| 568 | * Sets the optionOrigin value |
||
| 569 | * @param string $optionOrigin |
||
| 570 | * @return Generator |
||
| 571 | */ |
||
| 572 | 4 | public function setOptionOrigin($optionOrigin) |
|
| 578 | /** |
||
| 579 | * Gets the optionDestination value |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | 248 | public function getOptionDestination() |
|
| 583 | { |
||
| 584 | 248 | $destination = $this->options->getDestination(); |
|
| 585 | 248 | if (!empty($destination)) { |
|
| 586 | 244 | $destination = realpath($this->options->getDestination()) . DIRECTORY_SEPARATOR; |
|
| 587 | 183 | } |
|
| 588 | 248 | return $destination; |
|
| 589 | } |
||
| 590 | /** |
||
| 591 | * Sets the optionDestination value |
||
| 592 | * @param string $optionDestination |
||
| 593 | * @return Generator |
||
| 594 | */ |
||
| 595 | 12 | public function setOptionDestination($optionDestination) |
|
| 596 | { |
||
| 597 | 12 | if (!empty($optionDestination)) { |
|
| 598 | 8 | $this->options->setDestination(realpath($optionDestination) . DIRECTORY_SEPARATOR); |
|
| 599 | 6 | } else { |
|
| 600 | 4 | throw new \InvalidArgumentException('Package\'s destination can\'t be empty', __LINE__); |
|
| 601 | } |
||
| 602 | 8 | return $this; |
|
| 603 | } |
||
| 604 | /** |
||
| 605 | * Gets the optionSoapOptions value |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | 460 | public function getOptionSoapOptions() |
|
| 612 | /** |
||
| 613 | * Sets the optionSoapOptions value |
||
| 614 | * @param array $optionSoapOptions |
||
| 615 | * @return Generator |
||
| 616 | */ |
||
| 617 | 4 | public function setOptionSoapOptions($optionSoapOptions) |
|
| 625 | /** |
||
| 626 | * Gets the optionComposerName value |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | 36 | public function getOptionComposerName() |
|
| 630 | { |
||
| 631 | 36 | return $this->options->getComposerName(); |
|
| 632 | } |
||
| 633 | /** |
||
| 634 | * Sets the optionComposerName value |
||
| 635 | * @param string $optionComposerName |
||
| 636 | * @return Generator |
||
| 637 | */ |
||
| 638 | 16 | public function setOptionComposerName($optionComposerName) |
|
| 639 | { |
||
| 640 | 16 | if (!empty($optionComposerName)) { |
|
| 641 | 12 | $this->options->setComposerName($optionComposerName); |
|
| 642 | 9 | } else { |
|
| 643 | 4 | throw new \InvalidArgumentException('Package\'s composer name can\'t be empty', __LINE__); |
|
| 644 | } |
||
| 645 | 12 | return $this; |
|
| 646 | } |
||
| 647 | /** |
||
| 648 | * Gets the optionStructsFolder value |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | 216 | public function getOptionStructsFolder() |
|
| 652 | { |
||
| 653 | 216 | return $this->options->getStructsFolder(); |
|
| 654 | } |
||
| 655 | /** |
||
| 656 | * Sets the optionStructsFolder value |
||
| 657 | * @param string $optionStructsFolder |
||
| 658 | * @return Generator |
||
| 659 | */ |
||
| 660 | 4 | public function setOptionStructsFolder($optionStructsFolder) |
|
| 661 | { |
||
| 662 | 4 | $this->options->setStructsFolder($optionStructsFolder); |
|
| 663 | 4 | return $this; |
|
| 664 | } |
||
| 665 | /** |
||
| 666 | * Gets the optionArraysFolder value |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | 48 | public function getOptionArraysFolder() |
|
| 670 | { |
||
| 671 | 48 | return $this->options->getArraysFolder(); |
|
| 672 | } |
||
| 673 | /** |
||
| 674 | * Sets the optionArraysFolder value |
||
| 675 | * @param string $optionArraysFolder |
||
| 676 | * @return Generator |
||
| 677 | */ |
||
| 678 | 4 | public function setOptionArraysFolder($optionArraysFolder) |
|
| 679 | { |
||
| 680 | 4 | $this->options->setArraysFolder($optionArraysFolder); |
|
| 681 | 4 | return $this; |
|
| 682 | } |
||
| 683 | /** |
||
| 684 | * Gets the optionEnumsFolder value |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | 80 | public function getOptionEnumsFolder() |
|
| 688 | { |
||
| 689 | 80 | return $this->options->getEnumsFolder(); |
|
| 690 | } |
||
| 691 | /** |
||
| 692 | * Sets the optionEnumsFolder value |
||
| 693 | * @param string $optionEnumsFolder |
||
| 694 | * @return Generator |
||
| 695 | */ |
||
| 696 | 4 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
| 697 | { |
||
| 698 | 4 | $this->options->setEnumsFolder($optionEnumsFolder); |
|
| 699 | 4 | return $this; |
|
| 700 | } |
||
| 701 | /** |
||
| 702 | * Gets the optionServicesFolder value |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | 456 | public function getOptionServicesFolder() |
|
| 706 | { |
||
| 707 | 456 | return $this->options->getServicesFolder(); |
|
| 708 | } |
||
| 709 | /** |
||
| 710 | * Sets the optionServicesFolder value |
||
| 711 | * @param string $optionServicesFolder |
||
| 712 | * @return Generator |
||
| 713 | */ |
||
| 714 | 4 | public function setOptionServicesFolder($optionServicesFolder) |
|
| 715 | { |
||
| 716 | 4 | $this->options->setServicesFolder($optionServicesFolder); |
|
| 717 | 4 | return $this; |
|
| 718 | } |
||
| 719 | /** |
||
| 720 | * Gets the WSDL |
||
| 721 | * @return Wsdl|null |
||
| 722 | */ |
||
| 723 | 400 | public function getWsdl() |
|
| 727 | /** |
||
| 728 | * Sets the WSDLs |
||
| 729 | * @param Wsdl $wsdl |
||
| 730 | * @return Generator |
||
| 731 | */ |
||
| 732 | 448 | protected function setWsdl(Wsdl $wsdl) |
|
| 737 | /** |
||
| 738 | * Adds Wsdl location |
||
| 739 | * @param Wsdl $wsdl |
||
| 740 | * @param string $schemaLocation |
||
| 741 | * @return Generator |
||
| 742 | */ |
||
| 743 | 148 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
| 750 | /** |
||
| 751 | * Gets gather name class |
||
| 752 | * @param AbstractModel $model the model for which we generate the folder |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | 396 | private function getGather(AbstractModel $model) |
|
| 759 | /** |
||
| 760 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
| 761 | * @uses Generator::getGather() |
||
| 762 | * @param string $methodName original operation/method name |
||
| 763 | * @return string |
||
| 764 | */ |
||
| 765 | 412 | public function getServiceName($methodName) |
|
| 769 | /** |
||
| 770 | * @param GeneratorOptions $options |
||
| 771 | * @return Generator |
||
| 772 | */ |
||
| 773 | 452 | protected function setOptions(GeneratorOptions $options = null) |
|
| 778 | /** |
||
| 779 | * @return GeneratorOptions |
||
| 780 | */ |
||
| 781 | 488 | public function getOptions() |
|
| 785 | /** |
||
| 786 | * @return GeneratorSoapClient |
||
| 787 | */ |
||
| 788 | 432 | public function getSoapClient() |
|
| 792 | /** |
||
| 793 | * @param string $url |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | 456 | public function getUrlContent($url) |
|
| 805 | } |
||
| 806 |