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 | /** @var string Generated code */ |
||
| 14 | public $code = ''; |
||
| 15 | |||
| 16 | /** @var integer Level of code line tabbing for new lines */ |
||
| 17 | public $tabs = 0; |
||
| 18 | |||
| 19 | /** @var string Current class name */ |
||
| 20 | public $class; |
||
| 21 | |||
| 22 | /** @var int Current conditions nesting level */ |
||
| 23 | public $ifConditionLevel = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Add simple text to current code position |
||
| 27 | * @param string $text Text to add |
||
| 28 | * @return self |
||
| 29 | */ |
||
| 30 | public function text($text = '') |
||
| 31 | { |
||
| 32 | $this->code .= $text; |
||
| 33 | |||
| 34 | return $this; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Add current tabbing level to current line. |
||
| 39 | * |
||
| 40 | * @param string $endText Text to add after tabs |
||
| 41 | * @param integer $tabs Amount of tabs to add |
||
| 42 | * @param string $startText Text to add before tabs |
||
| 43 | * @return Generator Chaining |
||
| 44 | */ |
||
| 45 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
| 46 | { |
||
| 47 | // Generate tabs array |
||
| 48 | $tabs = isset($tabs) && $tabs ? array_fill(0, $tabs, "\t") : array(); |
||
| 49 | |||
| 50 | // Add necessary amount of tabs to line and append text |
||
| 51 | $this->text($startText.implode('', $tabs) . $endText); |
||
| 52 | |||
| 53 | return $this; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Add new line to code. |
||
| 58 | * |
||
| 59 | * @param string $text Code to add to new line |
||
| 60 | * @param integer $tabs Tabs count |
||
| 61 | * @return self |
||
| 62 | */ |
||
| 63 | public function newLine($text = '', $tabs = null) |
||
| 64 | { |
||
| 65 | // If no tabs count is specified set default tabs |
||
| 66 | if (!isset($tabs)) { |
||
| 67 | $tabs = $this->tabs; |
||
| 68 | } |
||
| 69 | |||
| 70 | return $this->tabs($text, $tabs, "\n"); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Add single line comment to code |
||
| 75 | * @param string $text Comment text |
||
| 76 | * @return self Chaining |
||
| 77 | */ |
||
| 78 | public function comment($text = '') |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Add multi-line comment. If array with one line is passed |
||
| 85 | * we create special syntax comment in one line, usually |
||
| 86 | * used for class variable definition in more compact form. |
||
| 87 | * |
||
| 88 | * @param array $lines Array of comments lines |
||
| 89 | * @return self Chaining |
||
| 90 | */ |
||
| 91 | public function multiComment(array $lines = array()) |
||
| 92 | { |
||
| 93 | // If array is not empty |
||
| 94 | if (sizeof($lines)) { |
||
| 95 | $this->newLine("/**"); |
||
| 96 | |||
| 97 | // Multi-comment with single line |
||
| 98 | if (sizeof($lines) === 1) { |
||
| 99 | $this->text(' '.$lines[0].' */'); |
||
| 100 | } else { // Iterate comments lines and if comment line is not empty |
||
| 101 | foreach ($lines as $line) { |
||
| 102 | if (isset($line{0})) { |
||
| 103 | $this->newLine(" * " . $line); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $this->newLine(" */"); |
||
| 108 | } |
||
| 109 | |||
|
|
|||
| 110 | } |
||
| 111 | |||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Add one line variable definition comment. |
||
| 117 | * |
||
| 118 | * @param string $type Variable type |
||
| 119 | * @param string $description Variable description |
||
| 120 | * @param string $name Variable name |
||
| 121 | * @return self Chaining |
||
| 122 | */ |
||
| 123 | public function commentVar($type, $description, $name = '') |
||
| 124 | { |
||
| 125 | return $this->multiComment(array( |
||
| 126 | '@var ' . trim($type) . (isset($name) ? trim($name) . ' ' : ' ') . trim($description) |
||
| 127 | )); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add string value definition. |
||
| 132 | * |
||
| 133 | * @param string $value String value to add |
||
| 134 | * @param string $tabs Tabs count |
||
| 135 | * @param string $quote Type of quote |
||
| 136 | * @return self Chaining |
||
| 137 | */ |
||
| 138 | public function stringValue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Add array values definition. |
||
| 145 | * |
||
| 146 | * @param array $items Array key-value pairs collection |
||
| 147 | * @return self Chaining |
||
| 148 | */ |
||
| 149 | public function arrayValue(array $items = array()) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Add variable definition with array merging. |
||
| 179 | * |
||
| 180 | * @param string $name Variable name |
||
| 181 | * @param array $value Array of key-value items for merging it to other array |
||
| 182 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
| 183 | * @return self Chaining |
||
| 184 | */ |
||
| 185 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Add variable definition. |
||
| 197 | * |
||
| 198 | * @param string $name Variable name |
||
| 199 | * @param mixed $value Variable default value |
||
| 200 | * @param string $after String to insert after variable definition |
||
| 201 | * @param string $end Closing part of variable definition |
||
| 202 | * @param string $quote Type of quote |
||
| 203 | * @return Generator Chaining |
||
| 204 | */ |
||
| 205 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add trait definition. |
||
| 235 | * |
||
| 236 | * @param string $name Trait name |
||
| 237 | * @return self Chaining |
||
| 238 | */ |
||
| 239 | public function defTrait($name) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Add class definition. |
||
| 262 | * |
||
| 263 | * @param string $name Class name |
||
| 264 | * @param string $extends Parent class name |
||
| 265 | * @param array $implements Interfaces names collection |
||
| 266 | * @return self Chaining |
||
| 267 | */ |
||
| 268 | public function defClass($name, $extends = null, array $implements = array()) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Close current class context. |
||
| 301 | * |
||
| 302 | * @return self Chaining |
||
| 303 | */ |
||
| 304 | public function endClass() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Define if statement condition. |
||
| 316 | * |
||
| 317 | * @param string $condition Condition statement |
||
| 318 | * @return self Chaining |
||
| 319 | */ |
||
| 320 | public function defIfCondition($condition) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Define elseif statement condition. |
||
| 332 | * |
||
| 333 | * @param string $condition Condition statement |
||
| 334 | * @return self Chaining |
||
| 335 | */ |
||
| 336 | public function defElseIfCondition($condition) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Define else statement. |
||
| 347 | * |
||
| 348 | * @return self Chaining |
||
| 349 | */ |
||
| 350 | public function defElseCondition() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Close if condition statement. |
||
| 361 | * |
||
| 362 | * @return self Chaining |
||
| 363 | */ |
||
| 364 | public function endIfCondition() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Add class variable definition. |
||
| 378 | * |
||
| 379 | * @param string $name Variable name |
||
| 380 | * @param string $visibility Variable accessibility level |
||
| 381 | * @param string $value Variable default value |
||
| 382 | * @return self Chaining |
||
| 383 | */ |
||
| 384 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Add class constant definition. |
||
| 395 | * |
||
| 396 | * @param string $name Constant name |
||
| 397 | * @param string $value Variable default value |
||
| 398 | * @return self Chaining |
||
| 399 | */ |
||
| 400 | public function defClassConst($name, $value) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Write file to disk |
||
| 407 | * @param string $name Path to file |
||
| 408 | * @param string $format Output file format |
||
| 409 | */ |
||
| 410 | public function write($name, $format = 'php') |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Flush internal data and return it. |
||
| 423 | * |
||
| 424 | * @return string Current generated code |
||
| 425 | */ |
||
| 426 | public function flush() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Add function definition. |
||
| 440 | * |
||
| 441 | * @param string $name Function name |
||
| 442 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
| 443 | * @return Generator Chaining |
||
| 444 | */ |
||
| 445 | public function defFunction($name, $parameters = array()) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Close current function context. |
||
| 465 | * |
||
| 466 | * @return self Chaining |
||
| 467 | */ |
||
| 468 | public function endFunction() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Constructor |
||
| 477 | * @param string $namespace Code namespace |
||
| 478 | */ |
||
| 479 | public function __construct($namespace = null) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Add namespace declaration |
||
| 489 | * @param string $name Namespace name |
||
| 490 | * @return self |
||
| 491 | */ |
||
| 492 | private function defnamespace($name) |
||
| 496 | } |
||
| 497 | //[PHPCOMPRESSOR(remove,end)] |
||
| 498 |