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 | 828 | public function __construct(GeneratorOptions $options) |
|
56 | /** |
||
57 | * @return Generator |
||
58 | */ |
||
59 | 828 | protected function initialize() |
|
67 | /** |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @return Generator |
||
70 | */ |
||
71 | 828 | protected function initSoapClient() |
|
78 | /** |
||
79 | * @return Generator |
||
80 | */ |
||
81 | 828 | protected function initContainers() |
|
88 | /** |
||
89 | * @return Generator |
||
90 | */ |
||
91 | 828 | protected function initParsers() |
|
98 | /** |
||
99 | * @return GeneratorParsers |
||
100 | */ |
||
101 | 288 | public function getParsers() |
|
105 | /** |
||
106 | * @return Generator |
||
107 | */ |
||
108 | 828 | protected function initFiles() |
|
115 | /** |
||
116 | * @return GeneratorFiles |
||
117 | */ |
||
118 | 72 | public function getFiles() |
|
122 | /** |
||
123 | * @throws \InvalidArgumentException |
||
124 | * @return Generator |
||
125 | */ |
||
126 | 42 | protected function initDirectory() |
|
134 | /** |
||
135 | * @return Generator |
||
136 | */ |
||
137 | 828 | protected function initWsdl() |
|
142 | /** |
||
143 | * @return Generator |
||
144 | */ |
||
145 | 54 | protected function doSanityChecks() |
|
157 | /** |
||
158 | * @return Generator |
||
159 | */ |
||
160 | 36 | 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() |
|
181 | /** |
||
182 | * Gets the struct by its name |
||
183 | * @uses Generator::getStructs() |
||
184 | * @param string $structName the original struct name |
||
185 | * @return Struct|null |
||
186 | */ |
||
187 | 606 | public function getStruct($structName) |
|
191 | /** |
||
192 | * Gets a service by its name |
||
193 | * @param string $serviceName the service name |
||
194 | * @return Service|null |
||
195 | */ |
||
196 | 528 | public function getService($serviceName) |
|
200 | /** |
||
201 | * Returns the method |
||
202 | * @uses Generator::getServiceName() |
||
203 | * @uses Generator::getService() |
||
204 | * @uses Service::getMethod() |
||
205 | * @param string $methodName the original function name |
||
206 | * @return Method|null |
||
207 | */ |
||
208 | 522 | public function getServiceMethod($methodName) |
|
212 | /** |
||
213 | * @return ServiceContainer |
||
214 | */ |
||
215 | 738 | public function getServices() |
|
219 | /** |
||
220 | * @return StructContainer |
||
221 | */ |
||
222 | 714 | public function getStructs() |
|
226 | /** |
||
227 | * Sets the optionCategory value |
||
228 | * @return string |
||
229 | */ |
||
230 | 462 | public function getOptionCategory() |
|
234 | /** |
||
235 | * Sets the optionCategory value |
||
236 | * @param string $category |
||
237 | * @return Generator |
||
238 | */ |
||
239 | 432 | public function setOptionCategory($category) |
|
244 | /** |
||
245 | * Sets the optionGatherMethods value |
||
246 | * @return string |
||
247 | */ |
||
248 | 750 | public function getOptionGatherMethods() |
|
252 | /** |
||
253 | * Sets the optionGatherMethods value |
||
254 | * @param string $gatherMethods |
||
255 | * @return Generator |
||
256 | */ |
||
257 | 432 | public function setOptionGatherMethods($gatherMethods) |
|
262 | /** |
||
263 | * Gets the optionGenericConstantsNames value |
||
264 | * @return bool |
||
265 | */ |
||
266 | 96 | public function getOptionGenericConstantsNames() |
|
270 | /** |
||
271 | * Sets the optionGenericConstantsNames value |
||
272 | * @param bool $genericConstantsNames |
||
273 | * @return Generator |
||
274 | */ |
||
275 | 12 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
280 | /** |
||
281 | * Gets the optionGenerateTutorialFile value |
||
282 | * @return bool |
||
283 | */ |
||
284 | 48 | public function getOptionGenerateTutorialFile() |
|
288 | /** |
||
289 | * Sets the optionGenerateTutorialFile value |
||
290 | * @param bool $generateTutorialFile |
||
291 | * @return Generator |
||
292 | */ |
||
293 | 6 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
298 | /** |
||
299 | * Gets the optionNamespacePrefix value |
||
300 | * @return string |
||
301 | */ |
||
302 | 426 | public function getOptionNamespacePrefix() |
|
306 | /** |
||
307 | * Sets the optionGenerateTutorialFile value |
||
308 | * @param string $namespace |
||
309 | * @return Generator |
||
310 | */ |
||
311 | 30 | public function setOptionNamespacePrefix($namespace) |
|
316 | /** |
||
317 | * Gets the optionAddComments value |
||
318 | * @return array |
||
319 | */ |
||
320 | 336 | public function getOptionAddComments() |
|
324 | /** |
||
325 | * Sets the optionAddComments value |
||
326 | * @param array $addComments |
||
327 | * @return Generator |
||
328 | */ |
||
329 | 432 | public function setOptionAddComments($addComments) |
|
334 | /** |
||
335 | * Gets the optionStandalone value |
||
336 | * @return bool |
||
337 | */ |
||
338 | 96 | public function getOptionStandalone() |
|
342 | /** |
||
343 | * Sets the optionStandalone value |
||
344 | * @param bool $standalone |
||
345 | * @return Generator |
||
346 | */ |
||
347 | 12 | public function setOptionStandalone($standalone) |
|
352 | /** |
||
353 | * Gets the optionValidation value |
||
354 | * @return bool |
||
355 | */ |
||
356 | 198 | public function getOptionValidation() |
|
360 | /** |
||
361 | * Sets the optionValidation value |
||
362 | * @param bool $validation |
||
363 | * @return Generator |
||
364 | */ |
||
365 | 42 | public function setOptionValidation($validation) |
|
370 | /** |
||
371 | * Gets the optionStructClass value |
||
372 | * @return string |
||
373 | */ |
||
374 | 168 | public function getOptionStructClass() |
|
378 | /** |
||
379 | * Sets the optionStructClass value |
||
380 | * @param string $structClass |
||
381 | * @return Generator |
||
382 | */ |
||
383 | 18 | public function setOptionStructClass($structClass) |
|
388 | /** |
||
389 | * Gets the optionStructArrayClass value |
||
390 | * @return string |
||
391 | */ |
||
392 | 60 | public function getOptionStructArrayClass() |
|
396 | /** |
||
397 | * Sets the optionStructArrayClass value |
||
398 | * @param string $structArrayClass |
||
399 | * @return Generator |
||
400 | */ |
||
401 | 6 | public function setOptionStructArrayClass($structArrayClass) |
|
406 | /** |
||
407 | * Gets the optionSoapClientClass value |
||
408 | * @return string |
||
409 | */ |
||
410 | 144 | public function getOptionSoapClientClass() |
|
414 | /** |
||
415 | * Sets the optionSoapClientClass value |
||
416 | * @param string $soapClientClass |
||
417 | * @return Generator |
||
418 | */ |
||
419 | 6 | public function setOptionSoapClientClass($soapClientClass) |
|
424 | /** |
||
425 | * Gets the package name prefix |
||
426 | * @param bool $ucFirst ucfirst package name prefix or not |
||
427 | * @return string |
||
428 | */ |
||
429 | 864 | public function getOptionPrefix($ucFirst = true) |
|
433 | /** |
||
434 | * Sets the package name prefix |
||
435 | * @param string $optionPrefix |
||
436 | * @return Generator |
||
437 | */ |
||
438 | 480 | public function setOptionPrefix($optionPrefix) |
|
443 | /** |
||
444 | * Gets the package name suffix |
||
445 | * @param bool $ucFirst ucfirst package name suffix or not |
||
446 | * @return string |
||
447 | */ |
||
448 | 828 | public function getOptionSuffix($ucFirst = true) |
|
452 | /** |
||
453 | * Sets the package name suffix |
||
454 | * @param string $optionSuffix |
||
455 | * @return Generator |
||
456 | */ |
||
457 | 42 | public function setOptionSuffix($optionSuffix) |
|
462 | /** |
||
463 | * Gets the optionBasicLogin value |
||
464 | * @return string |
||
465 | */ |
||
466 | 852 | public function getOptionBasicLogin() |
|
470 | /** |
||
471 | * Sets the optionBasicLogin value |
||
472 | * @param string $optionBasicLogin |
||
473 | * @return Generator |
||
474 | */ |
||
475 | 6 | public function setOptionBasicLogin($optionBasicLogin) |
|
480 | /** |
||
481 | * Gets the optionBasicPassword value |
||
482 | * @return string |
||
483 | */ |
||
484 | 852 | public function getOptionBasicPassword() |
|
488 | /** |
||
489 | * Sets the optionBasicPassword value |
||
490 | * @param string $optionBasicPassword |
||
491 | * @return Generator |
||
492 | */ |
||
493 | 6 | public function setOptionBasicPassword($optionBasicPassword) |
|
498 | /** |
||
499 | * Gets the optionProxyHost value |
||
500 | * @return string |
||
501 | */ |
||
502 | 852 | public function getOptionProxyHost() |
|
506 | /** |
||
507 | * Sets the optionProxyHost value |
||
508 | * @param string $optionProxyHost |
||
509 | * @return Generator |
||
510 | */ |
||
511 | 6 | public function setOptionProxyHost($optionProxyHost) |
|
516 | /** |
||
517 | * Gets the optionProxyPort value |
||
518 | * @return string |
||
519 | */ |
||
520 | 852 | public function getOptionProxyPort() |
|
524 | /** |
||
525 | * Sets the optionProxyPort value |
||
526 | * @param string $optionProxyPort |
||
527 | * @return Generator |
||
528 | */ |
||
529 | 6 | public function setOptionProxyPort($optionProxyPort) |
|
534 | /** |
||
535 | * Gets the optionProxyLogin value |
||
536 | * @return string |
||
537 | */ |
||
538 | 852 | public function getOptionProxyLogin() |
|
542 | /** |
||
543 | * Sets the optionProxyLogin value |
||
544 | * @param string $optionProxyLogin |
||
545 | * @return Generator |
||
546 | */ |
||
547 | 6 | public function setOptionProxyLogin($optionProxyLogin) |
|
552 | /** |
||
553 | * Gets the optionProxyPassword value |
||
554 | * @return string |
||
555 | */ |
||
556 | 852 | public function getOptionProxyPassword() |
|
560 | /** |
||
561 | * Sets the optionProxyPassword value |
||
562 | * @param string $optionProxyPassword |
||
563 | * @return Generator |
||
564 | */ |
||
565 | 6 | public function setOptionProxyPassword($optionProxyPassword) |
|
570 | /** |
||
571 | * Gets the optionOrigin value |
||
572 | * @return string |
||
573 | */ |
||
574 | 846 | public function getOptionOrigin() |
|
578 | /** |
||
579 | * Sets the optionOrigin value |
||
580 | * @param string $optionOrigin |
||
581 | * @return Generator |
||
582 | */ |
||
583 | 6 | public function setOptionOrigin($optionOrigin) |
|
589 | /** |
||
590 | * Gets the optionDestination value |
||
591 | * @return string |
||
592 | */ |
||
593 | 486 | public function getOptionDestination() |
|
601 | /** |
||
602 | * Sets the optionDestination value |
||
603 | * @param string $optionDestination |
||
604 | * @return Generator |
||
605 | */ |
||
606 | 18 | public function setOptionDestination($optionDestination) |
|
615 | /** |
||
616 | * Gets the optionSrcDiname value |
||
617 | * @return string |
||
618 | */ |
||
619 | 426 | public function getOptionSrcDirname() |
|
623 | /** |
||
624 | * Sets the optionSrcDirname value |
||
625 | * @param string $optionSrcDirname |
||
626 | * @return Generator |
||
627 | */ |
||
628 | 18 | public function setOptionSrcDirname($optionSrcDirname) |
|
629 | { |
||
630 | 18 | $this->options->setSrcDirname($optionSrcDirname); |
|
631 | 18 | return $this; |
|
632 | } |
||
633 | /** |
||
634 | * Gets the optionSoapOptions value |
||
635 | * @return string |
||
636 | */ |
||
637 | 840 | public function getOptionSoapOptions() |
|
638 | { |
||
639 | 840 | return $this->options->getSoapOptions(); |
|
640 | } |
||
641 | /** |
||
642 | * Sets the optionSoapOptions value |
||
643 | * @param array $optionSoapOptions |
||
644 | * @return Generator |
||
645 | */ |
||
646 | 6 | public function setOptionSoapOptions($optionSoapOptions) |
|
647 | { |
||
648 | 6 | $this->options->setSoapOptions($optionSoapOptions); |
|
649 | 6 | if ($this->getSoapClient() instanceof GeneratorSoapClient) { |
|
650 | 6 | $this->getSoapClient()->initSoapClient(); |
|
651 | 4 | } |
|
652 | 6 | return $this; |
|
653 | } |
||
654 | /** |
||
655 | * Gets the optionComposerName value |
||
656 | * @return string |
||
657 | */ |
||
658 | 78 | public function getOptionComposerName() |
|
659 | { |
||
660 | 78 | return $this->options->getComposerName(); |
|
661 | } |
||
662 | /** |
||
663 | * Sets the optionComposerName value |
||
664 | * @param string $optionComposerName |
||
665 | * @return Generator |
||
666 | */ |
||
667 | 42 | public function setOptionComposerName($optionComposerName) |
|
668 | { |
||
669 | 42 | if (!empty($optionComposerName)) { |
|
670 | 36 | $this->options->setComposerName($optionComposerName); |
|
671 | 24 | } else { |
|
672 | 6 | throw new \InvalidArgumentException('Package\'s composer name can\'t be empty', __LINE__); |
|
673 | } |
||
674 | 36 | return $this; |
|
675 | } |
||
676 | /** |
||
677 | * Gets the optionComposerSettings value |
||
678 | * @return array |
||
679 | */ |
||
680 | 60 | public function getOptionComposerSettings() |
|
684 | /** |
||
685 | * Sets the optionComposerSettings value |
||
686 | * @param array $optionComposerSettings |
||
687 | * @return Generator |
||
688 | */ |
||
689 | 12 | public function setOptionComposerSettings(array $optionComposerSettings = array()) |
|
694 | /** |
||
695 | * Gets the optionStructsFolder value |
||
696 | * @return string |
||
697 | */ |
||
698 | 420 | public function getOptionStructsFolder() |
|
702 | /** |
||
703 | * Sets the optionStructsFolder value |
||
704 | * @param string $optionStructsFolder |
||
705 | * @return Generator |
||
706 | */ |
||
707 | 6 | public function setOptionStructsFolder($optionStructsFolder) |
|
712 | /** |
||
713 | * Gets the optionArraysFolder value |
||
714 | * @return string |
||
715 | */ |
||
716 | 84 | public function getOptionArraysFolder() |
|
720 | /** |
||
721 | * Sets the optionArraysFolder value |
||
722 | * @param string $optionArraysFolder |
||
723 | * @return Generator |
||
724 | */ |
||
725 | 6 | public function setOptionArraysFolder($optionArraysFolder) |
|
730 | /** |
||
731 | * Gets the optionEnumsFolder value |
||
732 | * @return string |
||
733 | */ |
||
734 | 162 | public function getOptionEnumsFolder() |
|
738 | /** |
||
739 | * Sets the optionEnumsFolder value |
||
740 | * @param string $optionEnumsFolder |
||
741 | * @return Generator |
||
742 | */ |
||
743 | 6 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
748 | /** |
||
749 | * Gets the optionServicesFolder value |
||
750 | * @return string |
||
751 | */ |
||
752 | 804 | public function getOptionServicesFolder() |
|
756 | /** |
||
757 | * Sets the optionServicesFolder value |
||
758 | * @param string $optionServicesFolder |
||
759 | * @return Generator |
||
760 | */ |
||
761 | 6 | public function setOptionServicesFolder($optionServicesFolder) |
|
766 | /** |
||
767 | * Gets the WSDL |
||
768 | * @return Wsdl|null |
||
769 | */ |
||
770 | 738 | public function getWsdl() |
|
771 | { |
||
774 | /** |
||
775 | * Sets the WSDLs |
||
776 | * @param Wsdl $wsdl |
||
777 | * @return Generator |
||
778 | */ |
||
779 | 822 | protected function setWsdl(Wsdl $wsdl) |
|
784 | /** |
||
785 | * Adds Wsdl location |
||
786 | * @param Wsdl $wsdl |
||
787 | * @param string $schemaLocation |
||
788 | * @return Generator |
||
789 | */ |
||
790 | 288 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
797 | /** |
||
798 | * Gets gather name class |
||
799 | * @param AbstractModel $model the model for which we generate the folder |
||
800 | * @return string |
||
801 | */ |
||
802 | 714 | private function getGather(AbstractModel $model) |
|
806 | /** |
||
807 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
808 | * @uses Generator::getGather() |
||
809 | * @param string $methodName original operation/method name |
||
810 | * @return string |
||
811 | */ |
||
812 | 738 | public function getServiceName($methodName) |
|
816 | /** |
||
817 | * @param GeneratorOptions $options |
||
818 | * @return Generator |
||
819 | */ |
||
820 | 828 | protected function setOptions(GeneratorOptions $options = null) |
|
825 | /** |
||
826 | * @return GeneratorOptions |
||
827 | */ |
||
828 | 882 | public function getOptions() |
|
832 | /** |
||
833 | * @return GeneratorSoapClient |
||
834 | */ |
||
835 | 774 | public function getSoapClient() |
|
839 | /** |
||
840 | * @param string $url |
||
841 | * @return string |
||
842 | */ |
||
843 | 834 | public function getUrlContent($url) |
|
852 | } |
||
853 |