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 |
||
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 | /** @var string Generated code */ |
||
17 | public $code = ''; |
||
18 | |||
19 | /** @var integer Level of code line tabbing for new lines */ |
||
20 | public $tabs = 0; |
||
21 | |||
22 | /** @var string Current class name */ |
||
23 | public $class; |
||
24 | |||
25 | /** @var int Current conditions nesting level */ |
||
26 | public $ifConditionLevel = 0; |
||
27 | |||
28 | /** |
||
29 | * Add simple text to current code position |
||
30 | * @param string $text Text to add |
||
31 | * @return self |
||
32 | */ |
||
33 | public function text($text = '') |
||
39 | |||
40 | /** |
||
41 | * Add current tabbing level to current line. |
||
42 | * |
||
43 | * @param string $endText Text to add after tabs |
||
44 | * @param integer $tabs Amount of tabs to add |
||
45 | * @param string $startText Text to add before tabs |
||
46 | * @return Generator Chaining |
||
47 | */ |
||
48 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
58 | |||
59 | /** |
||
60 | * Add new line to code. |
||
61 | * |
||
62 | * @param string $text Code to add to new line |
||
63 | * @param integer $tabs Tabs count |
||
64 | * @return self |
||
65 | */ |
||
66 | public function newLine($text = '', $tabs = null) |
||
75 | |||
76 | /** |
||
77 | * Add single line comment to code |
||
78 | * @param string $text Comment text |
||
79 | * @return self Chaining |
||
80 | */ |
||
81 | public function comment($text = '') |
||
85 | |||
86 | /** |
||
87 | * Add multi-line comment. If array with one line is passed |
||
88 | * we create special syntax comment in one line, usually |
||
89 | * used for class variable definition in more compact form. |
||
90 | * |
||
91 | * @param array $lines Array of comments lines |
||
92 | * @return self Chaining |
||
93 | */ |
||
94 | public function multiComment(array $lines = array()) |
||
117 | |||
118 | /** |
||
119 | * Add one line variable definition comment. |
||
120 | * |
||
121 | * @param string $type Variable type |
||
122 | * @param string $description Variable description |
||
123 | * @param string $name Variable name |
||
124 | * @return self Chaining |
||
125 | */ |
||
126 | public function commentVar($type, $description, $name = '') |
||
132 | |||
133 | /** |
||
134 | * Add string value definition. |
||
135 | * |
||
136 | * @param string $value String value to add |
||
137 | * @param string $tabs Tabs count |
||
138 | * @param string $quote Type of quote |
||
139 | * @return self Chaining |
||
140 | */ |
||
141 | public function stringValue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
145 | |||
146 | /** |
||
147 | * Add array values definition. |
||
148 | * |
||
149 | * @param array $items Array key-value pairs collection |
||
150 | * @return self Chaining |
||
151 | */ |
||
152 | public function arrayValue(array $items = array()) |
||
179 | |||
180 | /** |
||
181 | * Add variable definition with array merging. |
||
182 | * |
||
183 | * @param string $name Variable name |
||
184 | * @param array $value Array of key-value items for merging it to other array |
||
185 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
186 | * @return self Chaining |
||
187 | */ |
||
188 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
197 | |||
198 | /** |
||
199 | * Add variable definition. |
||
200 | * |
||
201 | * @param string $name Variable name |
||
202 | * @param mixed $value Variable default value |
||
203 | * @param string $after String to insert after variable definition |
||
204 | * @param string $end Closing part of variable definition |
||
205 | * @param string $quote Type of quote |
||
206 | * @return Generator Chaining |
||
207 | */ |
||
208 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
239 | |||
240 | /** |
||
241 | * Add trait definition. |
||
242 | * |
||
243 | * @param string $name Trait name |
||
244 | * @return self Chaining |
||
245 | */ |
||
246 | public function defTrait($name) |
||
263 | |||
264 | /** |
||
265 | * Add class definition. |
||
266 | * |
||
267 | * @param string $name Class name |
||
268 | * @param string $extends Parent class name |
||
269 | * @param array $implements Interfaces names collection |
||
270 | * @return self Chaining |
||
271 | */ |
||
272 | public function defClass($name, $extends = null, array $implements = array()) |
||
302 | |||
303 | /** |
||
304 | * Close current class context. |
||
305 | * |
||
306 | * @return self Chaining |
||
307 | */ |
||
308 | public function endClass() |
||
317 | |||
318 | /** |
||
319 | * Define if statement condition. |
||
320 | * |
||
321 | * @param string $condition Condition statement |
||
322 | * @return self Chaining |
||
323 | */ |
||
324 | public function defIfCondition($condition) |
||
333 | |||
334 | /** |
||
335 | * Define elseif statement condition. |
||
336 | * |
||
337 | * @param string $condition Condition statement |
||
338 | * @return self Chaining |
||
339 | */ |
||
340 | public function defElseIfCondition($condition) |
||
348 | |||
349 | /** |
||
350 | * Define else statement. |
||
351 | * |
||
352 | * @return self Chaining |
||
353 | */ |
||
354 | public function defElseCondition() |
||
362 | |||
363 | /** |
||
364 | * Close if condition statement. |
||
365 | * |
||
366 | * @return self Chaining |
||
367 | */ |
||
368 | public function endIfCondition() |
||
379 | |||
380 | /** |
||
381 | * Add class variable definition. |
||
382 | * |
||
383 | * @param string $name Variable name |
||
384 | * @param string $visibility Variable accessibility level |
||
385 | * @param mixed $value Variable default value |
||
386 | * @return self Chaining |
||
387 | */ |
||
388 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
396 | |||
397 | /** |
||
398 | * Add class constant definition. |
||
399 | * |
||
400 | * @param string $name Constant name |
||
401 | * @param string $value Variable default value |
||
402 | * @return self Chaining |
||
403 | */ |
||
404 | public function defClassConst($name, $value) |
||
408 | |||
409 | /** |
||
410 | * Write file to disk |
||
411 | * @param string $name Path to file |
||
412 | * @param string $format Output file format |
||
413 | */ |
||
414 | public function write($name, $format = 'php') |
||
424 | |||
425 | /** |
||
426 | * Flush internal data and return it. |
||
427 | * |
||
428 | * @return string Current generated code |
||
429 | */ |
||
430 | public function flush() |
||
441 | |||
442 | /** |
||
443 | * Add function definition. |
||
444 | * |
||
445 | * @param string $name Function name |
||
446 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
447 | * @return Generator Chaining |
||
448 | */ |
||
449 | public function defFunction($name, $parameters = array()) |
||
466 | |||
467 | /** |
||
468 | * Close current function context. |
||
469 | * |
||
470 | * @return self Chaining |
||
471 | */ |
||
472 | public function endFunction() |
||
478 | |||
479 | /** |
||
480 | * Constructor |
||
481 | * @param string $namespace Code namespace |
||
482 | */ |
||
483 | public function __construct($namespace = null) |
||
490 | |||
491 | /** |
||
492 | * Add namespace declaration |
||
493 | * @param string $name Namespace name |
||
494 | * @return self |
||
495 | */ |
||
496 | public function defNamespace($name) |
||
500 | } |
||
501 | //[PHPCOMPRESSOR(remove,end)] |
||
502 |