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 | public function get<methodName>(): string | ||
| 32 | { | ||
| 33 | <spaces>return $this-><fieldName>; | ||
| 34 | }'; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * @var string | ||
| 38 | */ | ||
| 39 | private $isMethodTemplate = | ||
| 40 | ' | ||
| 41 | public function is<methodName>(): bool | ||
| 42 | { | ||
| 43 | <spaces>return $this-><fieldName>; | ||
| 44 | }'; | ||
| 45 | |||
| 46 | /** | ||
| 47 | * @var string | ||
| 48 | */ | ||
| 49 | private $setMethodTemplate = | ||
| 50 | ' | ||
| 51 | public function set<methodName>($<fieldName>): $this | ||
| 52 | { | ||
| 53 | <spaces>$this-><fieldName> = $<fieldName>; | ||
| 54 | |||
| 55 | <spaces>return $this; | ||
| 56 | }'; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * @var string | ||
| 60 | */ | ||
| 61 | private $constructorTemplate = | ||
| 62 | ' | ||
| 63 | public function __construct() | ||
| 64 | { | ||
| 65 | <fields> | ||
| 66 | }'; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @param array $metadata | ||
| 70 | * | ||
| 71 | * @return string | ||
| 72 | */ | ||
| 73 | public function generateDocumentClass(array $metadata) | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Generates document body | ||
| 92 | * | ||
| 93 | * @param array $metadata | ||
| 94 | * | ||
| 95 | * @return string | ||
| 96 | */ | ||
| 97 | private function generateDocumentBody(array $metadata) | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Generates document properties | ||
| 118 | * | ||
| 119 | * @param array $metadata | ||
| 120 | * | ||
| 121 | * @return string | ||
| 122 | */ | ||
| 123 | private function generateDocumentProperties(array $metadata) | ||
| 134 | |||
| 135 | /** | ||
| 136 | * Generates document methods | ||
| 137 | * | ||
| 138 | * @param array $metadata | ||
| 139 | * | ||
| 140 | * @return string | ||
| 141 | */ | ||
| 142 | private function generateDocumentMethods(array $metadata) | ||
| 160 | |||
| 161 | /** | ||
| 162 | * Generates document constructor | ||
| 163 | * | ||
| 164 | * @param array $metadata | ||
| 165 | * | ||
| 166 | * @return string | ||
| 167 | */ | ||
| 168 | private function generateDocumentConstructor(array $metadata) | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Generates document method | ||
| 185 | * | ||
| 186 | * @param array $metadata | ||
| 187 | * @param string $template | ||
| 188 | * | ||
| 189 | * @return string | ||
| 190 | */ | ||
| 191 | private function generateDocumentMethod(array $metadata, $template) | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Returns property doc block | ||
| 204 | * | ||
| 205 | * @param array $metadata | ||
| 206 | * | ||
| 207 | * @return string | ||
| 208 | */ | ||
| 209 | private function generatePropertyDocBlock(array $metadata) | ||
| 210 |     { | ||
| 211 | $lines = [ | ||
| 212 | $this->spaces . '/**', | ||
| 213 | $this->spaces . ' * @var string', | ||
| 214 | $this->spaces . ' *', | ||
| 215 | ]; | ||
| 216 | |||
| 217 | $column = []; | ||
| 218 |         if (isset($metadata['property_name']) && $metadata['property_name'] != $metadata['field_name']) { | ||
| 219 | $column[] = 'name="' . $metadata['property_name'] . '"'; | ||
| 220 | } | ||
| 221 | |||
| 222 |         if (isset($metadata['property_class'])) { | ||
| 223 | $column[] = 'class="' . $metadata['property_class'] . '"'; | ||
| 224 | } | ||
| 225 | |||
| 226 |         if (isset($metadata['property_multiple']) && $metadata['property_multiple']) { | ||
| 227 | $column[] = 'multiple=true'; | ||
| 228 | } | ||
| 229 | |||
| 230 |         if (isset($metadata['property_type']) && $metadata['annotation'] == 'property') { | ||
| 231 | $column[] = 'type="' . $metadata['property_type'] . '"'; | ||
| 232 | } | ||
| 233 | |||
| 234 |         if (isset($metadata['property_default'])) { | ||
| 235 | $column[] = 'default="' . $metadata['property_default'] . '"'; | ||
| 236 | } | ||
| 237 | |||
| 238 |         if (isset($metadata['property_options'])  && $metadata['property_options']) { | ||
| 239 |             $column[] = 'options={' . $metadata['property_options'] . '}'; | ||
| 240 | } | ||
| 241 | |||
| 242 | $lines[] = $this->spaces . ' * @ES\\' . Inflector::classify($metadata['annotation']) | ||
| 243 |             . '(' . implode(', ', $column) . ')'; | ||
| 244 | |||
| 245 | $lines[] = $this->spaces . ' */'; | ||
| 246 | |||
| 247 |         return implode("\n", $lines); | ||
| 248 | } | ||
| 249 | |||
| 250 | /** | ||
| 251 | * Generates document doc block | ||
| 252 | * | ||
| 253 | * @param array $metadata | ||
| 254 | * | ||
| 255 | * @return string | ||
| 256 | */ | ||
| 257 | private function generateDocumentDocBlock(array $metadata) | ||
| 258 |     { | ||
| 259 | return str_replace( | ||
| 260 | ['<className>', '<annotation>', '<options>'], | ||
| 261 | [ | ||
| 262 | $this->getClassName($metadata), | ||
| 263 | Inflector::classify($metadata['annotation']), | ||
| 264 | $this->getAnnotationOptions($metadata), | ||
| 265 | ], | ||
| 266 | '/** | ||
| 267 | * <className> | ||
| 268 | * | ||
| 269 | * @ES\<annotation>(<options>) | ||
| 270 | */' | ||
| 271 | ); | ||
| 272 | } | ||
| 273 | |||
| 274 | /** | ||
| 275 | * @param string $code | ||
| 276 | * | ||
| 277 | * @return string | ||
| 278 | */ | ||
| 279 | private function prefixWithSpaces($code) | ||
| 291 | |||
| 292 | /** | ||
| 293 | * Returns class name | ||
| 294 | * | ||
| 295 | * @param array $metadata | ||
| 296 | * | ||
| 297 | * @return string | ||
| 298 | */ | ||
| 299 | private function getClassName(array $metadata) | ||
| 304 | |||
| 305 | /** | ||
| 306 | * Returns annotation options | ||
| 307 | * | ||
| 308 | * @param array $metadata | ||
| 309 | * | ||
| 310 | * @return string | ||
| 311 | */ | ||
| 312 | private function getAnnotationOptions(array $metadata) | ||
| 324 | |||
| 325 | /** | ||
| 326 | * Generates document use statements | ||
| 327 | * | ||
| 328 | * @param array $metadata | ||
| 329 | * | ||
| 330 | * @return string | ||
| 331 | */ | ||
| 332 | private function generateDocumentUse(array $metadata) | ||
| 342 | |||
| 343 | /** | ||
| 344 | * @param array $metadata | ||
| 345 | * | ||
| 346 | * @return bool | ||
| 347 | */ | ||
| 348 | private function hasMultipleEmbedded(array $metadata) | ||
| 358 | } | ||
| 359 |