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 | 755 | public function __construct(GeneratorOptions $options) |
|
56 | /** |
||
57 | * @return Generator |
||
58 | */ |
||
59 | 755 | protected function initialize() |
|
67 | /** |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @return Generator |
||
70 | */ |
||
71 | 755 | protected function initSoapClient() |
|
78 | /** |
||
79 | * @return Generator |
||
80 | */ |
||
81 | 755 | protected function initContainers() |
|
88 | /** |
||
89 | * @return Generator |
||
90 | */ |
||
91 | 755 | protected function initParsers() |
|
98 | /** |
||
99 | * @return GeneratorParsers |
||
100 | */ |
||
101 | 150 | public function getParsers() |
|
105 | /** |
||
106 | * @return Generator |
||
107 | */ |
||
108 | 755 | protected function initFiles() |
|
115 | /** |
||
116 | * @return GeneratorFiles |
||
117 | */ |
||
118 | 65 | public function getFiles() |
|
122 | /** |
||
123 | * @throws \InvalidArgumentException |
||
124 | * @return Generator |
||
125 | */ |
||
126 | 35 | protected function initDirectory() |
|
134 | /** |
||
135 | * @return Generator |
||
136 | */ |
||
137 | 755 | protected function initWsdl() |
|
142 | /** |
||
143 | * @return Generator |
||
144 | */ |
||
145 | 45 | protected function doSanityChecks() |
|
157 | /** |
||
158 | * @return Generator |
||
159 | */ |
||
160 | 40 | protected function doParse() |
|
165 | /** |
||
166 | * @return Generator |
||
167 | */ |
||
168 | 30 | protected function doGenerate() |
|
173 | /** |
||
174 | * Generates all classes based on options |
||
175 | * @return Generator |
||
176 | */ |
||
177 | 45 | public function generatePackage() |
|
185 | /** |
||
186 | * Only parses what has to be parsed, called before actually generating the package |
||
187 | * @return Generator |
||
188 | */ |
||
189 | 40 | public function parse() |
|
193 | /** |
||
194 | * Gets the struct by its name |
||
195 | * @uses Generator::getStructs() |
||
196 | * @param string $structName the original struct name |
||
197 | * @return Struct|null |
||
198 | */ |
||
199 | 550 | public function getStruct($structName) |
|
203 | /** |
||
204 | * Gets a service by its name |
||
205 | * @param string $serviceName the service name |
||
206 | * @return Service|null |
||
207 | */ |
||
208 | 185 | public function getService($serviceName) |
|
212 | /** |
||
213 | * Returns the method |
||
214 | * @uses Generator::getServiceName() |
||
215 | * @uses Generator::getService() |
||
216 | * @uses Service::getMethod() |
||
217 | * @param string $methodName the original function name |
||
218 | * @return Method|null |
||
219 | */ |
||
220 | 175 | public function getServiceMethod($methodName) |
|
224 | /** |
||
225 | * @param bool $usingGatherMethods allows to gather methods within a single service if gather_methods options is set to true |
||
226 | * @return ServiceContainer |
||
227 | */ |
||
228 | 405 | public function getServices($usingGatherMethods = false) |
|
244 | /** |
||
245 | * @return StructContainer |
||
246 | */ |
||
247 | 640 | public function getStructs() |
|
251 | /** |
||
252 | * Sets the optionCategory value |
||
253 | * @return string |
||
254 | */ |
||
255 | 400 | public function getOptionCategory() |
|
259 | /** |
||
260 | * Sets the optionCategory value |
||
261 | * @param string $category |
||
262 | * @return Generator |
||
263 | */ |
||
264 | 10 | public function setOptionCategory($category) |
|
269 | /** |
||
270 | * Sets the optionGatherMethods value |
||
271 | * @return string |
||
272 | */ |
||
273 | 405 | public function getOptionGatherMethods() |
|
277 | /** |
||
278 | * Sets the optionGatherMethods value |
||
279 | * @param string $gatherMethods |
||
280 | * @return Generator |
||
281 | */ |
||
282 | 5 | public function setOptionGatherMethods($gatherMethods) |
|
287 | /** |
||
288 | * Gets the optionGenericConstantsNames value |
||
289 | * @return bool |
||
290 | */ |
||
291 | 80 | public function getOptionGenericConstantsNames() |
|
295 | /** |
||
296 | * Sets the optionGenericConstantsNames value |
||
297 | * @param bool $genericConstantsNames |
||
298 | * @return Generator |
||
299 | */ |
||
300 | 10 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
305 | /** |
||
306 | * Gets the optionGenerateTutorialFile value |
||
307 | * @return bool |
||
308 | */ |
||
309 | 40 | public function getOptionGenerateTutorialFile() |
|
313 | /** |
||
314 | * Sets the optionGenerateTutorialFile value |
||
315 | * @param bool $generateTutorialFile |
||
316 | * @return Generator |
||
317 | */ |
||
318 | 5 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
323 | /** |
||
324 | * Gets the optionNamespacePrefix value |
||
325 | * @return string |
||
326 | */ |
||
327 | 370 | public function getOptionNamespacePrefix() |
|
331 | /** |
||
332 | * Sets the optionGenerateTutorialFile value |
||
333 | * @param string $namespace |
||
334 | * @return Generator |
||
335 | */ |
||
336 | 25 | public function setOptionNamespacePrefix($namespace) |
|
341 | /** |
||
342 | * Gets the optionAddComments value |
||
343 | * @return array |
||
344 | */ |
||
345 | 290 | public function getOptionAddComments() |
|
349 | /** |
||
350 | * Sets the optionAddComments value |
||
351 | * @param array $addComments |
||
352 | * @return Generator |
||
353 | */ |
||
354 | 5 | public function setOptionAddComments($addComments) |
|
359 | /** |
||
360 | * Gets the optionStandalone value |
||
361 | * @return bool |
||
362 | */ |
||
363 | 85 | public function getOptionStandalone() |
|
367 | /** |
||
368 | * Sets the optionStandalone value |
||
369 | * @param bool $standalone |
||
370 | * @return Generator |
||
371 | */ |
||
372 | 15 | public function setOptionStandalone($standalone) |
|
377 | /** |
||
378 | * Gets the optionValidation value |
||
379 | * @return bool |
||
380 | */ |
||
381 | 170 | public function getOptionValidation() |
|
385 | /** |
||
386 | * Sets the optionValidation value |
||
387 | * @param bool $validation |
||
388 | * @return Generator |
||
389 | */ |
||
390 | 35 | public function setOptionValidation($validation) |
|
395 | /** |
||
396 | * Gets the optionStructClass value |
||
397 | * @return string |
||
398 | */ |
||
399 | 145 | public function getOptionStructClass() |
|
403 | /** |
||
404 | * Sets the optionStructClass value |
||
405 | * @param string $structClass |
||
406 | * @return Generator |
||
407 | */ |
||
408 | 15 | public function setOptionStructClass($structClass) |
|
413 | /** |
||
414 | * Gets the optionStructArrayClass value |
||
415 | * @return string |
||
416 | */ |
||
417 | 50 | public function getOptionStructArrayClass() |
|
421 | /** |
||
422 | * Sets the optionStructArrayClass value |
||
423 | * @param string $structArrayClass |
||
424 | * @return Generator |
||
425 | */ |
||
426 | 5 | public function setOptionStructArrayClass($structArrayClass) |
|
431 | /** |
||
432 | * Gets the optionSoapClientClass value |
||
433 | * @return string |
||
434 | */ |
||
435 | 125 | public function getOptionSoapClientClass() |
|
439 | /** |
||
440 | * Sets the optionSoapClientClass value |
||
441 | * @param string $soapClientClass |
||
442 | * @return Generator |
||
443 | */ |
||
444 | 5 | public function setOptionSoapClientClass($soapClientClass) |
|
449 | /** |
||
450 | * Gets the package name prefix |
||
451 | * @param bool $ucFirst ucfirst package name prefix or not |
||
452 | * @return string |
||
453 | */ |
||
454 | 765 | public function getOptionPrefix($ucFirst = true) |
|
458 | /** |
||
459 | * Sets the package name prefix |
||
460 | * @param string $optionPrefix |
||
461 | * @return Generator |
||
462 | */ |
||
463 | 105 | public function setOptionPrefix($optionPrefix) |
|
468 | /** |
||
469 | * Gets the package name suffix |
||
470 | * @param bool $ucFirst ucfirst package name suffix or not |
||
471 | * @return string |
||
472 | */ |
||
473 | 735 | public function getOptionSuffix($ucFirst = true) |
|
477 | /** |
||
478 | * Sets the package name suffix |
||
479 | * @param string $optionSuffix |
||
480 | * @return Generator |
||
481 | */ |
||
482 | 35 | public function setOptionSuffix($optionSuffix) |
|
487 | /** |
||
488 | * Gets the optionBasicLogin value |
||
489 | * @return string |
||
490 | */ |
||
491 | 775 | public function getOptionBasicLogin() |
|
495 | /** |
||
496 | * Sets the optionBasicLogin value |
||
497 | * @param string $optionBasicLogin |
||
498 | * @return Generator |
||
499 | */ |
||
500 | 5 | public function setOptionBasicLogin($optionBasicLogin) |
|
505 | /** |
||
506 | * Gets the optionBasicPassword value |
||
507 | * @return string |
||
508 | */ |
||
509 | 775 | public function getOptionBasicPassword() |
|
513 | /** |
||
514 | * Sets the optionBasicPassword value |
||
515 | * @param string $optionBasicPassword |
||
516 | * @return Generator |
||
517 | */ |
||
518 | 5 | public function setOptionBasicPassword($optionBasicPassword) |
|
519 | { |
||
520 | 5 | $this->options->setBasicPassword($optionBasicPassword); |
|
521 | 5 | return $this; |
|
522 | } |
||
523 | /** |
||
524 | * Gets the optionProxyHost value |
||
525 | * @return string |
||
526 | */ |
||
527 | 775 | public function getOptionProxyHost() |
|
528 | 1 | { |
|
529 | 775 | return $this->options->getProxyHost(); |
|
530 | } |
||
531 | /** |
||
532 | * Sets the optionProxyHost value |
||
533 | * @param string $optionProxyHost |
||
534 | * @return Generator |
||
535 | */ |
||
536 | 5 | public function setOptionProxyHost($optionProxyHost) |
|
541 | /** |
||
542 | * Gets the optionProxyPort value |
||
543 | * @return string |
||
544 | */ |
||
545 | 775 | public function getOptionProxyPort() |
|
549 | /** |
||
550 | * Sets the optionProxyPort value |
||
551 | * @param string $optionProxyPort |
||
552 | * @return Generator |
||
553 | */ |
||
554 | 5 | public function setOptionProxyPort($optionProxyPort) |
|
559 | /** |
||
560 | * Gets the optionProxyLogin value |
||
561 | * @return string |
||
562 | */ |
||
563 | 775 | public function getOptionProxyLogin() |
|
567 | /** |
||
568 | * Sets the optionProxyLogin value |
||
569 | * @param string $optionProxyLogin |
||
570 | * @return Generator |
||
571 | */ |
||
572 | 5 | public function setOptionProxyLogin($optionProxyLogin) |
|
577 | /** |
||
578 | * Gets the optionProxyPassword value |
||
579 | * @return string |
||
580 | */ |
||
581 | 775 | public function getOptionProxyPassword() |
|
585 | /** |
||
586 | * Sets the optionProxyPassword value |
||
587 | * @param string $optionProxyPassword |
||
588 | * @return Generator |
||
589 | */ |
||
590 | 5 | public function setOptionProxyPassword($optionProxyPassword) |
|
595 | /** |
||
596 | * Gets the optionOrigin value |
||
597 | * @return string |
||
598 | */ |
||
599 | 770 | public function getOptionOrigin() |
|
603 | /** |
||
604 | * Sets the optionOrigin value |
||
605 | * @param string $optionOrigin |
||
606 | * @return Generator |
||
607 | */ |
||
608 | 5 | public function setOptionOrigin($optionOrigin) |
|
614 | /** |
||
615 | * Gets the optionDestination value |
||
616 | * @return string |
||
617 | */ |
||
618 | 430 | public function getOptionDestination() |
|
626 | /** |
||
627 | * Sets the optionDestination value |
||
628 | * @param string $optionDestination |
||
629 | * @return Generator |
||
630 | */ |
||
631 | 15 | public function setOptionDestination($optionDestination) |
|
640 | /** |
||
641 | * Gets the optionSrcDirname value |
||
642 | * @return string |
||
643 | */ |
||
644 | 365 | public function getOptionSrcDirname() |
|
648 | /** |
||
649 | * Sets the optionSrcDirname value |
||
650 | * @param string $optionSrcDirname |
||
651 | * @return Generator |
||
652 | */ |
||
653 | 15 | public function setOptionSrcDirname($optionSrcDirname) |
|
658 | /** |
||
659 | * Gets the optionSoapOptions value |
||
660 | * @return array |
||
661 | */ |
||
662 | 765 | public function getOptionSoapOptions() |
|
666 | /** |
||
667 | * Sets the optionSoapOptions value |
||
668 | * @param array $optionSoapOptions |
||
669 | * @return Generator |
||
670 | */ |
||
671 | 5 | public function setOptionSoapOptions($optionSoapOptions) |
|
679 | /** |
||
680 | * Gets the optionComposerName value |
||
681 | * @return string |
||
682 | */ |
||
683 | 65 | public function getOptionComposerName() |
|
687 | /** |
||
688 | * Sets the optionComposerName value |
||
689 | * @param string $optionComposerName |
||
690 | * @return Generator |
||
691 | */ |
||
692 | 35 | public function setOptionComposerName($optionComposerName) |
|
701 | /** |
||
702 | * Gets the optionComposerSettings value |
||
703 | * @return array |
||
704 | */ |
||
705 | 50 | public function getOptionComposerSettings() |
|
709 | /** |
||
710 | * Sets the optionComposerSettings value |
||
711 | * @param array $optionComposerSettings |
||
712 | * @return Generator |
||
713 | */ |
||
714 | 10 | public function setOptionComposerSettings(array $optionComposerSettings = []) |
|
719 | /** |
||
720 | * Gets the optionStructsFolder value |
||
721 | * @return string |
||
722 | */ |
||
723 | 370 | public function getOptionStructsFolder() |
|
727 | /** |
||
728 | * Sets the optionStructsFolder value |
||
729 | * @param string $optionStructsFolder |
||
730 | * @return Generator |
||
731 | */ |
||
732 | 5 | public function setOptionStructsFolder($optionStructsFolder) |
|
737 | /** |
||
738 | * Gets the optionArraysFolder value |
||
739 | * @return string |
||
740 | */ |
||
741 | 70 | public function getOptionArraysFolder() |
|
745 | /** |
||
746 | * Sets the optionArraysFolder value |
||
747 | * @param string $optionArraysFolder |
||
748 | * @return Generator |
||
749 | */ |
||
750 | 5 | public function setOptionArraysFolder($optionArraysFolder) |
|
755 | /** |
||
756 | * Gets the optionEnumsFolder value |
||
757 | * @return string |
||
758 | */ |
||
759 | 140 | public function getOptionEnumsFolder() |
|
763 | /** |
||
764 | * Sets the optionEnumsFolder value |
||
765 | * @param string $optionEnumsFolder |
||
766 | * @return Generator |
||
767 | */ |
||
768 | 5 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
773 | /** |
||
774 | * Gets the optionServicesFolder value |
||
775 | * @return string |
||
776 | */ |
||
777 | 730 | public function getOptionServicesFolder() |
|
781 | /** |
||
782 | * Sets the optionServicesFolder value |
||
783 | * @param string $optionServicesFolder |
||
784 | * @return Generator |
||
785 | */ |
||
786 | 5 | public function setOptionServicesFolder($optionServicesFolder) |
|
791 | /** |
||
792 | * Gets the optionSchemasSave value |
||
793 | * @return bool |
||
794 | */ |
||
795 | 15 | public function getOptionSchemasSave() |
|
799 | /** |
||
800 | * Sets the optionSchemasSave value |
||
801 | * @param bool $optionSchemasSave |
||
802 | * @return Generator |
||
803 | */ |
||
804 | 10 | public function setOptionSchemasSave($optionSchemasSave) |
|
809 | /** |
||
810 | * Gets the optionSchemasFolder value |
||
811 | * @return string |
||
812 | */ |
||
813 | 10 | public function getOptionSchemasFolder() |
|
817 | /** |
||
818 | * Sets the optionSchemasFolder value |
||
819 | * @param string $optionSchemasFolder |
||
820 | * @return Generator |
||
821 | */ |
||
822 | 10 | public function setOptionSchemasFolder($optionSchemasFolder) |
|
827 | /** |
||
828 | * Gets the WSDL |
||
829 | * @return Wsdl|null |
||
830 | */ |
||
831 | 580 | public function getWsdl() |
|
835 | /** |
||
836 | * Sets the WSDLs |
||
837 | * @param Wsdl $wsdl |
||
838 | * @return Generator |
||
839 | */ |
||
840 | 750 | protected function setWsdl(Wsdl $wsdl) |
|
845 | /** |
||
846 | * Adds Wsdl location |
||
847 | * @param Wsdl $wsdl |
||
848 | * @param string $schemaLocation |
||
849 | * @return Generator |
||
850 | */ |
||
851 | 150 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
858 | /** |
||
859 | * Gets gather name class |
||
860 | * @param AbstractModel $model the model for which we generate the folder |
||
861 | * @return string |
||
862 | */ |
||
863 | 355 | protected function getGather(AbstractModel $model) |
|
867 | /** |
||
868 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
869 | * @param string $methodName original operation/method name |
||
870 | * @return string |
||
871 | */ |
||
872 | 355 | public function getServiceName($methodName) |
|
876 | /** |
||
877 | * @param GeneratorOptions $options |
||
878 | * @return Generator |
||
879 | */ |
||
880 | 755 | protected function setOptions(GeneratorOptions $options = null) |
|
885 | /** |
||
886 | * @return GeneratorOptions |
||
887 | */ |
||
888 | 775 | public function getOptions() |
|
892 | /** |
||
893 | * @return GeneratorSoapClient |
||
894 | */ |
||
895 | 300 | public function getSoapClient() |
|
899 | /** |
||
900 | * @param string $url |
||
901 | * @return string |
||
902 | */ |
||
903 | 760 | public function getUrlContent($url) |
|
916 | /** |
||
917 | * @return GeneratorContainers |
||
918 | */ |
||
919 | 380 | public function getContainers() |
|
923 | /** |
||
924 | * @return array |
||
925 | */ |
||
926 | 5 | public function jsonSerialize() |
|
933 | /** |
||
934 | * @param string $json |
||
935 | * @throws \InvalidArgumentException |
||
936 | * @return Generator |
||
937 | */ |
||
938 | 380 | public static function instanceFromSerializedJson($json) |
|
962 | /** |
||
963 | * @param Generator $generator |
||
964 | * @param array $jsonArrayEntry |
||
965 | * @return AbstractModel |
||
966 | */ |
||
967 | 380 | protected static function getModelInstanceFromJsonArrayEntry(Generator $generator, array $jsonArrayEntry) |
|
971 | } |
||
972 |