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 implements \JsonSerializable |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Wsdl |
||
| 21 | * @var Wsdl |
||
| 22 | */ |
||
| 23 | protected $wsdl; |
||
| 24 | /** |
||
| 25 | * @var GeneratorOptions |
||
| 26 | */ |
||
| 27 | protected $options; |
||
| 28 | /** |
||
| 29 | * Used parsers |
||
| 30 | * @var GeneratorParsers |
||
| 31 | */ |
||
| 32 | protected $parsers; |
||
| 33 | /** |
||
| 34 | * Used files |
||
| 35 | * @var GeneratorFiles |
||
| 36 | */ |
||
| 37 | protected $files; |
||
| 38 | /** |
||
| 39 | * Used containers |
||
| 40 | * @var GeneratorContainers |
||
| 41 | */ |
||
| 42 | protected $containers; |
||
| 43 | /** |
||
| 44 | * Used SoapClient |
||
| 45 | * @var GeneratorSoapClient |
||
| 46 | */ |
||
| 47 | protected $soapClient; |
||
| 48 | /** |
||
| 49 | * Constructor |
||
| 50 | * @param GeneratorOptions $options |
||
| 51 | */ |
||
| 52 | 918 | public function __construct(GeneratorOptions $options) |
|
| 56 | /** |
||
| 57 | * @return Generator |
||
| 58 | */ |
||
| 59 | 918 | protected function initialize() |
|
| 67 | /** |
||
| 68 | * @throws \InvalidArgumentException |
||
| 69 | * @return Generator |
||
| 70 | */ |
||
| 71 | 918 | protected function initSoapClient() |
|
| 78 | /** |
||
| 79 | * @return Generator |
||
| 80 | */ |
||
| 81 | 918 | protected function initContainers() |
|
| 88 | /** |
||
| 89 | * @return Generator |
||
| 90 | */ |
||
| 91 | 918 | protected function initParsers() |
|
| 98 | /** |
||
| 99 | * @return GeneratorParsers |
||
| 100 | */ |
||
| 101 | 180 | public function getParsers() |
|
| 105 | /** |
||
| 106 | * @return Generator |
||
| 107 | */ |
||
| 108 | 918 | protected function initFiles() |
|
| 115 | /** |
||
| 116 | * @return GeneratorFiles |
||
| 117 | */ |
||
| 118 | 78 | public function getFiles() |
|
| 122 | /** |
||
| 123 | * @throws \InvalidArgumentException |
||
| 124 | * @return Generator |
||
| 125 | */ |
||
| 126 | 42 | protected function initDirectory() |
|
| 134 | /** |
||
| 135 | * @return Generator |
||
| 136 | */ |
||
| 137 | 912 | protected function initWsdl() |
|
| 142 | /** |
||
| 143 | * @return Generator |
||
| 144 | */ |
||
| 145 | 54 | protected function doSanityChecks() |
|
| 157 | /** |
||
| 158 | * @return Generator |
||
| 159 | */ |
||
| 160 | 48 | protected function doParse() |
|
| 165 | /** |
||
| 166 | * @return Generator |
||
| 167 | */ |
||
| 168 | 36 | protected function doGenerate() |
|
| 173 | /** |
||
| 174 | * Generates all classes based on options |
||
| 175 | * @return Generator |
||
| 176 | */ |
||
| 177 | 54 | public function generatePackage() |
|
| 185 | /** |
||
| 186 | * Only parses what has to be parsed, called before actually generating the package |
||
| 187 | * @return Generator |
||
| 188 | */ |
||
| 189 | 48 | public function parse() |
|
| 193 | /** |
||
| 194 | * Gets the struct by its name |
||
| 195 | * Starting from issue #157, we know call getVirtual secondly as structs are now betterly parsed and so is their inheritance/type is detected |
||
| 196 | * @uses Generator::getStructs() |
||
| 197 | * @param string $structName the original struct name |
||
| 198 | * @return Struct|null |
||
| 199 | */ |
||
| 200 | 672 | public function getStructByName($structName) |
|
| 205 | /** |
||
| 206 | * Gets the struct by its name and type |
||
| 207 | * @uses Generator::getStructs() |
||
| 208 | * @param string $structName the original struct name |
||
| 209 | * @param string $type the original struct type |
||
| 210 | * @return Struct|null |
||
| 211 | */ |
||
| 212 | 342 | public function getStructByNameAndType($structName, $type) |
|
| 216 | /** |
||
| 217 | * Gets a service by its name |
||
| 218 | * @param string $serviceName the service name |
||
| 219 | * @return Service|null |
||
| 220 | */ |
||
| 221 | 222 | public function getService($serviceName) |
|
| 225 | /** |
||
| 226 | * Returns the method |
||
| 227 | * @uses Generator::getServiceName() |
||
| 228 | * @uses Generator::getService() |
||
| 229 | * @uses Service::getMethod() |
||
| 230 | * @param string $methodName the original function name |
||
| 231 | * @return Method|null |
||
| 232 | */ |
||
| 233 | 210 | public function getServiceMethod($methodName) |
|
| 237 | /** |
||
| 238 | * @param bool $usingGatherMethods allows to gather methods within a single service if gather_methods options is set to true |
||
| 239 | * @return ServiceContainer |
||
| 240 | */ |
||
| 241 | 486 | public function getServices($usingGatherMethods = false) |
|
| 257 | /** |
||
| 258 | * @return StructContainer |
||
| 259 | */ |
||
| 260 | 786 | public function getStructs() |
|
| 264 | /** |
||
| 265 | * Sets the optionCategory value |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 486 | public function getOptionCategory() |
|
| 272 | /** |
||
| 273 | * Sets the optionCategory value |
||
| 274 | * @param string $category |
||
| 275 | * @return Generator |
||
| 276 | */ |
||
| 277 | 12 | public function setOptionCategory($category) |
|
| 282 | /** |
||
| 283 | * Sets the optionGatherMethods value |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 486 | public function getOptionGatherMethods() |
|
| 290 | /** |
||
| 291 | * Sets the optionGatherMethods value |
||
| 292 | * @param string $gatherMethods |
||
| 293 | * @return Generator |
||
| 294 | */ |
||
| 295 | 6 | public function setOptionGatherMethods($gatherMethods) |
|
| 300 | /** |
||
| 301 | * Gets the optionGenericConstantsNames value |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | 96 | public function getOptionGenericConstantsNames() |
|
| 308 | /** |
||
| 309 | * Sets the optionGenericConstantsNames value |
||
| 310 | * @param bool $genericConstantsNames |
||
| 311 | * @return Generator |
||
| 312 | */ |
||
| 313 | 12 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
| 318 | /** |
||
| 319 | * Gets the optionGenerateTutorialFile value |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | 48 | public function getOptionGenerateTutorialFile() |
|
| 326 | /** |
||
| 327 | * Sets the optionGenerateTutorialFile value |
||
| 328 | * @param bool $generateTutorialFile |
||
| 329 | * @return Generator |
||
| 330 | */ |
||
| 331 | 6 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
| 336 | /** |
||
| 337 | * Gets the optionNamespacePrefix value |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 450 | public function getOptionNamespacePrefix() |
|
| 344 | /** |
||
| 345 | * Sets the optionGenerateTutorialFile value |
||
| 346 | * @param string $namespace |
||
| 347 | * @return Generator |
||
| 348 | */ |
||
| 349 | 30 | public function setOptionNamespacePrefix($namespace) |
|
| 354 | /** |
||
| 355 | * Gets the optionAddComments value |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | 354 | public function getOptionAddComments() |
|
| 362 | /** |
||
| 363 | * Sets the optionAddComments value |
||
| 364 | * @param array $addComments |
||
| 365 | * @return Generator |
||
| 366 | */ |
||
| 367 | 6 | public function setOptionAddComments($addComments) |
|
| 372 | /** |
||
| 373 | * Gets the optionStandalone value |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 102 | public function getOptionStandalone() |
|
| 380 | /** |
||
| 381 | * Sets the optionStandalone value |
||
| 382 | * @param bool $standalone |
||
| 383 | * @return Generator |
||
| 384 | */ |
||
| 385 | 18 | public function setOptionStandalone($standalone) |
|
| 390 | /** |
||
| 391 | * Gets the optionValidation value |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | 210 | public function getOptionValidation() |
|
| 398 | /** |
||
| 399 | * Sets the optionValidation value |
||
| 400 | * @param bool $validation |
||
| 401 | * @return Generator |
||
| 402 | */ |
||
| 403 | 42 | public function setOptionValidation($validation) |
|
| 408 | /** |
||
| 409 | * Gets the optionStructClass value |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 180 | public function getOptionStructClass() |
|
| 416 | /** |
||
| 417 | * Sets the optionStructClass value |
||
| 418 | * @param string $structClass |
||
| 419 | * @return Generator |
||
| 420 | */ |
||
| 421 | 18 | public function setOptionStructClass($structClass) |
|
| 426 | /** |
||
| 427 | * Gets the optionStructArrayClass value |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 60 | public function getOptionStructArrayClass() |
|
| 434 | /** |
||
| 435 | * Sets the optionStructArrayClass value |
||
| 436 | * @param string $structArrayClass |
||
| 437 | * @return Generator |
||
| 438 | */ |
||
| 439 | 6 | public function setOptionStructArrayClass($structArrayClass) |
|
| 444 | /** |
||
| 445 | * Gets the optionSoapClientClass value |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | 150 | public function getOptionSoapClientClass() |
|
| 452 | /** |
||
| 453 | * Sets the optionSoapClientClass value |
||
| 454 | * @param string $soapClientClass |
||
| 455 | * @return Generator |
||
| 456 | */ |
||
| 457 | 6 | public function setOptionSoapClientClass($soapClientClass) |
|
| 462 | /** |
||
| 463 | * Gets the package name prefix |
||
| 464 | * @param bool $ucFirst ucfirst package name prefix or not |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 930 | public function getOptionPrefix($ucFirst = true) |
|
| 471 | /** |
||
| 472 | * Sets the package name prefix |
||
| 473 | * @param string $optionPrefix |
||
| 474 | * @return Generator |
||
| 475 | */ |
||
| 476 | 126 | public function setOptionPrefix($optionPrefix) |
|
| 481 | /** |
||
| 482 | * Gets the package name suffix |
||
| 483 | * @param bool $ucFirst ucfirst package name suffix or not |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | 894 | public function getOptionSuffix($ucFirst = true) |
|
| 490 | /** |
||
| 491 | * Sets the package name suffix |
||
| 492 | * @param string $optionSuffix |
||
| 493 | * @return Generator |
||
| 494 | */ |
||
| 495 | 42 | public function setOptionSuffix($optionSuffix) |
|
| 500 | /** |
||
| 501 | * Gets the optionBasicLogin value |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | 942 | public function getOptionBasicLogin() |
|
| 508 | /** |
||
| 509 | * Sets the optionBasicLogin value |
||
| 510 | * @param string $optionBasicLogin |
||
| 511 | * @return Generator |
||
| 512 | */ |
||
| 513 | 6 | public function setOptionBasicLogin($optionBasicLogin) |
|
| 518 | /** |
||
| 519 | * Gets the optionBasicPassword value |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | 942 | public function getOptionBasicPassword() |
|
| 526 | /** |
||
| 527 | * Sets the optionBasicPassword value |
||
| 528 | * @param string $optionBasicPassword |
||
| 529 | * @return Generator |
||
| 530 | */ |
||
| 531 | 6 | public function setOptionBasicPassword($optionBasicPassword) |
|
| 536 | /** |
||
| 537 | * Gets the optionProxyHost value |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | 942 | public function getOptionProxyHost() |
|
| 544 | /** |
||
| 545 | * Sets the optionProxyHost value |
||
| 546 | * @param string $optionProxyHost |
||
| 547 | * @return Generator |
||
| 548 | */ |
||
| 549 | 6 | public function setOptionProxyHost($optionProxyHost) |
|
| 554 | /** |
||
| 555 | * Gets the optionProxyPort value |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | 942 | public function getOptionProxyPort() |
|
| 562 | /** |
||
| 563 | * Sets the optionProxyPort value |
||
| 564 | * @param string $optionProxyPort |
||
| 565 | * @return Generator |
||
| 566 | */ |
||
| 567 | 6 | public function setOptionProxyPort($optionProxyPort) |
|
| 572 | /** |
||
| 573 | * Gets the optionProxyLogin value |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | 942 | public function getOptionProxyLogin() |
|
| 580 | /** |
||
| 581 | * Sets the optionProxyLogin value |
||
| 582 | * @param string $optionProxyLogin |
||
| 583 | * @return Generator |
||
| 584 | */ |
||
| 585 | 6 | public function setOptionProxyLogin($optionProxyLogin) |
|
| 590 | /** |
||
| 591 | * Gets the optionProxyPassword value |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | 942 | public function getOptionProxyPassword() |
|
| 598 | /** |
||
| 599 | * Sets the optionProxyPassword value |
||
| 600 | * @param string $optionProxyPassword |
||
| 601 | * @return Generator |
||
| 602 | */ |
||
| 603 | 6 | public function setOptionProxyPassword($optionProxyPassword) |
|
| 608 | /** |
||
| 609 | * Gets the optionOrigin value |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | 936 | public function getOptionOrigin() |
|
| 616 | /** |
||
| 617 | * Sets the optionOrigin value |
||
| 618 | * @param string $optionOrigin |
||
| 619 | * @return Generator |
||
| 620 | */ |
||
| 621 | 6 | public function setOptionOrigin($optionOrigin) |
|
| 627 | /** |
||
| 628 | * Gets the optionDestination value |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | 522 | public function getOptionDestination() |
|
| 639 | /** |
||
| 640 | * Sets the optionDestination value |
||
| 641 | * @param string $optionDestination |
||
| 642 | * @return Generator |
||
| 643 | */ |
||
| 644 | 18 | public function setOptionDestination($optionDestination) |
|
| 653 | /** |
||
| 654 | * Gets the optionSrcDirname value |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | 444 | public function getOptionSrcDirname() |
|
| 661 | /** |
||
| 662 | * Sets the optionSrcDirname value |
||
| 663 | * @param string $optionSrcDirname |
||
| 664 | * @return Generator |
||
| 665 | */ |
||
| 666 | 18 | public function setOptionSrcDirname($optionSrcDirname) |
|
| 671 | /** |
||
| 672 | * Gets the optionSoapOptions value |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | 930 | public function getOptionSoapOptions() |
|
| 679 | /** |
||
| 680 | * Sets the optionSoapOptions value |
||
| 681 | * @param array $optionSoapOptions |
||
| 682 | * @return Generator |
||
| 683 | */ |
||
| 684 | 6 | public function setOptionSoapOptions($optionSoapOptions) |
|
| 692 | /** |
||
| 693 | * Gets the optionComposerName value |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | 78 | public function getOptionComposerName() |
|
| 700 | /** |
||
| 701 | * Sets the optionComposerName value |
||
| 702 | * @param string $optionComposerName |
||
| 703 | * @return Generator |
||
| 704 | */ |
||
| 705 | 42 | public function setOptionComposerName($optionComposerName) |
|
| 714 | /** |
||
| 715 | * Gets the optionComposerSettings value |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | 60 | public function getOptionComposerSettings() |
|
| 722 | /** |
||
| 723 | * Sets the optionComposerSettings value |
||
| 724 | * @param array $optionComposerSettings |
||
| 725 | * @return Generator |
||
| 726 | */ |
||
| 727 | 12 | public function setOptionComposerSettings(array $optionComposerSettings = []) |
|
| 732 | /** |
||
| 733 | * Gets the optionStructsFolder value |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | 450 | public function getOptionStructsFolder() |
|
| 740 | /** |
||
| 741 | * Sets the optionStructsFolder value |
||
| 742 | * @param string $optionStructsFolder |
||
| 743 | * @return Generator |
||
| 744 | */ |
||
| 745 | 6 | public function setOptionStructsFolder($optionStructsFolder) |
|
| 750 | /** |
||
| 751 | * Gets the optionArraysFolder value |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | 84 | public function getOptionArraysFolder() |
|
| 758 | /** |
||
| 759 | * Sets the optionArraysFolder value |
||
| 760 | * @param string $optionArraysFolder |
||
| 761 | * @return Generator |
||
| 762 | */ |
||
| 763 | 6 | public function setOptionArraysFolder($optionArraysFolder) |
|
| 768 | /** |
||
| 769 | * Gets the optionEnumsFolder value |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | 168 | public function getOptionEnumsFolder() |
|
| 776 | /** |
||
| 777 | * Sets the optionEnumsFolder value |
||
| 778 | * @param string $optionEnumsFolder |
||
| 779 | * @return Generator |
||
| 780 | */ |
||
| 781 | 6 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
| 786 | /** |
||
| 787 | * Gets the optionServicesFolder value |
||
| 788 | * @return string |
||
| 789 | */ |
||
| 790 | 888 | public function getOptionServicesFolder() |
|
| 794 | /** |
||
| 795 | * Sets the optionServicesFolder value |
||
| 796 | * @param string $optionServicesFolder |
||
| 797 | * @return Generator |
||
| 798 | */ |
||
| 799 | 6 | public function setOptionServicesFolder($optionServicesFolder) |
|
| 804 | /** |
||
| 805 | * Gets the optionSchemasSave value |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | 18 | public function getOptionSchemasSave() |
|
| 812 | /** |
||
| 813 | * Sets the optionSchemasSave value |
||
| 814 | * @param bool $optionSchemasSave |
||
| 815 | * @return Generator |
||
| 816 | */ |
||
| 817 | 12 | public function setOptionSchemasSave($optionSchemasSave) |
|
| 822 | /** |
||
| 823 | * Gets the optionSchemasFolder value |
||
| 824 | * @return string |
||
| 825 | */ |
||
| 826 | 12 | public function getOptionSchemasFolder() |
|
| 830 | /** |
||
| 831 | * Sets the optionSchemasFolder value |
||
| 832 | * @param string $optionSchemasFolder |
||
| 833 | * @return Generator |
||
| 834 | */ |
||
| 835 | 12 | public function setOptionSchemasFolder($optionSchemasFolder) |
|
| 840 | /** |
||
| 841 | * Gets the optionXsdTypesPath value |
||
| 842 | * @return string |
||
| 843 | */ |
||
| 844 | 240 | public function getOptionXsdTypesPath() |
|
| 848 | /** |
||
| 849 | * Sets the optionXsdTypesPath value |
||
| 850 | * @param string $xsdTypesPath |
||
| 851 | * @return Generator |
||
| 852 | */ |
||
| 853 | 6 | public function setOptionXsdTypesPath($xsdTypesPath) |
|
| 858 | /** |
||
| 859 | * Gets the WSDL |
||
| 860 | * @return Wsdl|null |
||
| 861 | */ |
||
| 862 | 702 | public function getWsdl() |
|
| 866 | /** |
||
| 867 | * Sets the WSDLs |
||
| 868 | * @param Wsdl $wsdl |
||
| 869 | * @return Generator |
||
| 870 | */ |
||
| 871 | 912 | protected function setWsdl(Wsdl $wsdl) |
|
| 876 | /** |
||
| 877 | * Adds Wsdl location |
||
| 878 | * @param Wsdl $wsdl |
||
| 879 | * @param string $schemaLocation |
||
| 880 | * @return Generator |
||
| 881 | */ |
||
| 882 | 180 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
| 889 | /** |
||
| 890 | * Gets gather name class |
||
| 891 | * @param AbstractModel $model the model for which we generate the folder |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | 426 | protected function getGather(AbstractModel $model) |
|
| 898 | /** |
||
| 899 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
| 900 | * @param string $methodName original operation/method name |
||
| 901 | * @return string |
||
| 902 | */ |
||
| 903 | 426 | public function getServiceName($methodName) |
|
| 907 | /** |
||
| 908 | * @param GeneratorOptions $options |
||
| 909 | * @return Generator |
||
| 910 | */ |
||
| 911 | 918 | protected function setOptions(GeneratorOptions $options = null) |
|
| 916 | /** |
||
| 917 | * @return GeneratorOptions |
||
| 918 | */ |
||
| 919 | 942 | public function getOptions() |
|
| 923 | /** |
||
| 924 | * @return GeneratorSoapClient |
||
| 925 | */ |
||
| 926 | 360 | public function getSoapClient() |
|
| 930 | /** |
||
| 931 | * @param string $url |
||
| 932 | * @return string |
||
| 933 | */ |
||
| 934 | 918 | public function getUrlContent($url) |
|
| 947 | /** |
||
| 948 | * @return GeneratorContainers |
||
| 949 | */ |
||
| 950 | 468 | public function getContainers() |
|
| 954 | /** |
||
| 955 | * @return array |
||
| 956 | */ |
||
| 957 | 6 | public function jsonSerialize() |
|
| 964 | /** |
||
| 965 | * @param string $json |
||
| 966 | * @throws \InvalidArgumentException |
||
| 967 | * @return Generator |
||
| 968 | */ |
||
| 969 | 468 | public static function instanceFromSerializedJson($json) |
|
| 993 | /** |
||
| 994 | * @param Generator $generator |
||
| 995 | * @param array $jsonArrayEntry |
||
| 996 | * @return AbstractModel |
||
| 997 | */ |
||
| 998 | 468 | protected static function getModelInstanceFromJsonArrayEntry(Generator $generator, array $jsonArrayEntry) |
|
| 1002 | } |
||
| 1003 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.