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 | |||
| 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 | * Add new line to code. |
||
| 62 | * |
||
| 63 | * @param string $text Code to add to new line |
||
| 64 | * @param integer $tabs Tabs count |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | public function newLine($text = '', $tabs = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Add single line comment to code |
||
| 79 | * @param string $text Comment text |
||
| 80 | * @return self Chaining |
||
| 81 | */ |
||
| 82 | public function comment($text = '') |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Add multi-line comment. If array with one line is passed |
||
| 89 | * we create special syntax comment in one line, usually |
||
| 90 | * used for class variable definition in more compact form. |
||
| 91 | * |
||
| 92 | * @param array $lines Array of comments lines |
||
| 93 | * @return self Chaining |
||
| 94 | */ |
||
| 95 | public function multiComment(array $lines = array()) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add one line variable definition comment. |
||
| 121 | * |
||
| 122 | * @param string $type Variable type |
||
| 123 | * @param string $description Variable description |
||
| 124 | * @param string $name Variable name |
||
| 125 | * @return self Chaining |
||
| 126 | */ |
||
| 127 | public function commentVar($type, $description, $name = '') |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Add string value definition. |
||
| 136 | * |
||
| 137 | * @param string $value String value to add |
||
| 138 | * @param string $tabs Tabs count |
||
| 139 | * @param string $quote Type of quote |
||
| 140 | * @return self Chaining |
||
| 141 | */ |
||
| 142 | public function stringValue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Add array values definition. |
||
| 149 | * |
||
| 150 | * @param array $items Array key-value pairs collection |
||
| 151 | * @return self Chaining |
||
| 152 | */ |
||
| 153 | public function arrayValue(array $items = array()) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Add variable definition with array merging. |
||
| 183 | * |
||
| 184 | * @param string $name Variable name |
||
| 185 | * @param array $value Array of key-value items for merging it to other array |
||
| 186 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
| 187 | * @return self Chaining |
||
| 188 | */ |
||
| 189 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Add variable definition. |
||
| 201 | * |
||
| 202 | * @param string $name Variable name |
||
| 203 | * @param mixed $value Variable default value |
||
| 204 | * @param string $after String to insert after variable definition |
||
| 205 | * @param string $end Closing part of variable definition |
||
| 206 | * @param string $quote Type of quote |
||
| 207 | * @return Generator Chaining |
||
| 208 | */ |
||
| 209 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Add trait definition. |
||
| 243 | * |
||
| 244 | * @param string $name Trait name |
||
| 245 | * @return self Chaining |
||
| 246 | */ |
||
| 247 | public function defTrait($name) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Add class definition. |
||
| 270 | * |
||
| 271 | * @param string $name Class name |
||
| 272 | * @param string $extends Parent class name |
||
| 273 | * @param array $implements Interfaces names collection |
||
| 274 | * @return self Chaining |
||
| 275 | */ |
||
| 276 | public function defClass($name, $extends = null, array $implements = array()) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Close current class context. |
||
| 309 | * |
||
| 310 | * @return self Chaining |
||
| 311 | */ |
||
| 312 | public function endClass() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Define if statement condition. |
||
| 324 | * |
||
| 325 | * @param string $condition Condition statement |
||
| 326 | * @return self Chaining |
||
| 327 | */ |
||
| 328 | public function defIfCondition($condition) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Define elseif statement condition. |
||
| 340 | * |
||
| 341 | * @param string $condition Condition statement |
||
| 342 | * @return self Chaining |
||
| 343 | */ |
||
| 344 | public function defElseIfCondition($condition) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Define else statement. |
||
| 355 | * |
||
| 356 | * @return self Chaining |
||
| 357 | */ |
||
| 358 | public function defElseCondition() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Close if condition statement. |
||
| 369 | * |
||
| 370 | * @return self Chaining |
||
| 371 | */ |
||
| 372 | public function endIfCondition() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Add class variable definition. |
||
| 386 | * |
||
| 387 | * @param string $name Variable name |
||
| 388 | * @param string $visibility Variable accessibility level |
||
| 389 | * @param mixed $value Variable default value |
||
| 390 | * @return self Chaining |
||
| 391 | */ |
||
| 392 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Add class constant definition. |
||
| 403 | * |
||
| 404 | * @param string $name Constant name |
||
| 405 | * @param string $value Variable default value |
||
| 406 | * @return self Chaining |
||
| 407 | */ |
||
| 408 | public function defClassConst($name, $value) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Write file to disk |
||
| 415 | * @param string $name Path to file |
||
| 416 | * @param string $format Output file format |
||
| 417 | */ |
||
| 418 | public function write($name, $format = 'php') |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Flush internal data and return it. |
||
| 431 | * |
||
| 432 | * @return string Current generated code |
||
| 433 | */ |
||
| 434 | public function flush() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Add function definition. |
||
| 448 | * |
||
| 449 | * @param string $name Function name |
||
| 450 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
| 451 | * @return Generator Chaining |
||
| 452 | */ |
||
| 453 | public function defFunction($name, $parameters = array()) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Close current function context. |
||
| 473 | * |
||
| 474 | * @return self Chaining |
||
| 475 | */ |
||
| 476 | public function endFunction() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Constructor |
||
| 485 | * @param string $namespace Code namespace |
||
| 486 | */ |
||
| 487 | public function __construct($namespace = null) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add namespace declaration |
||
| 497 | * @param string $name Namespace name |
||
| 498 | * @return self |
||
| 499 | */ |
||
| 500 | public function defNamespace($name) |
||
| 504 | } |
||
| 505 | //[PHPCOMPRESSOR(remove,end)] |
||
| 506 |