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 | |||
| 8 | /** Single quote for string value **/ |
||
| 9 | const QUOTE_SINGLE = "'"; |
||
| 10 | |||
| 11 | /** Double quote for string value **/ |
||
| 12 | const QUOTE_DOUBLE = '"'; |
||
| 13 | |||
| 14 | /** @var string Generated code */ |
||
| 15 | public $code = ''; |
||
| 16 | |||
| 17 | /** @var integer Level of code line tabbing for new lines */ |
||
| 18 | public $tabs = 0; |
||
| 19 | |||
| 20 | /** @var string Current class name */ |
||
| 21 | public $class; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Add simple text to current code position |
||
| 25 | * @param string $text Text to add |
||
| 26 | * @return self |
||
| 27 | */ |
||
| 28 | public function text($text = '') |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Add current tabbing level to current line. |
||
| 37 | * |
||
| 38 | * @param string $endText Text to add after tabs |
||
| 39 | * @param integer $tabs Amount of tabs to add |
||
| 40 | * @param string $startText Text to add before tabs |
||
| 41 | * @return Generator Chaining |
||
| 42 | */ |
||
| 43 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add new line to code. |
||
| 56 | * |
||
| 57 | * @param string $text Code to add to new line |
||
| 58 | * @param integer $tabs Tabs count |
||
| 59 | * @return self |
||
| 60 | */ |
||
| 61 | public function newline($text = '', $tabs = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add single line comment to code |
||
| 73 | * @param string $text Comment text |
||
| 74 | * @return self Chaining |
||
| 75 | */ |
||
| 76 | public function comment($text = '') |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Add multi-line comment. If array with one line is passed |
||
| 83 | * we create special syntax comment in one line, usually |
||
| 84 | * used for class variable definition in more compact form. |
||
| 85 | * |
||
| 86 | * @param array $lines Array of comments lines |
||
| 87 | * @return self Chaining |
||
| 88 | */ |
||
| 89 | public function multicomment(array $lines = array()) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add string value definition |
||
| 115 | * @param string $value String value to add |
||
| 116 | * @param string $tabs Tabs count |
||
| 117 | * @param string $quote Type of quote |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | public function stringvalue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Add array values definition |
||
| 127 | * @param array $items Array key-value pairs collection |
||
| 128 | * @return self Chaining |
||
| 129 | */ |
||
| 130 | public function arrayvalue(array $items = array()) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add variable definition with array merging. |
||
| 168 | * |
||
| 169 | * @param string $name Variable name |
||
| 170 | * @param array $value Array of key-value items for merging it to other array |
||
| 171 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
| 172 | * @return self Chaining |
||
| 173 | */ |
||
| 174 | public function defarraymerge($name, array $value, $arrayName = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Add variable definition. |
||
| 186 | * |
||
| 187 | * @param string $name Variable name |
||
| 188 | * @param string $value Variable default value |
||
| 189 | * @param string $after String to insert after variable definition |
||
| 190 | * @param string $end Closing part of variable definition |
||
| 191 | * @param string $quote Type of quote |
||
| 192 | * @return Generator Chaining |
||
| 193 | */ |
||
| 194 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add class definition. |
||
| 226 | * |
||
| 227 | * @param string $name Class name |
||
| 228 | * @param string $extends Parent class name |
||
| 229 | * @param array $implements Interfaces names collection |
||
| 230 | * @return self Chaining |
||
| 231 | */ |
||
| 232 | public function defClass($name, $extends = null, array $implements = array()) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Close current class context. |
||
| 265 | * |
||
| 266 | * @return self Chaining |
||
| 267 | */ |
||
| 268 | public function endclass() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Add class variable definition. |
||
| 282 | * |
||
| 283 | * @param string $name Variable name |
||
| 284 | * @param string $visibility Variable accessibility level |
||
| 285 | * @param string|null $comment Variable description |
||
| 286 | * @param string $value Variable default value |
||
| 287 | * @return self Chaining |
||
| 288 | */ |
||
| 289 | public function defClassVar($name, $visibility = 'public', $comment = null, $value = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add class constant definition. |
||
| 300 | * |
||
| 301 | * @param string $name Constant name |
||
| 302 | * @param string $value Variable default value |
||
| 303 | * @param string|null $comment Variable description |
||
| 304 | * @return self Chaining |
||
| 305 | */ |
||
| 306 | public function defClassConst($name, $value, $comment = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Write file to disk |
||
| 313 | * @param string $name Path to file |
||
| 314 | * @param string $format Output file format |
||
| 315 | */ |
||
| 316 | public function write($name, $format = 'php') |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Flush internal data and return it. |
||
| 327 | * |
||
| 328 | * @return string Current generated code |
||
| 329 | */ |
||
| 330 | public function flush() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Add function definition |
||
| 344 | * @param string $name Function name |
||
| 345 | * @return self Chaining |
||
| 346 | */ |
||
| 347 | public function deffunction($name) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Close current function context |
||
| 356 | * @return\samson\activerecord\Generator |
||
| 357 | */ |
||
| 358 | public function endfunction() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Constructor |
||
| 365 | * @param string $namespace Code namespace |
||
| 366 | */ |
||
| 367 | public function __construct($namespace = null) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Add namespace declaration |
||
| 375 | * @param string $name Namespace name |
||
| 376 | * @return self |
||
| 377 | */ |
||
| 378 | private function defnamespace($name) |
||
| 382 | |||
| 383 | } |
||
| 384 | //[PHPCOMPRESSOR(remove,end)] |
||
| 385 |
Adding braces to control structures avoids accidental mistakes as your code changes: