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 | 504 | public function __construct(GeneratorOptions $options) |
|
53 | { |
||
54 | 504 | $this |
|
55 | 504 | ->setOptions($options) |
|
56 | 504 | ->initialize(); |
|
57 | 496 | } |
|
58 | /** |
||
59 | * @return Generator |
||
60 | */ |
||
61 | 504 | protected function initialize() |
|
62 | { |
||
63 | 504 | return $this |
|
64 | 504 | ->initContainers() |
|
65 | 504 | ->initParsers() |
|
66 | 504 | ->initFiles() |
|
67 | 504 | ->initSoapClient() |
|
68 | 500 | ->initWsdl(); |
|
69 | 20 | } |
|
70 | /** |
||
71 | * @throws \InvalidArgumentException |
||
72 | * @return Generator |
||
73 | */ |
||
74 | 504 | protected function initSoapClient() |
|
75 | { |
||
76 | 504 | if (!isset($this->soapClient)) { |
|
77 | 504 | $this->soapClient = new GeneratorSoapClient($this); |
|
78 | 500 | } |
|
79 | 500 | return $this; |
|
80 | } |
||
81 | /** |
||
82 | * @return Generator |
||
83 | */ |
||
84 | 504 | protected function initContainers() |
|
85 | { |
||
86 | 504 | if (!isset($this->containers)) { |
|
87 | 504 | $this->containers = new GeneratorContainers($this); |
|
88 | 504 | } |
|
89 | 504 | return $this; |
|
90 | } |
||
91 | /** |
||
92 | * @return Generator |
||
93 | */ |
||
94 | 504 | protected function initParsers() |
|
95 | { |
||
96 | 504 | if (!isset($this->parsers)) { |
|
97 | 504 | $this->parsers = new GeneratorParsers($this); |
|
98 | 504 | } |
|
99 | 504 | return $this; |
|
100 | } |
||
101 | /** |
||
102 | * @return GeneratorParsers |
||
103 | */ |
||
104 | 180 | public function getParsers() |
|
108 | /** |
||
109 | * @return Generator |
||
110 | */ |
||
111 | 504 | protected function initFiles() |
|
112 | { |
||
113 | 504 | if (!isset($this->files)) { |
|
114 | 504 | $this->files = new GeneratorFiles($this); |
|
115 | 504 | } |
|
116 | 504 | return $this; |
|
117 | } |
||
118 | /** |
||
119 | * @return GeneratorFiles |
||
120 | */ |
||
121 | 44 | public function getFiles() |
|
122 | { |
||
123 | 44 | return $this->files; |
|
124 | } |
||
125 | /** |
||
126 | * @throws \InvalidArgumentException |
||
127 | * @return Generator |
||
128 | */ |
||
129 | 24 | protected function initDirectory() |
|
130 | { |
||
131 | 24 | Utils::createDirectory($this->getOptions()->getDestination()); |
|
132 | 24 | if (!is_writable($this->getOptionDestination())) { |
|
133 | 4 | throw new \InvalidArgumentException(sprintf('Unable to use dir "%s" as dir does not exists, its creation has been impossible or it\'s not writable', $this->getOptionDestination()), __LINE__); |
|
134 | } |
||
135 | 20 | return $this; |
|
136 | } |
||
137 | /** |
||
138 | * @return Generator |
||
139 | */ |
||
140 | 504 | protected function initWsdl() |
|
141 | { |
||
142 | 504 | $this->setWsdl(new Wsdl($this, $this->getOptionOrigin(), $this->getUrlContent($this->getOptionOrigin()))); |
|
143 | 500 | return $this; |
|
144 | } |
||
145 | /** |
||
146 | * @return Generator |
||
147 | */ |
||
148 | 32 | protected function doSanityChecks() |
|
149 | { |
||
150 | 32 | $destination = $this->getOptionDestination(); |
|
151 | 32 | if (empty($destination)) { |
|
152 | 4 | throw new \InvalidArgumentException('Package\'s destination must be defined', __LINE__); |
|
153 | } |
||
154 | 28 | $composerName = $this->getOptionComposerName(); |
|
155 | 28 | if ($this->getOptionStandalone() && empty($composerName)) { |
|
156 | 4 | throw new \InvalidArgumentException('Package\'s composer name must be defined', __LINE__); |
|
157 | } |
||
158 | 24 | return $this; |
|
159 | } |
||
160 | /** |
||
161 | * @return Generator |
||
162 | */ |
||
163 | 20 | protected function doParse() |
|
164 | { |
||
165 | 20 | $this->parsers->doParse(); |
|
166 | 20 | return $this; |
|
167 | } |
||
168 | /** |
||
169 | * @return Generator |
||
170 | */ |
||
171 | 20 | protected function doGenerate() |
|
172 | { |
||
173 | 20 | $this->files->doGenerate(); |
|
174 | 20 | return $this; |
|
175 | } |
||
176 | /** |
||
177 | * Generates all classes based on options |
||
178 | * @return Generator |
||
179 | */ |
||
180 | 32 | public function generatePackage() |
|
181 | { |
||
182 | 32 | return $this |
|
183 | 32 | ->doSanityChecks() |
|
184 | 24 | ->initDirectory() |
|
185 | 20 | ->doParse() |
|
186 | 20 | ->doGenerate(); |
|
187 | } |
||
188 | /** |
||
189 | * Gets the struct by its name |
||
190 | * @uses Generator::getStructs() |
||
191 | * @param string $structName the original struct name |
||
192 | * @return Struct|null |
||
193 | */ |
||
194 | 376 | public function getStruct($structName) |
|
195 | { |
||
196 | 376 | return $this->getStructs()->getStructByName($structName); |
|
197 | } |
||
198 | /** |
||
199 | * Gets a service by its name |
||
200 | * @param string $serviceName the service name |
||
201 | * @return Service|null |
||
202 | */ |
||
203 | 332 | public function getService($serviceName) |
|
204 | { |
||
205 | 332 | return $this->getServices()->getServiceByName($serviceName); |
|
206 | } |
||
207 | /** |
||
208 | * Returns the method |
||
209 | * @uses Generator::getServiceName() |
||
210 | * @uses Generator::getService() |
||
211 | * @uses Service::getMethod() |
||
212 | * @param string $methodName the original function name |
||
213 | * @return Method|null |
||
214 | */ |
||
215 | 328 | public function getServiceMethod($methodName) |
|
216 | { |
||
217 | 328 | return $this->getService($this->getServiceName($methodName)) instanceof Service ? $this->getService($this->getServiceName($methodName))->getMethod($methodName) : null; |
|
218 | } |
||
219 | /** |
||
220 | * @return ServiceContainer |
||
221 | */ |
||
222 | 464 | public function getServices() |
|
223 | { |
||
224 | 464 | return $this->containers->getServices(); |
|
225 | } |
||
226 | /** |
||
227 | * @return StructContainer |
||
228 | */ |
||
229 | 444 | public function getStructs() |
|
230 | { |
||
231 | 444 | return $this->containers->getStructs(); |
|
232 | } |
||
233 | /** |
||
234 | * Sets the optionCategory value |
||
235 | * @return string |
||
236 | */ |
||
237 | 276 | public function getOptionCategory() |
|
238 | { |
||
239 | 276 | return $this->options->getCategory(); |
|
240 | } |
||
241 | /** |
||
242 | * Sets the optionCategory value |
||
243 | * @param string $category |
||
244 | * @return Generator |
||
245 | */ |
||
246 | 272 | public function setOptionCategory($category) |
|
247 | { |
||
248 | 272 | $this->options->setCategory($category); |
|
249 | 272 | return $this; |
|
250 | } |
||
251 | /** |
||
252 | * Sets the optionGatherMethods value |
||
253 | * @return string |
||
254 | */ |
||
255 | 472 | public function getOptionGatherMethods() |
|
256 | { |
||
257 | 472 | return $this->options->getGatherMethods(); |
|
258 | } |
||
259 | /** |
||
260 | * Sets the optionGatherMethods value |
||
261 | * @param string $gatherMethods |
||
262 | * @return Generator |
||
263 | */ |
||
264 | 272 | public function setOptionGatherMethods($gatherMethods) |
|
265 | { |
||
266 | 272 | $this->options->setGatherMethods($gatherMethods); |
|
267 | 272 | return $this; |
|
268 | } |
||
269 | /** |
||
270 | * Gets the optionGenericConstantsNames value |
||
271 | * @return bool |
||
272 | */ |
||
273 | 60 | public function getOptionGenericConstantsNames() |
|
274 | { |
||
275 | 60 | return $this->options->getGenericConstantsName(); |
|
276 | } |
||
277 | /** |
||
278 | * Sets the optionGenericConstantsNames value |
||
279 | * @param bool $genericConstantsNames |
||
280 | * @return Generator |
||
281 | */ |
||
282 | 8 | public function setOptionGenericConstantsNames($genericConstantsNames) |
|
283 | { |
||
284 | 8 | $this->options->setGenericConstantsName($genericConstantsNames); |
|
285 | 8 | return $this; |
|
286 | } |
||
287 | /** |
||
288 | * Gets the optionGenerateTutorialFile value |
||
289 | * @return bool |
||
290 | */ |
||
291 | 28 | public function getOptionGenerateTutorialFile() |
|
292 | { |
||
293 | 28 | return $this->options->getGenerateTutorialFile(); |
|
294 | } |
||
295 | /** |
||
296 | * Sets the optionGenerateTutorialFile value |
||
297 | * @param bool $generateTutorialFile |
||
298 | * @return Generator |
||
299 | */ |
||
300 | 4 | public function setOptionGenerateTutorialFile($generateTutorialFile) |
|
301 | { |
||
302 | 4 | $this->options->setGenerateTutorialFile($generateTutorialFile); |
|
303 | 4 | return $this; |
|
304 | } |
||
305 | /** |
||
306 | * Gets the optionNamespacePrefix value |
||
307 | * @return string |
||
308 | */ |
||
309 | 252 | public function getOptionNamespacePrefix() |
|
310 | { |
||
311 | 252 | return $this->options->getNamespace(); |
|
312 | } |
||
313 | /** |
||
314 | * Sets the optionGenerateTutorialFile value |
||
315 | * @param string $namespace |
||
316 | * @return Generator |
||
317 | */ |
||
318 | 20 | public function setOptionNamespacePrefix($namespace) |
|
319 | { |
||
320 | 20 | $this->options->setNamespace($namespace); |
|
321 | 20 | return $this; |
|
322 | } |
||
323 | /** |
||
324 | * Gets the optionAddComments value |
||
325 | * @return array |
||
326 | */ |
||
327 | 204 | public function getOptionAddComments() |
|
328 | { |
||
329 | 204 | return $this->options->getAddComments(); |
|
330 | } |
||
331 | /** |
||
332 | * Sets the optionAddComments value |
||
333 | * @param array $addComments |
||
334 | * @return Generator |
||
335 | */ |
||
336 | 272 | public function setOptionAddComments($addComments) |
|
337 | { |
||
338 | 272 | $this->options->setAddComments($addComments); |
|
339 | 272 | return $this; |
|
340 | } |
||
341 | /** |
||
342 | * Gets the optionStandalone value |
||
343 | * @return bool |
||
344 | */ |
||
345 | 60 | public function getOptionStandalone() |
|
346 | { |
||
347 | 60 | return $this->options->getStandalone(); |
|
348 | } |
||
349 | /** |
||
350 | * Sets the optionStandalone value |
||
351 | * @param bool $standalone |
||
352 | * @return Generator |
||
353 | */ |
||
354 | 8 | public function setOptionStandalone($standalone) |
|
355 | { |
||
356 | 8 | $this->options->setStandalone($standalone); |
|
357 | 8 | return $this; |
|
358 | } |
||
359 | /** |
||
360 | * Gets the optionValidation value |
||
361 | * @return bool |
||
362 | */ |
||
363 | 112 | public function getOptionValidation() |
|
367 | /** |
||
368 | * Sets the optionValidation value |
||
369 | * @param bool $validation |
||
370 | * @return Generator |
||
371 | */ |
||
372 | 12 | public function setOptionValidation($validation) |
|
377 | /** |
||
378 | * Gets the optionStructClass value |
||
379 | * @return string |
||
380 | */ |
||
381 | 92 | public function getOptionStructClass() |
|
382 | { |
||
383 | 92 | return $this->options->getStructClass(); |
|
384 | } |
||
385 | /** |
||
386 | * Sets the optionStructClass value |
||
387 | * @param string $structClass |
||
388 | * @return Generator |
||
389 | */ |
||
390 | 12 | public function setOptionStructClass($structClass) |
|
391 | { |
||
392 | 12 | $this->options->setStructClass($structClass); |
|
393 | 12 | return $this; |
|
394 | } |
||
395 | /** |
||
396 | * Gets the optionStructArrayClass value |
||
397 | * @return string |
||
398 | */ |
||
399 | 36 | public function getOptionStructArrayClass() |
|
400 | { |
||
401 | 36 | return $this->options->getStructArrayClass(); |
|
402 | } |
||
403 | /** |
||
404 | * Sets the optionStructArrayClass value |
||
405 | * @param string $structArrayClass |
||
406 | * @return Generator |
||
407 | */ |
||
408 | 4 | public function setOptionStructArrayClass($structArrayClass) |
|
409 | { |
||
410 | 4 | $this->options->setStructArrayClass($structArrayClass); |
|
411 | 4 | return $this; |
|
412 | } |
||
413 | /** |
||
414 | * Gets the optionSoapClientClass value |
||
415 | * @return string |
||
416 | */ |
||
417 | 92 | public function getOptionSoapClientClass() |
|
421 | /** |
||
422 | * Sets the optionSoapClientClass value |
||
423 | * @param string $soapClientClass |
||
424 | * @return Generator |
||
425 | */ |
||
426 | 4 | public function setOptionSoapClientClass($soapClientClass) |
|
431 | /** |
||
432 | * Gets the package name prefix |
||
433 | * @param bool $ucFirst ucfirst package name prefix or not |
||
434 | * @return string |
||
435 | */ |
||
436 | 532 | public function getOptionPrefix($ucFirst = true) |
|
440 | /** |
||
441 | * Sets the package name prefix |
||
442 | * @param string $optionPrefix |
||
443 | * @return Generator |
||
444 | */ |
||
445 | 292 | public function setOptionPrefix($optionPrefix) |
|
450 | /** |
||
451 | * Gets the package name suffix |
||
452 | * @param bool $ucFirst ucfirst package name suffix or not |
||
453 | * @return string |
||
454 | */ |
||
455 | 520 | public function getOptionSuffix($ucFirst = true) |
|
459 | /** |
||
460 | * Sets the package name suffix |
||
461 | * @param string $optionSuffix |
||
462 | * @return Generator |
||
463 | */ |
||
464 | 28 | public function setOptionSuffix($optionSuffix) |
|
469 | /** |
||
470 | * Gets the optionBasicLogin value |
||
471 | * @return string |
||
472 | */ |
||
473 | 520 | public function getOptionBasicLogin() |
|
477 | /** |
||
478 | * Sets the optionBasicLogin value |
||
479 | * @param string $optionBasicLogin |
||
480 | * @return Generator |
||
481 | */ |
||
482 | 4 | public function setOptionBasicLogin($optionBasicLogin) |
|
487 | /** |
||
488 | * Gets the optionBasicPassword value |
||
489 | * @return string |
||
490 | */ |
||
491 | 520 | public function getOptionBasicPassword() |
|
495 | /** |
||
496 | * Sets the optionBasicPassword value |
||
497 | * @param string $optionBasicPassword |
||
498 | * @return Generator |
||
499 | */ |
||
500 | 4 | public function setOptionBasicPassword($optionBasicPassword) |
|
505 | /** |
||
506 | * Gets the optionProxyHost value |
||
507 | * @return string |
||
508 | */ |
||
509 | 520 | public function getOptionProxyHost() |
|
513 | /** |
||
514 | * Sets the optionProxyHost value |
||
515 | * @param string $optionProxyHost |
||
516 | * @return Generator |
||
517 | */ |
||
518 | 4 | public function setOptionProxyHost($optionProxyHost) |
|
523 | /** |
||
524 | * Gets the optionProxyPort value |
||
525 | * @return string |
||
526 | */ |
||
527 | 520 | public function getOptionProxyPort() |
|
531 | /** |
||
532 | * Sets the optionProxyPort value |
||
533 | * @param string $optionProxyPort |
||
534 | * @return Generator |
||
535 | */ |
||
536 | 4 | public function setOptionProxyPort($optionProxyPort) |
|
541 | /** |
||
542 | * Gets the optionProxyLogin value |
||
543 | * @return string |
||
544 | */ |
||
545 | 520 | public function getOptionProxyLogin() |
|
549 | /** |
||
550 | * Sets the optionProxyLogin value |
||
551 | * @param string $optionProxyLogin |
||
552 | * @return Generator |
||
553 | */ |
||
554 | 4 | public function setOptionProxyLogin($optionProxyLogin) |
|
559 | /** |
||
560 | * Gets the optionProxyPassword value |
||
561 | * @return string |
||
562 | */ |
||
563 | 520 | public function getOptionProxyPassword() |
|
567 | /** |
||
568 | * Sets the optionProxyPassword value |
||
569 | * @param string $optionProxyPassword |
||
570 | * @return Generator |
||
571 | */ |
||
572 | 4 | public function setOptionProxyPassword($optionProxyPassword) |
|
577 | /** |
||
578 | * Gets the optionOrigin value |
||
579 | * @return string |
||
580 | */ |
||
581 | 516 | public function getOptionOrigin() |
|
585 | /** |
||
586 | * Sets the optionOrigin value |
||
587 | * @param string $optionOrigin |
||
588 | * @return Generator |
||
589 | */ |
||
590 | 4 | public function setOptionOrigin($optionOrigin) |
|
596 | /** |
||
597 | * Gets the optionDestination value |
||
598 | * @return string |
||
599 | */ |
||
600 | 292 | public function getOptionDestination() |
|
608 | /** |
||
609 | * Sets the optionDestination value |
||
610 | * @param string $optionDestination |
||
611 | * @return Generator |
||
612 | */ |
||
613 | 12 | public function setOptionDestination($optionDestination) |
|
622 | /** |
||
623 | * Gets the optionSoapOptions value |
||
624 | * @return string |
||
625 | */ |
||
626 | 512 | public function getOptionSoapOptions() |
|
630 | /** |
||
631 | * Sets the optionSoapOptions value |
||
632 | * @param array $optionSoapOptions |
||
633 | * @return Generator |
||
634 | */ |
||
635 | 4 | public function setOptionSoapOptions($optionSoapOptions) |
|
643 | /** |
||
644 | * Gets the optionComposerName value |
||
645 | * @return string |
||
646 | */ |
||
647 | 36 | public function getOptionComposerName() |
|
651 | /** |
||
652 | * Sets the optionComposerName value |
||
653 | * @param string $optionComposerName |
||
654 | * @return Generator |
||
655 | */ |
||
656 | 16 | public function setOptionComposerName($optionComposerName) |
|
665 | /** |
||
666 | * Gets the optionStructsFolder value |
||
667 | * @return string |
||
668 | */ |
||
669 | 260 | public function getOptionStructsFolder() |
|
673 | /** |
||
674 | * Sets the optionStructsFolder value |
||
675 | * @param string $optionStructsFolder |
||
676 | * @return Generator |
||
677 | */ |
||
678 | 4 | public function setOptionStructsFolder($optionStructsFolder) |
|
683 | /** |
||
684 | * Gets the optionArraysFolder value |
||
685 | * @return string |
||
686 | */ |
||
687 | 52 | public function getOptionArraysFolder() |
|
691 | /** |
||
692 | * Sets the optionArraysFolder value |
||
693 | * @param string $optionArraysFolder |
||
694 | * @return Generator |
||
695 | */ |
||
696 | 4 | public function setOptionArraysFolder($optionArraysFolder) |
|
701 | /** |
||
702 | * Gets the optionEnumsFolder value |
||
703 | * @return string |
||
704 | */ |
||
705 | 92 | public function getOptionEnumsFolder() |
|
709 | /** |
||
710 | * Sets the optionEnumsFolder value |
||
711 | * @param string $optionEnumsFolder |
||
712 | * @return Generator |
||
713 | */ |
||
714 | 4 | public function setOptionEnumsFolder($optionEnumsFolder) |
|
719 | /** |
||
720 | * Gets the optionServicesFolder value |
||
721 | * @return string |
||
722 | */ |
||
723 | 512 | public function getOptionServicesFolder() |
|
727 | /** |
||
728 | * Sets the optionServicesFolder value |
||
729 | * @param string $optionServicesFolder |
||
730 | * @return Generator |
||
731 | */ |
||
732 | 4 | public function setOptionServicesFolder($optionServicesFolder) |
|
737 | /** |
||
738 | * Gets the WSDL |
||
739 | * @return Wsdl|null |
||
740 | */ |
||
741 | 452 | public function getWsdl() |
|
745 | /** |
||
746 | * Sets the WSDLs |
||
747 | * @param Wsdl $wsdl |
||
748 | * @return Generator |
||
749 | */ |
||
750 | 500 | protected function setWsdl(Wsdl $wsdl) |
|
755 | /** |
||
756 | * Adds Wsdl location |
||
757 | * @param Wsdl $wsdl |
||
758 | * @param string $schemaLocation |
||
759 | * @return Generator |
||
760 | */ |
||
761 | 180 | public function addSchemaToWsdl(Wsdl $wsdl, $schemaLocation) |
|
768 | /** |
||
769 | * Gets gather name class |
||
770 | * @param AbstractModel $model the model for which we generate the folder |
||
771 | * @return string |
||
772 | */ |
||
773 | 448 | private function getGather(AbstractModel $model) |
|
777 | /** |
||
778 | * Returns the service name associated to the method/operation name in order to gather them in one service class |
||
779 | * @uses Generator::getGather() |
||
780 | * @param string $methodName original operation/method name |
||
781 | * @return string |
||
782 | */ |
||
783 | 464 | public function getServiceName($methodName) |
|
787 | /** |
||
788 | * @param GeneratorOptions $options |
||
789 | * @return Generator |
||
790 | */ |
||
791 | 504 | protected function setOptions(GeneratorOptions $options = null) |
|
796 | /** |
||
797 | * @return GeneratorOptions |
||
798 | */ |
||
799 | 544 | public function getOptions() |
|
803 | /** |
||
804 | * @return GeneratorSoapClient |
||
805 | */ |
||
806 | 484 | public function getSoapClient() |
|
810 | /** |
||
811 | * @param string $url |
||
812 | * @return string |
||
813 | */ |
||
814 | 508 | public function getUrlContent($url) |
|
823 | } |
||
824 |