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 | * Generate correct value. |
||
| 177 | * |
||
| 178 | * Metho handles arrays, numerics, strings and constants. |
||
| 179 | * |
||
| 180 | * @param mixed $value Value to put in generated code |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | protected function defineValue($value) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Add array values definition. |
||
| 205 | * |
||
| 206 | * @param array $items Array key-value pairs collection |
||
| 207 | * @return self Chaining |
||
| 208 | */ |
||
| 209 | public function arrayValue(array $items = array()) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Add variable definition with array merging. |
||
| 232 | * |
||
| 233 | * @param string $name Variable name |
||
| 234 | * @param array $value Array of key-value items for merging it to other array |
||
| 235 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
| 236 | * @return self Chaining |
||
| 237 | */ |
||
| 238 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add variable definition. |
||
| 250 | * |
||
| 251 | * @param string $name Variable name |
||
| 252 | * @param mixed $value Variable default value |
||
| 253 | * @param string $after String to insert after variable definition |
||
| 254 | * @param string $end Closing part of variable definition |
||
| 255 | * @param string $quote Type of quote |
||
| 256 | * @return Generator Chaining |
||
| 257 | */ |
||
| 258 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add trait definition. |
||
| 292 | * |
||
| 293 | * @param string $name Trait name |
||
| 294 | * @return self Chaining |
||
| 295 | */ |
||
| 296 | public function defTrait($name) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Add class definition. |
||
| 319 | * |
||
| 320 | * @param string $name Class name |
||
| 321 | * @param string $extends Parent class name |
||
| 322 | * @param array $implements Interfaces names collection |
||
| 323 | * @return self Chaining |
||
| 324 | */ |
||
| 325 | public function defClass($name, $extends = null, array $implements = array()) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Close current class context. |
||
| 358 | * |
||
| 359 | * @return self Chaining |
||
| 360 | */ |
||
| 361 | public function endClass() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Define if statement condition. |
||
| 373 | * |
||
| 374 | * @param string $condition Condition statement |
||
| 375 | * @return self Chaining |
||
| 376 | */ |
||
| 377 | public function defIfCondition($condition) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Define elseif statement condition. |
||
| 389 | * |
||
| 390 | * @param string $condition Condition statement |
||
| 391 | * @return self Chaining |
||
| 392 | */ |
||
| 393 | public function defElseIfCondition($condition) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Define else statement. |
||
| 404 | * |
||
| 405 | * @return self Chaining |
||
| 406 | */ |
||
| 407 | public function defElseCondition() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Close if condition statement. |
||
| 418 | * |
||
| 419 | * @return self Chaining |
||
| 420 | */ |
||
| 421 | public function endIfCondition() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Add class variable definition. |
||
| 435 | * |
||
| 436 | * @param string $name Variable name |
||
| 437 | * @param string $visibility Variable accessibility level |
||
| 438 | * @param mixed $value Variable default value |
||
| 439 | * @return self Chaining |
||
| 440 | */ |
||
| 441 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Add class constant definition. |
||
| 452 | * |
||
| 453 | * @param string $name Constant name |
||
| 454 | * @param string $value Variable default value |
||
| 455 | * @return self Chaining |
||
| 456 | */ |
||
| 457 | public function defClassConst($name, $value) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Write file to disk |
||
| 464 | * @param string $name Path to file |
||
| 465 | * @param string $format Output file format |
||
| 466 | */ |
||
| 467 | public function write($name, $format = 'php') |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Flush internal data and return it. |
||
| 480 | * |
||
| 481 | * @return string Current generated code |
||
| 482 | */ |
||
| 483 | public function flush() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add class function definition. |
||
| 497 | * |
||
| 498 | * @param string $name Class function name |
||
| 499 | * @param string $visibility Class function visibility |
||
| 500 | * @param array $parameters Class function arguments |
||
| 501 | * @param array $comments Class function multi-line comments |
||
| 502 | * @param null $returnType Class function return type PHP7 |
||
| 503 | * |
||
| 504 | * @return $this |
||
| 505 | * |
||
| 506 | */ |
||
| 507 | public function defClassFunction(string $name, string $visibility = 'public', array $parameters = [], array $comments = [], $returnType = null) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @see self::defClassFunction with public visibility |
||
| 520 | * |
||
| 521 | * @return $this |
||
| 522 | */ |
||
| 523 | public function defPublicClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @see self::defClassFunction with private visibility |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | public function defPrivateClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @see self::defClassFunction with protected visibility |
||
| 540 | * |
||
| 541 | * @return $this |
||
| 542 | */ |
||
| 543 | public function defProtectedClassFunction(string $name, array $parameters = [], array $comments = [], $returnType = null) |
||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * Close class function definition. |
||
| 551 | * |
||
| 552 | * @return $this Chaining |
||
| 553 | */ |
||
| 554 | public function endClassFunction() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Add function definition. |
||
| 563 | * |
||
| 564 | * @param string $name Function name |
||
| 565 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
| 566 | * @param string $prefix Function prefix |
||
| 567 | * @param array $comments Function multi-line comments |
||
| 568 | * @param string $returnType Function return type PHP7 |
||
| 569 | * |
||
| 570 | * @return Generator Chaining |
||
| 571 | */ |
||
| 572 | public function defFunction(string $name, array $parameters = [], string $prefix = '', array $comments = [], string $returnType = null) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Close current function context. |
||
| 595 | * |
||
| 596 | * @return self Chaining |
||
| 597 | */ |
||
| 598 | public function endFunction() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Constructor |
||
| 607 | * @param string $namespace Code namespace |
||
| 608 | */ |
||
| 609 | public function __construct($namespace = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Add namespace declaration. |
||
| 619 | * |
||
| 620 | * @param string $name Namespace name |
||
| 621 | * |
||
| 622 | * @return $this Chaining |
||
| 623 | */ |
||
| 624 | public function defNamespace($name) |
||
| 632 | } |
||
| 633 | //[PHPCOMPRESSOR(remove,end)] |
||
| 634 |