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 declare(strict_types=1); |
||
5 | class Generator |
||
6 | { |
||
7 | /** Single quote for string value **/ |
||
8 | const QUOTE_SINGLE = "'"; |
||
9 | |||
10 | /** Double quote for string value **/ |
||
11 | const QUOTE_DOUBLE = '"'; |
||
12 | |||
13 | /** No quote for string heredoc value **/ |
||
14 | const QUOTE_NO = ''; |
||
15 | |||
16 | |||
17 | /** @var string Generated code */ |
||
18 | public $code = ''; |
||
19 | |||
20 | /** @var integer Level of code line tabbing for new lines */ |
||
21 | public $tabs = 0; |
||
22 | |||
23 | /** @var string Current class name */ |
||
24 | public $class; |
||
25 | |||
26 | /** @var int Current conditions nesting level */ |
||
27 | public $ifConditionLevel = 0; |
||
28 | |||
29 | /** |
||
30 | * Add simple text to current code position |
||
31 | * @param string $text Text to add |
||
32 | * @return self |
||
33 | */ |
||
34 | public function text($text = '') |
||
40 | |||
41 | /** |
||
42 | * Add current tabbing level to current line. |
||
43 | * |
||
44 | * @param string $endText Text to add after tabs |
||
45 | * @param integer $tabs Amount of tabs to add |
||
46 | * @param string $startText Text to add before tabs |
||
47 | * @return Generator Chaining |
||
48 | */ |
||
49 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
59 | |||
60 | /** |
||
61 | * Increase current code indentation. |
||
62 | * |
||
63 | * @param int $amount Indentation amount |
||
64 | * |
||
65 | * @return $this Chaining |
||
66 | */ |
||
67 | public function increaseIndentation($amount = 1) |
||
73 | |||
74 | /** |
||
75 | * Reduce current code indentation. |
||
76 | * |
||
77 | * @param int $amount Indentation amount |
||
78 | * |
||
79 | * @return $this Chaining |
||
80 | */ |
||
81 | public function decreaseIndentation($amount = 1) |
||
87 | |||
88 | /** |
||
89 | * Add new line to code. |
||
90 | * |
||
91 | * @param string $text Code to add to new line |
||
92 | * @param integer $tabs Tabs count |
||
93 | * @return self |
||
94 | */ |
||
95 | public function newLine($text = '', $tabs = null) |
||
104 | |||
105 | /** |
||
106 | * Add single line comment to code |
||
107 | * @param string $text Comment text |
||
108 | * @return self Chaining |
||
109 | */ |
||
110 | public function comment($text = '') |
||
114 | |||
115 | /** |
||
116 | * Add multi-line comment. If array with one line is passed |
||
117 | * we create special syntax comment in one line, usually |
||
118 | * used for class variable definition in more compact form. |
||
119 | * |
||
120 | * @param array $lines Array of comments lines |
||
121 | * @return self Chaining |
||
122 | */ |
||
123 | public function multiComment(array $lines = array()) |
||
146 | |||
147 | /** |
||
148 | * Add one line variable definition comment. |
||
149 | * |
||
150 | * @param string $type Variable typeHint |
||
151 | * @param string $description Variable description |
||
152 | * @param string $name Variable name |
||
153 | * @return self Chaining |
||
154 | */ |
||
155 | public function commentVar($type, $description, $name = '') |
||
161 | |||
162 | /** |
||
163 | * Add string value definition. |
||
164 | * |
||
165 | * @param string $value String value to add |
||
166 | * @param string $tabs Tabs count |
||
167 | * @param string $quote Type of quote |
||
168 | * @return self Chaining |
||
169 | */ |
||
170 | public function stringValue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
174 | |||
175 | /** |
||
176 | * Add array values definition. |
||
177 | * |
||
178 | * @param array $items Array key-value pairs collection |
||
179 | * @return self Chaining |
||
180 | */ |
||
181 | public function arrayValue(array $items = array()) |
||
208 | |||
209 | /** |
||
210 | * Add variable definition with array merging. |
||
211 | * |
||
212 | * @param string $name Variable name |
||
213 | * @param array $value Array of key-value items for merging it to other array |
||
214 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
215 | * @return self Chaining |
||
216 | */ |
||
217 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
226 | |||
227 | /** |
||
228 | * Add variable definition. |
||
229 | * |
||
230 | * @param string $name Variable name |
||
231 | * @param mixed $value Variable default value |
||
232 | * @param string $after String to insert after variable definition |
||
233 | * @param string $end Closing part of variable definition |
||
234 | * @param string $quote Type of quote |
||
235 | * @return Generator Chaining |
||
236 | */ |
||
237 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
268 | |||
269 | /** |
||
270 | * Add trait definition. |
||
271 | * |
||
272 | * @param string $name Trait name |
||
273 | * @return self Chaining |
||
274 | */ |
||
275 | public function defTrait($name) |
||
295 | |||
296 | /** |
||
297 | * Add class definition. |
||
298 | * |
||
299 | * @param string $name Class name |
||
300 | * @param string $extends Parent class name |
||
301 | * @param array $implements Interfaces names collection |
||
302 | * @return self Chaining |
||
303 | */ |
||
304 | public function defClass($name, $extends = null, array $implements = array()) |
||
334 | |||
335 | /** |
||
336 | * Close current class context. |
||
337 | * |
||
338 | * @return self Chaining |
||
339 | */ |
||
340 | public function endClass() |
||
349 | |||
350 | /** |
||
351 | * Define if statement condition. |
||
352 | * |
||
353 | * @param string $condition Condition statement |
||
354 | * @return self Chaining |
||
355 | */ |
||
356 | public function defIfCondition($condition) |
||
365 | |||
366 | /** |
||
367 | * Define elseif statement condition. |
||
368 | * |
||
369 | * @param string $condition Condition statement |
||
370 | * @return self Chaining |
||
371 | */ |
||
372 | public function defElseIfCondition($condition) |
||
380 | |||
381 | /** |
||
382 | * Define else statement. |
||
383 | * |
||
384 | * @return self Chaining |
||
385 | */ |
||
386 | public function defElseCondition() |
||
394 | |||
395 | /** |
||
396 | * Close if condition statement. |
||
397 | * |
||
398 | * @return self Chaining |
||
399 | */ |
||
400 | public function endIfCondition() |
||
411 | |||
412 | /** |
||
413 | * Add class variable definition. |
||
414 | * |
||
415 | * @param string $name Variable name |
||
416 | * @param string $visibility Variable accessibility level |
||
417 | * @param mixed $value Variable default value |
||
418 | * @return self Chaining |
||
419 | */ |
||
420 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
428 | |||
429 | /** |
||
430 | * Add class constant definition. |
||
431 | * |
||
432 | * @param string $name Constant name |
||
433 | * @param string $value Variable default value |
||
434 | * @return self Chaining |
||
435 | */ |
||
436 | public function defClassConst($name, $value) |
||
440 | |||
441 | /** |
||
442 | * Write file to disk |
||
443 | * @param string $name Path to file |
||
444 | * @param string $format Output file format |
||
445 | */ |
||
446 | public function write($name, $format = 'php') |
||
456 | |||
457 | /** |
||
458 | * Flush internal data and return it. |
||
459 | * |
||
460 | * @return string Current generated code |
||
461 | */ |
||
462 | public function flush() |
||
473 | |||
474 | /** |
||
475 | * Add class function definition. |
||
476 | * |
||
477 | * @param string $name Class function name |
||
478 | * @param string $visibility Class function visibility |
||
479 | * @param array $parameters Class function arguments |
||
480 | * @param array $comments Class function multi-line comments |
||
481 | * @param null $returnType Class function return type PHP7 |
||
482 | * |
||
483 | * @return $this |
||
484 | * |
||
485 | */ |
||
486 | public function defClassFunction(string $name, string $visibility = 'public', array $parameters = [], array $comments = [], $returnType = null) |
||
496 | |||
497 | /** |
||
498 | * @see self::defClassFunction with public visibility |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function defPublicClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
506 | |||
507 | /** |
||
508 | * @see self::defClassFunction with private visibility |
||
509 | * |
||
510 | * @return $this |
||
511 | */ |
||
512 | public function defPrivateClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
516 | |||
517 | /** |
||
518 | * @see self::defClassFunction with protected visibility |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function defProtectedClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
526 | |||
527 | |||
528 | /** |
||
529 | * Close class function definition. |
||
530 | * |
||
531 | * @return $this Chaining |
||
532 | */ |
||
533 | public function endClassFunction() |
||
539 | |||
540 | /** |
||
541 | * Add function definition. |
||
542 | * |
||
543 | * @param string $name Function name |
||
544 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
545 | * @param string $prefix Function prefix |
||
546 | * @param array $comments Function multi-line comments |
||
547 | * @param string $returnType Function return type PHP7 |
||
548 | * |
||
549 | * @return Generator Chaining |
||
550 | */ |
||
551 | public function defFunction(string $name, array $parameters = [], string $prefix = '', array $comments = [], string $returnType = null) |
||
571 | |||
572 | /** |
||
573 | * Close current function context. |
||
574 | * |
||
575 | * @return self Chaining |
||
576 | */ |
||
577 | public function endFunction() |
||
583 | |||
584 | /** |
||
585 | * Constructor |
||
586 | * @param string $namespace Code namespace |
||
587 | */ |
||
588 | public function __construct($namespace = null) |
||
595 | |||
596 | /** |
||
597 | * Add namespace declaration. |
||
598 | * |
||
599 | * @param string $name Namespace name |
||
600 | * |
||
601 | * @return $this Chaining |
||
602 | */ |
||
603 | public function defNamespace($name) |
||
611 | } |
||
612 | //[PHPCOMPRESSOR(remove,end)] |
||
613 |