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) |
||
226 | { |
||
227 | $lines = [ |
||
228 | $this->spaces . '/**', |
||
229 | $this->spaces . ' * @var string', |
||
230 | $this->spaces . ' *', |
||
231 | ]; |
||
232 | |||
233 | $column = []; |
||
234 | if (isset($metadata['property_name']) && $metadata['property_name'] != $metadata['field_name']) { |
||
235 | $column[] = 'name="' . $metadata['property_name'] . '"'; |
||
236 | } |
||
237 | |||
238 | if (isset($metadata['property_class'])) { |
||
239 | $column[] = 'class="' . $metadata['property_class'] . '"'; |
||
240 | } |
||
241 | |||
242 | if (isset($metadata['property_multiple']) && $metadata['property_multiple']) { |
||
243 | $column[] = 'multiple=true'; |
||
244 | } |
||
245 | |||
246 | if (isset($metadata['property_type']) && $metadata['annotation'] == 'property') { |
||
247 | $column[] = 'type="' . $metadata['property_type'] . '"'; |
||
248 | } |
||
249 | |||
250 | if (isset($metadata['property_default'])) { |
||
251 | $column[] = 'default="' . $metadata['property_default'] . '"'; |
||
252 | } |
||
253 | |||
254 | if (isset($metadata['property_options']) && $metadata['property_options']) { |
||
255 | $column[] = 'options={' . $metadata['property_options'] . '}'; |
||
256 | } |
||
257 | |||
258 | $lines[] = $this->spaces . ' * @ES\\' . Inflector::classify($metadata['annotation']) |
||
259 | . '(' . implode(', ', $column) . ')'; |
||
260 | |||
261 | $lines[] = $this->spaces . ' */'; |
||
262 | |||
263 | return implode("\n", $lines); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Generates document doc block |
||
268 | * |
||
269 | * @param array $metadata |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | private function generateDocumentDocBlock(array $metadata) |
||
274 | { |
||
275 | return str_replace( |
||
276 | ['<className>', '<annotation>', '<options>'], |
||
277 | [ |
||
278 | $this->getClassName($metadata), |
||
279 | Inflector::classify($metadata['annotation']), |
||
280 | $this->getAnnotationOptions($metadata), |
||
281 | ], |
||
282 | '/** |
||
283 | * <className> |
||
284 | * |
||
285 | * @ES\<annotation>(<options>) |
||
286 | */' |
||
287 | ); |
||
288 | } |
||
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 | * Returns annotation options |
||
323 | * |
||
324 | * @param array $metadata |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | private function getAnnotationOptions(array $metadata) |
||
340 | |||
341 | /** |
||
342 | * Generates document use statements |
||
343 | * |
||
344 | * @param array $metadata |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | private function generateDocumentUse(array $metadata) |
||
358 | |||
359 | /** |
||
360 | * @param array $metadata |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | private function hasMultipleEmbedded(array $metadata) |
||
374 | } |
||
375 |