Complex classes like DocumentGenerator 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 DocumentGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 19 | class DocumentGenerator | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * @var string | ||
| 23 | */ | ||
| 24 | private $spaces = ' '; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @var string | ||
| 28 | */ | ||
| 29 | private $getMethodTemplate = | ||
| 30 | '/** | ||
| 31 | * Returns <fieldName> | ||
| 32 | * | ||
| 33 | * @return string | ||
| 34 | */ | ||
| 35 | public function get<methodName>() | ||
| 36 | { | ||
| 37 | <spaces>return $this-><fieldName>; | ||
| 38 | }'; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var string | ||
| 42 | */ | ||
| 43 | private $isMethodTemplate = | ||
| 44 | '/** | ||
| 45 | * Returns <fieldName> | ||
| 46 | * | ||
| 47 | * @return string | ||
| 48 | */ | ||
| 49 | public function is<methodName>() | ||
| 50 | { | ||
| 51 | <spaces>return $this-><fieldName>; | ||
| 52 | }'; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * @var string | ||
| 56 | */ | ||
| 57 | private $setMethodTemplate = | ||
| 58 | '/** | ||
| 59 | * Sets <fieldName> | ||
| 60 | * | ||
| 61 | * @param string $<fieldName> | ||
| 62 | * | ||
| 63 | * @return self | ||
| 64 | */ | ||
| 65 | public function set<methodName>($<fieldName>) | ||
| 66 | { | ||
| 67 | <spaces>$this-><fieldName> = $<fieldName>; | ||
| 68 | |||
| 69 | <spaces>return $this; | ||
| 70 | }'; | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @var string | ||
| 74 | */ | ||
| 75 | private $constructorTemplate = | ||
| 76 | '/** | ||
| 77 | * Constructor | ||
| 78 | */ | ||
| 79 | public function __construct() | ||
| 80 | { | ||
| 81 | <fields> | ||
| 82 | }'; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * @param array $metadata | ||
| 86 | * | ||
| 87 | * @return string | ||
| 88 | */ | ||
| 89 | public function generateDocumentClass(array $metadata) | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Generates document body | ||
| 108 | * | ||
| 109 | * @param array $metadata | ||
| 110 | * | ||
| 111 | * @return string | ||
| 112 | */ | ||
| 113 | private function generateDocumentBody(array $metadata) | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Generates document properties | ||
| 134 | * | ||
| 135 | * @param array $metadata | ||
| 136 | * | ||
| 137 | * @return string | ||
| 138 | */ | ||
| 139 | private function generateDocumentProperties(array $metadata) | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Generates document methods | ||
| 153 | * | ||
| 154 | * @param array $metadata | ||
| 155 | * | ||
| 156 | * @return string | ||
| 157 | */ | ||
| 158 | private function generateDocumentMethods(array $metadata) | ||
| 176 | |||
| 177 | /** | ||
| 178 | * Generates document constructor | ||
| 179 | * | ||
| 180 | * @param array $metadata | ||
| 181 | * | ||
| 182 | * @return string | ||
| 183 | */ | ||
| 184 | private function generateDocumentConstructor(array $metadata) | ||
| 198 | |||
| 199 | /** | ||
| 200 | * Generates document method | ||
| 201 | * | ||
| 202 | * @param array $metadata | ||
| 203 | * @param string $template | ||
| 204 | * | ||
| 205 | * @return string | ||
| 206 | */ | ||
| 207 | private function generateDocumentMethod(array $metadata, $template) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Returns property doc block | ||
| 220 | * | ||
| 221 | * @param array $metadata | ||
| 222 | * | ||
| 223 | * @return string | ||
| 224 | */ | ||
| 225 | private function generatePropertyDocBlock(array $metadata) | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Generates document doc block | ||
| 267 | * | ||
| 268 | * @param array $metadata | ||
| 269 | * | ||
| 270 | * @return string | ||
| 271 | */ | ||
| 272 | private function generateDocumentDocBlock(array $metadata) | ||
| 289 | |||
| 290 | /** | ||
| 291 | * @param string $code | ||
| 292 | * | ||
| 293 | * @return string | ||
| 294 | */ | ||
| 295 | private function prefixWithSpaces($code) | ||
| 307 | |||
| 308 | /** | ||
| 309 | * Returns class name | ||
| 310 | * | ||
| 311 | * @param array $metadata | ||
| 312 | * | ||
| 313 | * @return string | ||
| 314 | */ | ||
| 315 | private function getClassName(array $metadata) | ||
| 320 | |||
| 321 | /** | ||
| 322 | * Generates document use statements | ||
| 323 | * | ||
| 324 | * @param array $metadata | ||
| 325 | * | ||
| 326 | * @return string | ||
| 327 | */ | ||
| 328 | private function generateDocumentUse(array $metadata) | ||
| 338 | |||
| 339 | /** | ||
| 340 | * @param array $metadata | ||
| 341 | * | ||
| 342 | * @return bool | ||
| 343 | */ | ||
| 344 | private function hasMultipleEmbedded(array $metadata) | ||
| 354 | } | ||
| 355 |