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 | 412 | public function __construct(GeneratorOptions $options) |
|
58 | /** |
||
59 | * @return Generator |
||
60 | */ |
||
61 | 412 | protected function initialize() |
|
70 | /** |
||
71 | * @throws \InvalidArgumentException |
||
72 | * @return Generator |
||
73 | */ |
||
74 | 404 | protected function initSoapClient() |
|
81 | /** |
||
82 | * @return Generator |
||
83 | */ |
||
84 | 412 | protected function initContainers() |
|
91 | /** |
||
92 | * @return Generator |
||
93 | */ |
||
94 | 412 | protected function initParsers() |
|
101 | /** |
||
102 | * @return Generator |
||
103 | */ |
||
104 | 412 | protected function initFiles() |
|
111 | /** |
||
112 | * @return GeneratorFiles |
||
113 | */ |
||
114 | 44 | public function getFiles() |
|
118 | /** |
||
119 | * @throws \InvalidArgumentException |
||
120 | * @return Generator |
||
121 | */ |
||
122 | 24 | protected function initDirectory() |
|
130 | /** |
||
131 | * @return Generator |
||
132 | */ |
||
133 | 416 | protected function initWsdl() |
|
138 | /** |
||
139 | * @return Generator |
||
140 | */ |
||
141 | 28 | protected function doSanityChecks() |
|
142 | { |
||
143 | 28 | $destination = $this->getOptionDestination(); |
|
144 | 28 | if (empty($destination)) { |
|
145 | throw new \InvalidArgumentException('Package\'s destination must be defined', __LINE__); |
||
146 | } |
||
147 | 28 | $composerName = $this->getOptionComposerName(); |
|
148 | 28 | if (empty($composerName)) { |
|
149 | 4 | throw new \InvalidArgumentException('Package\'s composer name must be defined', __LINE__); |
|
150 | } |
||
151 | 24 | return $this; |
|
152 | } |
||
153 | /** |
||
154 | * @return Generator |
||
155 | */ |
||
156 | 20 | protected function doParse() |
|
161 | /** |
||
162 | * @return Generator |
||
163 | */ |
||
164 | 20 | protected function doGenerate() |
|
169 | /** |
||
170 | * Generates all classes based on options |
||
171 | * @return Generator |
||
172 | */ |
||
173 | 28 | 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 | 304 | 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 | 276 | 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 | 272 | public function getServiceMethod($methodName) |
|
212 | /** |
||
213 | * @return ServiceContainer |
||
214 | */ |
||
215 | 376 | public function getServices() |
|
219 | /** |
||
220 | * @return StructContainer |
||
221 | */ |
||
222 | 356 | public function getStructs() |
|
226 | /** |
||
227 | * Sets the optionCategory value |
||
228 | * @return string |
||
229 | */ |
||
230 | 220 | public function getOptionCategory() |
|
234 | /** |
||
235 | * Sets the optionCategory value |
||
236 | * @param string $category |
||
237 | * @return Generator |
||
238 | */ |
||
239 | 216 | public function setOptionCategory($category) |
|
244 | /** |
||
245 | * Sets the optionGatherMethods value |
||
246 | * @return string |
||
247 | */ |
||
248 | 384 | public function getOptionGatherMethods() |
|
252 | /** |
||
253 | * Sets the optionGatherMethods value |
||
254 | * @param string $gatherMethods |
||
255 | * @return Generator |
||
256 | */ |
||
257 | 216 | public function setOptionGatherMethods($gatherMethods) |
|
262 | /** |
||
263 | * Gets the optionGenericConstantsNames value |
||
264 | * @return bool |
||
265 | */ |
||
266 | 56 | public function getOptionGenericConstantsNames() |
|
270 | /** |
||
271 | * Sets the optionGenericConstantsNames value |
||
272 | * @param bool $genericConstantsNames |
||
273 | * @return Generator |
||
274 | */ |
||
275 | 8 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
280 | /** |
||
281 | * Gets the optionGenerateTutorialFile value |
||
282 | * @return bool |
||
283 | */ |
||
284 | 28 | public function getOptionGenerateTutorialFile() |
|
288 | /** |
||
289 | * Sets the optionGenerateTutorialFile value |
||
290 | * @param bool $generateTutorialFile |
||
291 | * @return Generator |
||
292 | */ |
||
293 | 4 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
298 | /** |
||
299 | * Gets the optionNamespacePrefix value |
||
300 | * @return string |
||
301 | */ |
||
302 | 196 | public function getOptionNamespacePrefix() |
|
306 | /** |
||
307 | * Sets the optionGenerateTutorialFile value |
||
308 | * @param string $namespace |
||
309 | * @return Generator |
||
310 | */ |
||
311 | 12 | public function setOptionNamespacePrefix($namespace) |
|
316 | /** |
||
317 | * Gets the optionAddComments value |
||
318 | * @return array |
||
319 | */ |
||
320 | 156 | public function getOptionAddComments() |
|
324 | /** |
||
325 | * Sets the optionAddComments value |
||
326 | * @param array $addComments |
||
327 | * @return Generator |
||
328 | */ |
||
329 | 216 | public function setOptionAddComments($addComments) |
|
334 | /** |
||
335 | * Gets the optionStandalone value |
||
336 | * @return bool |
||
337 | */ |
||
338 | 52 | public function getOptionStandalone() |
|
342 | /** |
||
343 | * Sets the optionStandalone value |
||
344 | * @param bool $standalone |
||
345 | * @return Generator |
||
346 | */ |
||
347 | 8 | public function setOptionStandalone($standalone) |
|
352 | /** |
||
353 | * Gets the optionStructClass value |
||
354 | * @return string |
||
355 | */ |
||
356 | 56 | public function getOptionStructClass() |
|
360 | /** |
||
361 | * Sets the optionStructClass value |
||
362 | * @param string $structClass |
||
363 | * @return Generator |
||
364 | */ |
||
365 | 12 | public function setOptionStructClass($structClass) |
|
370 | /** |
||
371 | * Gets the optionStructArrayClass value |
||
372 | * @return string |
||
373 | */ |
||
374 | 36 | public function getOptionStructArrayClass() |
|
378 | /** |
||
379 | * Sets the optionStructArrayClass value |
||
380 | * @param string $structArrayClass |
||
381 | * @return Generator |
||
382 | */ |
||
383 | 4 | public function setOptionStructArrayClass($structArrayClass) |
|
388 | /** |
||
389 | * Gets the optionSoapClientClass value |
||
390 | * @return string |
||
391 | */ |
||
392 | 80 | public function getOptionSoapClientClass() |
|
396 | /** |
||
397 | * Sets the optionSoapClientClass value |
||
398 | * @param string $soapClientClass |
||
399 | * @return Generator |
||
400 | */ |
||
401 | 4 | public function setOptionSoapClientClass($soapClientClass) |
|
406 | /** |
||
407 | * Gets the package name prefix |
||
408 | * @param bool $ucFirst ucfirst package name prefix or not |
||
409 | * @return string |
||
410 | */ |
||
411 | 440 | public function getOptionPrefix($ucFirst = true) |
|
415 | /** |
||
416 | * Sets the package name prefix |
||
417 | * @param string $optionPrefix |
||
418 | * @return Generator |
||
419 | */ |
||
420 | 236 | public function setOptionPrefix($optionPrefix) |
|
425 | /** |
||
426 | * Gets the package name suffix |
||
427 | * @param bool $ucFirst ucfirst package name suffix or not |
||
428 | * @return string |
||
429 | */ |
||
430 | 428 | public function getOptionSuffix($ucFirst = true) |
|
431 | { |
||
432 | 428 | return $ucFirst ? ucfirst($this->getOptions()->getSuffix()) : $this->getOptions()->getSuffix(); |
|
433 | } |
||
434 | /** |
||
435 | * Sets the package name suffix |
||
436 | * @param string $optionSuffix |
||
437 | * @return Generator |
||
438 | */ |
||
439 | 28 | public function setOptionSuffix($optionSuffix) |
|
440 | { |
||
441 | 28 | $this->options->setSuffix($optionSuffix); |
|
442 | 28 | return $this; |
|
443 | } |
||
444 | /** |
||
445 | * Gets the optionBasicLogin value |
||
446 | * @return string |
||
447 | */ |
||
448 | 416 | public function getOptionBasicLogin() |
|
452 | /** |
||
453 | * Sets the optionBasicLogin value |
||
454 | * @param string $optionBasicLogin |
||
455 | * @return Generator |
||
456 | */ |
||
457 | 4 | public function setOptionBasicLogin($optionBasicLogin) |
|
462 | /** |
||
463 | * Gets the optionBasicPassword value |
||
464 | * @return string |
||
465 | */ |
||
466 | 416 | public function getOptionBasicPassword() |
|
470 | /** |
||
471 | * Sets the optionBasicPassword value |
||
472 | * @param string $optionBasicPassword |
||
473 | * @return Generator |
||
474 | */ |
||
475 | 4 | public function setOptionBasicPassword($optionBasicPassword) |
|
480 | /** |
||
481 | * Gets the optionProxyHost value |
||
482 | * @return string |
||
483 | */ |
||
484 | 416 | public function getOptionProxyHost() |
|
488 | /** |
||
489 | * Sets the optionProxyHost value |
||
490 | * @param string $optionProxyHost |
||
491 | * @return Generator |
||
492 | */ |
||
493 | 4 | public function setOptionProxyHost($optionProxyHost) |
|
498 | /** |
||
499 | * Gets the optionProxyPort value |
||
500 | * @return string |
||
501 | */ |
||
502 | 416 | public function getOptionProxyPort() |
|
506 | /** |
||
507 | * Sets the optionProxyPort value |
||
508 | * @param string $optionProxyPort |
||
509 | * @return Generator |
||
510 | */ |
||
511 | 4 | public function setOptionProxyPort($optionProxyPort) |
|
516 | /** |
||
517 | * Gets the optionProxyLogin value |
||
518 | * @return string |
||
519 | */ |
||
520 | 416 | public function getOptionProxyLogin() |
|
524 | /** |
||
525 | * Sets the optionProxyLogin value |
||
526 | * @param string $optionProxyLogin |
||
527 | * @return Generator |
||
528 | */ |
||
529 | 4 | public function setOptionProxyLogin($optionProxyLogin) |
|
534 | /** |
||
535 | * Gets the optionProxyPassword value |
||
536 | * @return string |
||
537 | */ |
||
538 | 416 | public function getOptionProxyPassword() |
|
542 | /** |
||
543 | * Sets the optionProxyPassword value |
||
544 | * @param string $optionProxyPassword |
||
545 | * @return Generator |
||
546 | */ |
||
547 | 4 | public function setOptionProxyPassword($optionProxyPassword) |
|
552 | /** |
||
553 | * Gets the optionOrigin value |
||
554 | * @return string |
||
555 | */ |
||
556 | 420 | public function getOptionOrigin() |
|
560 | /** |
||
561 | * Sets the optionOrigin value |
||
562 | * @param string $optionOrigin |
||
563 | * @return Generator |
||
564 | */ |
||
565 | 4 | public function setOptionOrigin($optionOrigin) |
|
571 | /** |
||
572 | * Gets the optionDestination value |
||
573 | * @return string |
||
574 | */ |
||
575 | 232 | public function getOptionDestination() |
|
583 | /** |
||
584 | * Sets the optionDestination value |
||
585 | * @param string $optionDestination |
||
586 | * @return Generator |
||
587 | */ |
||
588 | 12 | public function setOptionDestination($optionDestination) |
|
597 | /** |
||
598 | * Gets the optionSoapOptions value |
||
599 | * @return string |
||
600 | */ |
||
601 | 412 | public function getOptionSoapOptions() |
|
605 | /** |
||
606 | * Sets the optionSoapOptions value |
||
607 | * @param array $optionSoapOptions |
||
608 | * @return Generator |
||
609 | */ |
||
610 | 4 | public function setOptionSoapOptions($optionSoapOptions) |
|
615 | /** |
||
616 | * Gets the optionComposerName value |
||
617 | * @return string |
||
618 | */ |
||
619 | 36 | public function getOptionComposerName() |
|
623 | /** |
||
624 | * Sets the optionComposerName value |
||
625 | * @param string $optionComposerName |
||
626 | * @return Generator |
||
627 | */ |
||
628 | 16 | public function setOptionComposerName($optionComposerName) |
|
637 | /** |
||
638 | * Gets the optionStructsFolder value |
||
639 | * @return string |
||
640 | */ |
||
641 | 204 | public function getOptionStructsFolder() |
|
645 | /** |
||
646 | * Sets the optionStructsFolder value |
||
647 | * @param string $optionStructsFolder |
||
648 | * @return Generator |
||
649 | */ |
||
650 | 4 | public function setOptionStructsFolder($optionStructsFolder) |
|
655 | /** |
||
656 | * Gets the optionArraysFolder value |
||
657 | * @return string |
||
658 | */ |
||
659 | 48 | public function getOptionArraysFolder() |
|
663 | /** |
||
664 | * Sets the optionArraysFolder value |
||
665 | * @param string $optionArraysFolder |
||
666 | * @return Generator |
||
667 | */ |
||
668 | 4 | public function setOptionArraysFolder($optionArraysFolder) |
|
673 | /** |
||
674 | * Gets the optionEnumsFolder value |
||
675 | * @return string |
||
676 | */ |
||
677 | 76 | public function getOptionEnumsFolder() |
|
681 | /** |
||
682 | * Sets the optionEnumsFolder value |
||
683 | * @param string $optionEnumsFolder |
||
684 | * @return Generator |
||
685 | */ |
||
686 | 4 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
691 | /** |
||
692 | * Gets the optionServicesFolder value |
||
693 | * @return string |
||
694 | */ |
||
695 | 424 | public function getOptionServicesFolder() |
|
699 | /** |
||
700 | * Sets the optionServicesFolder value |
||
701 | * @param string $optionServicesFolder |
||
702 | * @return Generator |
||
703 | */ |
||
704 | 4 | public function setOptionServicesFolder($optionServicesFolder) |
|
709 | /** |
||
710 | * Gets the WSDL |
||
711 | * @return Wsdl|null |
||
712 | */ |
||
713 | 372 | public function getWsdl() |
|
717 | /** |
||
718 | * Sets the WSDLs |
||
719 | * @param Wsdl $wsdl |
||
720 | * @return Generator |
||
721 | */ |
||
722 | 408 | protected function setWsdl(Wsdl $wsdl) |
|
727 | /** |
||
728 | * Adds Wsdl location |
||
729 | * @param Wsdl $wsdl |
||
730 | * @param string $schemaLocation |
||
731 | * @return Generator |
||
732 | */ |
||
733 | 120 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
740 | /** |
||
741 | * Gets gather name class |
||
742 | * @param AbstractModel $model the model for which we generate the folder |
||
743 | * @return string |
||
744 | */ |
||
745 | 360 | private function getGather(AbstractModel $model) |
|
749 | /** |
||
750 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
751 | * @uses Generator::getGather() |
||
752 | * @param string $methodName original operation/method name |
||
753 | * @return string |
||
754 | */ |
||
755 | 376 | public function getServiceName($methodName) |
|
759 | /** |
||
760 | * @param GeneratorOptions $options |
||
761 | * @return Generator |
||
762 | */ |
||
763 | 412 | protected function setOptions(GeneratorOptions $options = null) |
|
768 | /** |
||
769 | * @return GeneratorOptions |
||
770 | */ |
||
771 | 452 | public function getOptions() |
|
775 | /** |
||
776 | * @return GeneratorSoapClient |
||
777 | */ |
||
778 | 380 | public function getSoapClient() |
|
782 | /** |
||
783 | * @param string $url |
||
784 | * @return string |
||
785 | */ |
||
786 | 420 | public function getUrlContent($url) |
|
795 | } |
||
796 |