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 | 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 | 740 | public function __construct(GeneratorOptions $options) |
|
56 | /** |
||
57 | * @return Generator |
||
58 | */ |
||
59 | 740 | protected function initialize() |
|
67 | /** |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @return Generator |
||
70 | */ |
||
71 | 740 | protected function initSoapClient() |
|
78 | /** |
||
79 | * @return Generator |
||
80 | */ |
||
81 | 740 | protected function initContainers() |
|
88 | /** |
||
89 | * @return Generator |
||
90 | */ |
||
91 | 740 | protected function initParsers() |
|
98 | /** |
||
99 | * @return GeneratorParsers |
||
100 | */ |
||
101 | 150 | public function getParsers() |
|
105 | /** |
||
106 | * @return Generator |
||
107 | */ |
||
108 | 740 | protected function initFiles() |
|
115 | /** |
||
116 | * @return GeneratorFiles |
||
117 | */ |
||
118 | 60 | public function getFiles() |
|
122 | /** |
||
123 | * @throws \InvalidArgumentException |
||
124 | * @return Generator |
||
125 | */ |
||
126 | 35 | protected function initDirectory() |
|
134 | /** |
||
135 | * @return Generator |
||
136 | */ |
||
137 | 740 | 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 | 540 | 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 | * @return ServiceContainer |
||
226 | */ |
||
227 | 390 | public function getServices() |
|
231 | /** |
||
232 | * @return StructContainer |
||
233 | */ |
||
234 | 630 | public function getStructs() |
|
238 | /** |
||
239 | * Sets the optionCategory value |
||
240 | * @return string |
||
241 | */ |
||
242 | 390 | public function getOptionCategory() |
|
246 | /** |
||
247 | * Sets the optionCategory value |
||
248 | * @param string $category |
||
249 | * @return Generator |
||
250 | */ |
||
251 | 10 | public function setOptionCategory($category) |
|
256 | /** |
||
257 | * Sets the optionGatherMethods value |
||
258 | * @return string |
||
259 | */ |
||
260 | 365 | public function getOptionGatherMethods() |
|
264 | /** |
||
265 | * Sets the optionGatherMethods value |
||
266 | * @param string $gatherMethods |
||
267 | * @return Generator |
||
268 | */ |
||
269 | 5 | public function setOptionGatherMethods($gatherMethods) |
|
274 | /** |
||
275 | * Gets the optionGenericConstantsNames value |
||
276 | * @return bool |
||
277 | */ |
||
278 | 80 | public function getOptionGenericConstantsNames() |
|
282 | /** |
||
283 | * Sets the optionGenericConstantsNames value |
||
284 | * @param bool $genericConstantsNames |
||
285 | * @return Generator |
||
286 | */ |
||
287 | 10 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
292 | /** |
||
293 | * Gets the optionGenerateTutorialFile value |
||
294 | * @return bool |
||
295 | */ |
||
296 | 40 | public function getOptionGenerateTutorialFile() |
|
300 | /** |
||
301 | * Sets the optionGenerateTutorialFile value |
||
302 | * @param bool $generateTutorialFile |
||
303 | * @return Generator |
||
304 | */ |
||
305 | 5 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
310 | /** |
||
311 | * Gets the optionNamespacePrefix value |
||
312 | * @return string |
||
313 | */ |
||
314 | 360 | public function getOptionNamespacePrefix() |
|
318 | /** |
||
319 | * Sets the optionGenerateTutorialFile value |
||
320 | * @param string $namespace |
||
321 | * @return Generator |
||
322 | */ |
||
323 | 25 | public function setOptionNamespacePrefix($namespace) |
|
328 | /** |
||
329 | * Gets the optionAddComments value |
||
330 | * @return array |
||
331 | */ |
||
332 | 285 | public function getOptionAddComments() |
|
336 | /** |
||
337 | * Sets the optionAddComments value |
||
338 | * @param array $addComments |
||
339 | * @return Generator |
||
340 | */ |
||
341 | 5 | public function setOptionAddComments($addComments) |
|
346 | /** |
||
347 | * Gets the optionStandalone value |
||
348 | * @return bool |
||
349 | */ |
||
350 | 80 | public function getOptionStandalone() |
|
354 | /** |
||
355 | * Sets the optionStandalone value |
||
356 | * @param bool $standalone |
||
357 | * @return Generator |
||
358 | */ |
||
359 | 15 | public function setOptionStandalone($standalone) |
|
364 | /** |
||
365 | * Gets the optionValidation value |
||
366 | * @return bool |
||
367 | */ |
||
368 | 165 | public function getOptionValidation() |
|
372 | /** |
||
373 | * Sets the optionValidation value |
||
374 | * @param bool $validation |
||
375 | * @return Generator |
||
376 | */ |
||
377 | 35 | public function setOptionValidation($validation) |
|
382 | /** |
||
383 | * Gets the optionStructClass value |
||
384 | * @return string |
||
385 | */ |
||
386 | 140 | public function getOptionStructClass() |
|
390 | /** |
||
391 | * Sets the optionStructClass value |
||
392 | * @param string $structClass |
||
393 | * @return Generator |
||
394 | */ |
||
395 | 15 | public function setOptionStructClass($structClass) |
|
400 | /** |
||
401 | * Gets the optionStructArrayClass value |
||
402 | * @return string |
||
403 | */ |
||
404 | 50 | public function getOptionStructArrayClass() |
|
408 | /** |
||
409 | * Sets the optionStructArrayClass value |
||
410 | * @param string $structArrayClass |
||
411 | * @return Generator |
||
412 | */ |
||
413 | 5 | public function setOptionStructArrayClass($structArrayClass) |
|
418 | /** |
||
419 | * Gets the optionSoapClientClass value |
||
420 | * @return string |
||
421 | */ |
||
422 | 125 | public function getOptionSoapClientClass() |
|
426 | /** |
||
427 | * Sets the optionSoapClientClass value |
||
428 | * @param string $soapClientClass |
||
429 | * @return Generator |
||
430 | */ |
||
431 | 5 | public function setOptionSoapClientClass($soapClientClass) |
|
436 | /** |
||
437 | * Gets the package name prefix |
||
438 | * @param bool $ucFirst ucfirst package name prefix or not |
||
439 | * @return string |
||
440 | */ |
||
441 | 745 | public function getOptionPrefix($ucFirst = true) |
|
445 | /** |
||
446 | * Sets the package name prefix |
||
447 | * @param string $optionPrefix |
||
448 | * @return Generator |
||
449 | */ |
||
450 | 105 | public function setOptionPrefix($optionPrefix) |
|
455 | /** |
||
456 | * Gets the package name suffix |
||
457 | * @param bool $ucFirst ucfirst package name suffix or not |
||
458 | * @return string |
||
459 | */ |
||
460 | 715 | public function getOptionSuffix($ucFirst = true) |
|
464 | /** |
||
465 | * Sets the package name suffix |
||
466 | * @param string $optionSuffix |
||
467 | * @return Generator |
||
468 | */ |
||
469 | 35 | public function setOptionSuffix($optionSuffix) |
|
474 | /** |
||
475 | * Gets the optionBasicLogin value |
||
476 | * @return string |
||
477 | */ |
||
478 | 760 | public function getOptionBasicLogin() |
|
482 | /** |
||
483 | * Sets the optionBasicLogin value |
||
484 | * @param string $optionBasicLogin |
||
485 | * @return Generator |
||
486 | */ |
||
487 | 5 | public function setOptionBasicLogin($optionBasicLogin) |
|
492 | /** |
||
493 | * Gets the optionBasicPassword value |
||
494 | * @return string |
||
495 | */ |
||
496 | 760 | public function getOptionBasicPassword() |
|
500 | /** |
||
501 | * Sets the optionBasicPassword value |
||
502 | * @param string $optionBasicPassword |
||
503 | * @return Generator |
||
504 | */ |
||
505 | 5 | public function setOptionBasicPassword($optionBasicPassword) |
|
510 | /** |
||
511 | * Gets the optionProxyHost value |
||
512 | * @return string |
||
513 | */ |
||
514 | 760 | public function getOptionProxyHost() |
|
518 | /** |
||
519 | * Sets the optionProxyHost value |
||
520 | * @param string $optionProxyHost |
||
521 | * @return Generator |
||
522 | */ |
||
523 | 5 | public function setOptionProxyHost($optionProxyHost) |
|
524 | { |
||
525 | 5 | $this->options->setProxyHost($optionProxyHost); |
|
526 | 5 | return $this; |
|
527 | 1 | } |
|
528 | /** |
||
529 | * Gets the optionProxyPort value |
||
530 | * @return string |
||
531 | */ |
||
532 | 760 | public function getOptionProxyPort() |
|
536 | /** |
||
537 | * Sets the optionProxyPort value |
||
538 | * @param string $optionProxyPort |
||
539 | * @return Generator |
||
540 | */ |
||
541 | 5 | public function setOptionProxyPort($optionProxyPort) |
|
546 | /** |
||
547 | * Gets the optionProxyLogin value |
||
548 | * @return string |
||
549 | */ |
||
550 | 760 | public function getOptionProxyLogin() |
|
554 | /** |
||
555 | * Sets the optionProxyLogin value |
||
556 | * @param string $optionProxyLogin |
||
557 | * @return Generator |
||
558 | */ |
||
559 | 5 | public function setOptionProxyLogin($optionProxyLogin) |
|
564 | /** |
||
565 | * Gets the optionProxyPassword value |
||
566 | * @return string |
||
567 | */ |
||
568 | 760 | public function getOptionProxyPassword() |
|
572 | /** |
||
573 | * Sets the optionProxyPassword value |
||
574 | * @param string $optionProxyPassword |
||
575 | * @return Generator |
||
576 | */ |
||
577 | 5 | public function setOptionProxyPassword($optionProxyPassword) |
|
582 | /** |
||
583 | * Gets the optionOrigin value |
||
584 | * @return string |
||
585 | */ |
||
586 | 755 | public function getOptionOrigin() |
|
590 | /** |
||
591 | * Sets the optionOrigin value |
||
592 | * @param string $optionOrigin |
||
593 | * @return Generator |
||
594 | */ |
||
595 | 5 | public function setOptionOrigin($optionOrigin) |
|
601 | /** |
||
602 | * Gets the optionDestination value |
||
603 | * @return string |
||
604 | */ |
||
605 | 415 | public function getOptionDestination() |
|
613 | /** |
||
614 | * Sets the optionDestination value |
||
615 | * @param string $optionDestination |
||
616 | * @return Generator |
||
617 | */ |
||
618 | 15 | public function setOptionDestination($optionDestination) |
|
627 | /** |
||
628 | * Gets the optionSrcDiname value |
||
629 | * @return string |
||
630 | */ |
||
631 | 360 | public function getOptionSrcDirname() |
|
635 | /** |
||
636 | * Sets the optionSrcDirname value |
||
637 | * @param string $optionSrcDirname |
||
638 | * @return Generator |
||
639 | */ |
||
640 | 15 | public function setOptionSrcDirname($optionSrcDirname) |
|
645 | /** |
||
646 | * Gets the optionSoapOptions value |
||
647 | * @return string |
||
648 | */ |
||
649 | 750 | public function getOptionSoapOptions() |
|
653 | /** |
||
654 | * Sets the optionSoapOptions value |
||
655 | * @param array $optionSoapOptions |
||
656 | * @return Generator |
||
657 | */ |
||
658 | 5 | public function setOptionSoapOptions($optionSoapOptions) |
|
666 | /** |
||
667 | * Gets the optionComposerName value |
||
668 | * @return string |
||
669 | */ |
||
670 | 65 | public function getOptionComposerName() |
|
674 | /** |
||
675 | * Sets the optionComposerName value |
||
676 | * @param string $optionComposerName |
||
677 | * @return Generator |
||
678 | */ |
||
679 | 35 | public function setOptionComposerName($optionComposerName) |
|
688 | /** |
||
689 | * Gets the optionComposerSettings value |
||
690 | * @return array |
||
691 | */ |
||
692 | 50 | public function getOptionComposerSettings() |
|
696 | /** |
||
697 | * Sets the optionComposerSettings value |
||
698 | * @param array $optionComposerSettings |
||
699 | * @return Generator |
||
700 | */ |
||
701 | 10 | public function setOptionComposerSettings(array $optionComposerSettings = array()) |
|
706 | /** |
||
707 | * Gets the optionStructsFolder value |
||
708 | * @return string |
||
709 | */ |
||
710 | 360 | public function getOptionStructsFolder() |
|
714 | /** |
||
715 | * Sets the optionStructsFolder value |
||
716 | * @param string $optionStructsFolder |
||
717 | * @return Generator |
||
718 | */ |
||
719 | 5 | public function setOptionStructsFolder($optionStructsFolder) |
|
724 | /** |
||
725 | * Gets the optionArraysFolder value |
||
726 | * @return string |
||
727 | */ |
||
728 | 70 | public function getOptionArraysFolder() |
|
732 | /** |
||
733 | * Sets the optionArraysFolder value |
||
734 | * @param string $optionArraysFolder |
||
735 | * @return Generator |
||
736 | */ |
||
737 | 5 | public function setOptionArraysFolder($optionArraysFolder) |
|
742 | /** |
||
743 | * Gets the optionEnumsFolder value |
||
744 | * @return string |
||
745 | */ |
||
746 | 140 | public function getOptionEnumsFolder() |
|
750 | /** |
||
751 | * Sets the optionEnumsFolder value |
||
752 | * @param string $optionEnumsFolder |
||
753 | * @return Generator |
||
754 | */ |
||
755 | 5 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
760 | /** |
||
761 | * Gets the optionServicesFolder value |
||
762 | * @return string |
||
763 | */ |
||
764 | 710 | public function getOptionServicesFolder() |
|
768 | /** |
||
769 | * Sets the optionServicesFolder value |
||
770 | * @param string $optionServicesFolder |
||
771 | * @return Generator |
||
772 | */ |
||
773 | 5 | public function setOptionServicesFolder($optionServicesFolder) |
|
778 | /** |
||
779 | * Gets the WSDL |
||
780 | * @return Wsdl|null |
||
781 | */ |
||
782 | 570 | public function getWsdl() |
|
786 | /** |
||
787 | * Sets the WSDLs |
||
788 | * @param Wsdl $wsdl |
||
789 | * @return Generator |
||
790 | */ |
||
791 | 735 | protected function setWsdl(Wsdl $wsdl) |
|
796 | /** |
||
797 | * Adds Wsdl location |
||
798 | * @param Wsdl $wsdl |
||
799 | * @param string $schemaLocation |
||
800 | * @return Generator |
||
801 | */ |
||
802 | 150 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
809 | /** |
||
810 | * Gets gather name class |
||
811 | * @param AbstractModel $model the model for which we generate the folder |
||
812 | * @return string |
||
813 | */ |
||
814 | 355 | private function getGather(AbstractModel $model) |
|
818 | /** |
||
819 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
820 | * @param string $methodName original operation/method name |
||
821 | * @return string |
||
822 | */ |
||
823 | 355 | public function getServiceName($methodName) |
|
827 | /** |
||
828 | * @param GeneratorOptions $options |
||
829 | * @return Generator |
||
830 | */ |
||
831 | 740 | protected function setOptions(GeneratorOptions $options = null) |
|
836 | /** |
||
837 | * @return GeneratorOptions |
||
838 | */ |
||
839 | 755 | public function getOptions() |
|
843 | /** |
||
844 | * @return GeneratorSoapClient |
||
845 | */ |
||
846 | 300 | public function getSoapClient() |
|
850 | /** |
||
851 | * @param string $url |
||
852 | * @return string |
||
853 | */ |
||
854 | 745 | public function getUrlContent($url) |
|
863 | /** |
||
864 | * @return GeneratorContainers |
||
865 | */ |
||
866 | 365 | public function getContainers() |
|
870 | /** |
||
871 | * @return array |
||
872 | */ |
||
873 | 5 | public function jsonSerialize() |
|
880 | /** |
||
881 | * @param string $json |
||
882 | * @throws \InvalidArgumentException |
||
883 | * @return Generator |
||
884 | */ |
||
885 | 365 | public static function instanceFromSerializedJson($json) |
|
909 | /** |
||
910 | * @param Generator $generator |
||
911 | * @param array $jsonArrayEntry |
||
912 | * @return AbstractModel |
||
913 | */ |
||
914 | 365 | private static function getModelInstanceFromJsonArrayEntry(Generator $generator, array $jsonArrayEntry) |
|
918 | } |
||
919 |