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 | 765 | public function __construct(GeneratorOptions $options) |
|
56 | /** |
||
57 | * @return Generator |
||
58 | */ |
||
59 | 765 | protected function initialize() |
|
67 | /** |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @return Generator |
||
70 | */ |
||
71 | 765 | protected function initSoapClient() |
|
78 | /** |
||
79 | * @return Generator |
||
80 | */ |
||
81 | 765 | protected function initContainers() |
|
88 | /** |
||
89 | * @return Generator |
||
90 | */ |
||
91 | 765 | protected function initParsers() |
|
98 | /** |
||
99 | * @return GeneratorParsers |
||
100 | */ |
||
101 | 150 | public function getParsers() |
|
105 | /** |
||
106 | * @return Generator |
||
107 | */ |
||
108 | 765 | 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 | 760 | 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 | 565 | public function getStructByName($structName) |
|
203 | /** |
||
204 | * Gets the struct by its name and type |
||
205 | * @uses Generator::getStructs() |
||
206 | * @param string $structName the original struct name |
||
207 | * @param string $type the original struct type |
||
208 | * @return Struct|null |
||
209 | */ |
||
210 | public function getStructByNameAndType($structName, $type) |
||
214 | /** |
||
215 | * Gets a service by its name |
||
216 | * @param string $serviceName the service name |
||
217 | * @return Service|null |
||
218 | */ |
||
219 | 185 | public function getService($serviceName) |
|
223 | /** |
||
224 | * Returns the method |
||
225 | * @uses Generator::getServiceName() |
||
226 | * @uses Generator::getService() |
||
227 | * @uses Service::getMethod() |
||
228 | * @param string $methodName the original function name |
||
229 | * @return Method|null |
||
230 | */ |
||
231 | 175 | public function getServiceMethod($methodName) |
|
235 | /** |
||
236 | * @param bool $usingGatherMethods allows to gather methods within a single service if gather_methods options is set to true |
||
237 | * @return ServiceContainer |
||
238 | */ |
||
239 | 405 | public function getServices($usingGatherMethods = false) |
|
255 | /** |
||
256 | * @return StructContainer |
||
257 | */ |
||
258 | 655 | public function getStructs() |
|
262 | /** |
||
263 | * Sets the optionCategory value |
||
264 | * @return string |
||
265 | */ |
||
266 | 405 | public function getOptionCategory() |
|
270 | /** |
||
271 | * Sets the optionCategory value |
||
272 | * @param string $category |
||
273 | * @return Generator |
||
274 | */ |
||
275 | 10 | public function setOptionCategory($category) |
|
280 | /** |
||
281 | * Sets the optionGatherMethods value |
||
282 | * @return string |
||
283 | */ |
||
284 | 405 | public function getOptionGatherMethods() |
|
288 | /** |
||
289 | * Sets the optionGatherMethods value |
||
290 | * @param string $gatherMethods |
||
291 | * @return Generator |
||
292 | */ |
||
293 | 5 | public function setOptionGatherMethods($gatherMethods) |
|
298 | /** |
||
299 | * Gets the optionGenericConstantsNames value |
||
300 | * @return bool |
||
301 | */ |
||
302 | 80 | public function getOptionGenericConstantsNames() |
|
306 | /** |
||
307 | * Sets the optionGenericConstantsNames value |
||
308 | * @param bool $genericConstantsNames |
||
309 | * @return Generator |
||
310 | */ |
||
311 | 10 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
316 | /** |
||
317 | * Gets the optionGenerateTutorialFile value |
||
318 | * @return bool |
||
319 | */ |
||
320 | 40 | public function getOptionGenerateTutorialFile() |
|
324 | /** |
||
325 | * Sets the optionGenerateTutorialFile value |
||
326 | * @param bool $generateTutorialFile |
||
327 | * @return Generator |
||
328 | */ |
||
329 | 5 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
334 | /** |
||
335 | * Gets the optionNamespacePrefix value |
||
336 | * @return string |
||
337 | */ |
||
338 | 375 | public function getOptionNamespacePrefix() |
|
342 | /** |
||
343 | * Sets the optionGenerateTutorialFile value |
||
344 | * @param string $namespace |
||
345 | * @return Generator |
||
346 | */ |
||
347 | 25 | public function setOptionNamespacePrefix($namespace) |
|
352 | /** |
||
353 | * Gets the optionAddComments value |
||
354 | * @return array |
||
355 | */ |
||
356 | 295 | public function getOptionAddComments() |
|
360 | /** |
||
361 | * Sets the optionAddComments value |
||
362 | * @param array $addComments |
||
363 | * @return Generator |
||
364 | */ |
||
365 | 5 | public function setOptionAddComments($addComments) |
|
370 | /** |
||
371 | * Gets the optionStandalone value |
||
372 | * @return bool |
||
373 | */ |
||
374 | 85 | public function getOptionStandalone() |
|
378 | /** |
||
379 | * Sets the optionStandalone value |
||
380 | * @param bool $standalone |
||
381 | * @return Generator |
||
382 | */ |
||
383 | 15 | public function setOptionStandalone($standalone) |
|
388 | /** |
||
389 | * Gets the optionValidation value |
||
390 | * @return bool |
||
391 | */ |
||
392 | 175 | public function getOptionValidation() |
|
396 | /** |
||
397 | * Sets the optionValidation value |
||
398 | * @param bool $validation |
||
399 | * @return Generator |
||
400 | */ |
||
401 | 35 | public function setOptionValidation($validation) |
|
406 | /** |
||
407 | * Gets the optionStructClass value |
||
408 | * @return string |
||
409 | */ |
||
410 | 150 | public function getOptionStructClass() |
|
414 | /** |
||
415 | * Sets the optionStructClass value |
||
416 | * @param string $structClass |
||
417 | * @return Generator |
||
418 | */ |
||
419 | 15 | public function setOptionStructClass($structClass) |
|
424 | /** |
||
425 | * Gets the optionStructArrayClass value |
||
426 | * @return string |
||
427 | */ |
||
428 | 50 | public function getOptionStructArrayClass() |
|
432 | /** |
||
433 | * Sets the optionStructArrayClass value |
||
434 | * @param string $structArrayClass |
||
435 | * @return Generator |
||
436 | */ |
||
437 | 5 | public function setOptionStructArrayClass($structArrayClass) |
|
442 | /** |
||
443 | * Gets the optionSoapClientClass value |
||
444 | * @return string |
||
445 | */ |
||
446 | 125 | public function getOptionSoapClientClass() |
|
450 | /** |
||
451 | * Sets the optionSoapClientClass value |
||
452 | * @param string $soapClientClass |
||
453 | * @return Generator |
||
454 | */ |
||
455 | 5 | public function setOptionSoapClientClass($soapClientClass) |
|
460 | /** |
||
461 | * Gets the package name prefix |
||
462 | * @param bool $ucFirst ucfirst package name prefix or not |
||
463 | * @return string |
||
464 | */ |
||
465 | 775 | public function getOptionPrefix($ucFirst = true) |
|
469 | /** |
||
470 | * Sets the package name prefix |
||
471 | * @param string $optionPrefix |
||
472 | * @return Generator |
||
473 | */ |
||
474 | 105 | public function setOptionPrefix($optionPrefix) |
|
479 | /** |
||
480 | * Gets the package name suffix |
||
481 | * @param bool $ucFirst ucfirst package name suffix or not |
||
482 | * @return string |
||
483 | */ |
||
484 | 745 | public function getOptionSuffix($ucFirst = true) |
|
488 | /** |
||
489 | * Sets the package name suffix |
||
490 | * @param string $optionSuffix |
||
491 | * @return Generator |
||
492 | */ |
||
493 | 35 | public function setOptionSuffix($optionSuffix) |
|
498 | /** |
||
499 | * Gets the optionBasicLogin value |
||
500 | * @return string |
||
501 | */ |
||
502 | 785 | public function getOptionBasicLogin() |
|
506 | /** |
||
507 | * Sets the optionBasicLogin value |
||
508 | * @param string $optionBasicLogin |
||
509 | * @return Generator |
||
510 | */ |
||
511 | 5 | public function setOptionBasicLogin($optionBasicLogin) |
|
516 | /** |
||
517 | * Gets the optionBasicPassword value |
||
518 | * @return string |
||
519 | */ |
||
520 | 785 | public function getOptionBasicPassword() |
|
524 | /** |
||
525 | * Sets the optionBasicPassword value |
||
526 | * @param string $optionBasicPassword |
||
527 | * @return Generator |
||
528 | */ |
||
529 | 5 | public function setOptionBasicPassword($optionBasicPassword) |
|
534 | /** |
||
535 | * Gets the optionProxyHost value |
||
536 | * @return string |
||
537 | */ |
||
538 | 785 | public function getOptionProxyHost() |
|
542 | /** |
||
543 | * Sets the optionProxyHost value |
||
544 | * @param string $optionProxyHost |
||
545 | * @return Generator |
||
546 | */ |
||
547 | 5 | public function setOptionProxyHost($optionProxyHost) |
|
552 | /** |
||
553 | * Gets the optionProxyPort value |
||
554 | * @return string |
||
555 | */ |
||
556 | 785 | public function getOptionProxyPort() |
|
560 | /** |
||
561 | * Sets the optionProxyPort value |
||
562 | * @param string $optionProxyPort |
||
563 | * @return Generator |
||
564 | */ |
||
565 | 5 | public function setOptionProxyPort($optionProxyPort) |
|
570 | /** |
||
571 | * Gets the optionProxyLogin value |
||
572 | * @return string |
||
573 | */ |
||
574 | 785 | public function getOptionProxyLogin() |
|
578 | /** |
||
579 | * Sets the optionProxyLogin value |
||
580 | * @param string $optionProxyLogin |
||
581 | * @return Generator |
||
582 | */ |
||
583 | 5 | public function setOptionProxyLogin($optionProxyLogin) |
|
588 | /** |
||
589 | * Gets the optionProxyPassword value |
||
590 | * @return string |
||
591 | */ |
||
592 | 785 | public function getOptionProxyPassword() |
|
596 | /** |
||
597 | * Sets the optionProxyPassword value |
||
598 | * @param string $optionProxyPassword |
||
599 | * @return Generator |
||
600 | */ |
||
601 | 5 | public function setOptionProxyPassword($optionProxyPassword) |
|
606 | /** |
||
607 | * Gets the optionOrigin value |
||
608 | * @return string |
||
609 | */ |
||
610 | 780 | public function getOptionOrigin() |
|
614 | /** |
||
615 | * Sets the optionOrigin value |
||
616 | * @param string $optionOrigin |
||
617 | * @return Generator |
||
618 | */ |
||
619 | 5 | public function setOptionOrigin($optionOrigin) |
|
625 | /** |
||
626 | * Gets the optionDestination value |
||
627 | * @return string |
||
628 | */ |
||
629 | 435 | public function getOptionDestination() |
|
637 | /** |
||
638 | * Sets the optionDestination value |
||
639 | * @param string $optionDestination |
||
640 | * @return Generator |
||
641 | */ |
||
642 | 15 | public function setOptionDestination($optionDestination) |
|
651 | /** |
||
652 | * Gets the optionSrcDirname value |
||
653 | * @return string |
||
654 | */ |
||
655 | 370 | public function getOptionSrcDirname() |
|
659 | /** |
||
660 | * Sets the optionSrcDirname value |
||
661 | * @param string $optionSrcDirname |
||
662 | * @return Generator |
||
663 | */ |
||
664 | 15 | public function setOptionSrcDirname($optionSrcDirname) |
|
669 | /** |
||
670 | * Gets the optionSoapOptions value |
||
671 | * @return array |
||
672 | */ |
||
673 | 775 | public function getOptionSoapOptions() |
|
677 | /** |
||
678 | * Sets the optionSoapOptions value |
||
679 | * @param array $optionSoapOptions |
||
680 | * @return Generator |
||
681 | */ |
||
682 | 5 | public function setOptionSoapOptions($optionSoapOptions) |
|
690 | /** |
||
691 | * Gets the optionComposerName value |
||
692 | * @return string |
||
693 | */ |
||
694 | 65 | public function getOptionComposerName() |
|
698 | /** |
||
699 | * Sets the optionComposerName value |
||
700 | * @param string $optionComposerName |
||
701 | * @return Generator |
||
702 | */ |
||
703 | 35 | public function setOptionComposerName($optionComposerName) |
|
712 | /** |
||
713 | * Gets the optionComposerSettings value |
||
714 | * @return array |
||
715 | */ |
||
716 | 50 | public function getOptionComposerSettings() |
|
720 | /** |
||
721 | * Sets the optionComposerSettings value |
||
722 | * @param array $optionComposerSettings |
||
723 | * @return Generator |
||
724 | */ |
||
725 | 10 | public function setOptionComposerSettings(array $optionComposerSettings = []) |
|
730 | /** |
||
731 | * Gets the optionStructsFolder value |
||
732 | * @return string |
||
733 | */ |
||
734 | 375 | public function getOptionStructsFolder() |
|
738 | /** |
||
739 | * Sets the optionStructsFolder value |
||
740 | * @param string $optionStructsFolder |
||
741 | * @return Generator |
||
742 | */ |
||
743 | 5 | public function setOptionStructsFolder($optionStructsFolder) |
|
748 | /** |
||
749 | * Gets the optionArraysFolder value |
||
750 | * @return string |
||
751 | */ |
||
752 | 70 | public function getOptionArraysFolder() |
|
756 | /** |
||
757 | * Sets the optionArraysFolder value |
||
758 | * @param string $optionArraysFolder |
||
759 | * @return Generator |
||
760 | */ |
||
761 | 5 | public function setOptionArraysFolder($optionArraysFolder) |
|
766 | /** |
||
767 | * Gets the optionEnumsFolder value |
||
768 | * @return string |
||
769 | */ |
||
770 | 140 | public function getOptionEnumsFolder() |
|
774 | /** |
||
775 | * Sets the optionEnumsFolder value |
||
776 | * @param string $optionEnumsFolder |
||
777 | * @return Generator |
||
778 | */ |
||
779 | 5 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
784 | /** |
||
785 | * Gets the optionServicesFolder value |
||
786 | * @return string |
||
787 | */ |
||
788 | 740 | public function getOptionServicesFolder() |
|
792 | /** |
||
793 | * Sets the optionServicesFolder value |
||
794 | * @param string $optionServicesFolder |
||
795 | * @return Generator |
||
796 | */ |
||
797 | 5 | public function setOptionServicesFolder($optionServicesFolder) |
|
802 | /** |
||
803 | * Gets the optionSchemasSave value |
||
804 | * @return bool |
||
805 | */ |
||
806 | 15 | public function getOptionSchemasSave() |
|
810 | /** |
||
811 | * Sets the optionSchemasSave value |
||
812 | * @param bool $optionSchemasSave |
||
813 | * @return Generator |
||
814 | */ |
||
815 | 10 | public function setOptionSchemasSave($optionSchemasSave) |
|
820 | /** |
||
821 | * Gets the optionSchemasFolder value |
||
822 | * @return string |
||
823 | */ |
||
824 | 10 | public function getOptionSchemasFolder() |
|
828 | /** |
||
829 | * Sets the optionSchemasFolder value |
||
830 | * @param string $optionSchemasFolder |
||
831 | * @return Generator |
||
832 | */ |
||
833 | 10 | public function setOptionSchemasFolder($optionSchemasFolder) |
|
838 | /** |
||
839 | * Gets the optionXsdTypesPath value |
||
840 | * @return string |
||
841 | */ |
||
842 | 200 | public function getOptionXsdTypesPath() |
|
846 | /** |
||
847 | * Sets the optionXsdTypesPath value |
||
848 | * @param string $xsdTypesPath |
||
849 | * @return Generator |
||
850 | */ |
||
851 | 5 | public function setOptionXsdTypesPath($xsdTypesPath) |
|
856 | /** |
||
857 | * Gets the WSDL |
||
858 | * @return Wsdl|null |
||
859 | */ |
||
860 | 585 | public function getWsdl() |
|
864 | /** |
||
865 | * Sets the WSDLs |
||
866 | * @param Wsdl $wsdl |
||
867 | * @return Generator |
||
868 | */ |
||
869 | 760 | protected function setWsdl(Wsdl $wsdl) |
|
874 | /** |
||
875 | * Adds Wsdl location |
||
876 | * @param Wsdl $wsdl |
||
877 | * @param string $schemaLocation |
||
878 | * @return Generator |
||
879 | */ |
||
880 | 150 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
887 | /** |
||
888 | * Gets gather name class |
||
889 | * @param AbstractModel $model the model for which we generate the folder |
||
890 | * @return string |
||
891 | */ |
||
892 | 355 | protected function getGather(AbstractModel $model) |
|
896 | /** |
||
897 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
898 | * @param string $methodName original operation/method name |
||
899 | * @return string |
||
900 | */ |
||
901 | 355 | public function getServiceName($methodName) |
|
905 | /** |
||
906 | * @param GeneratorOptions $options |
||
907 | * @return Generator |
||
908 | */ |
||
909 | 765 | protected function setOptions(GeneratorOptions $options = null) |
|
914 | /** |
||
915 | * @return GeneratorOptions |
||
916 | */ |
||
917 | 785 | public function getOptions() |
|
921 | /** |
||
922 | * @return GeneratorSoapClient |
||
923 | */ |
||
924 | 300 | public function getSoapClient() |
|
928 | /** |
||
929 | * @param string $url |
||
930 | * @return string |
||
931 | */ |
||
932 | 765 | public function getUrlContent($url) |
|
945 | /** |
||
946 | * @return GeneratorContainers |
||
947 | */ |
||
948 | 390 | public function getContainers() |
|
952 | /** |
||
953 | * @return array |
||
954 | */ |
||
955 | 5 | public function jsonSerialize() |
|
962 | /** |
||
963 | * @param string $json |
||
964 | * @throws \InvalidArgumentException |
||
965 | * @return Generator |
||
966 | */ |
||
967 | 390 | public static function instanceFromSerializedJson($json) |
|
991 | /** |
||
992 | * @param Generator $generator |
||
993 | * @param array $jsonArrayEntry |
||
994 | * @return AbstractModel |
||
995 | */ |
||
996 | 390 | protected static function getModelInstanceFromJsonArrayEntry(Generator $generator, array $jsonArrayEntry) |
|
1000 | } |
||
1001 |
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.