Complex classes like Formatter 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 Formatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Formatter |
||
20 | { |
||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $debug; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $indentationStep; |
||
30 | |||
31 | /** |
||
32 | * @param bool $debug |
||
33 | * @param int $indentationStep |
||
34 | */ |
||
35 | 1808 | public function __construct($debug = false, $indentationStep = 4) |
|
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 464 | public function isDebug() |
|
48 | |||
49 | /** |
||
50 | * @param bool $debug |
||
51 | */ |
||
52 | 1808 | public function setDebug($debug) |
|
56 | |||
57 | /** |
||
58 | * @return int |
||
59 | */ |
||
60 | 8 | public function getIndentationStep() |
|
64 | |||
65 | /** |
||
66 | * @param int $indentationStep |
||
67 | */ |
||
68 | 1808 | public function setIndentationStep($indentationStep) |
|
72 | |||
73 | /** |
||
74 | * @param string|null $name |
||
75 | * @param string|false|null $namespace |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 580 | public function renderClass($name = null, $namespace = null) |
|
91 | |||
92 | /** |
||
93 | * @param string $class |
||
94 | * @param string $value |
||
95 | * @param string|false|null $namespace |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 320 | public function renderConstant($class, $value, $namespace = null) |
|
103 | |||
104 | /** |
||
105 | * @param string $class |
||
106 | * @param string[] $arguments |
||
107 | * @param string|false|null $namespace |
||
108 | * @param bool $semicolon |
||
109 | * @param bool $newLine |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 436 | public function renderObject( |
|
127 | |||
128 | /** |
||
129 | * @param string $object |
||
130 | * @param string|null $property |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 740 | public function renderProperty($object, $property = null) |
|
142 | |||
143 | /** |
||
144 | * @param VariableAwareInterface $object |
||
145 | * @param string $method |
||
146 | * @param string[] $arguments |
||
147 | * @param bool $semicolon |
||
148 | * @param bool $newLine |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 308 | public function renderObjectCall( |
|
166 | |||
167 | /** |
||
168 | * @param string|null $method |
||
169 | * @param string[] $arguments |
||
170 | * @param bool $semicolon |
||
171 | * @param bool $newLine |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | 668 | public function renderCall($method, array $arguments = [], $semicolon = false, $newLine = false) |
|
183 | |||
184 | /** |
||
185 | * @param string|null $code |
||
186 | * @param string[] $arguments |
||
187 | * @param string|null $name |
||
188 | * @param bool $semicolon |
||
189 | * @param bool $newLine |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 400 | public function renderClosure( |
|
212 | |||
213 | /** |
||
214 | * @param VariableAwareInterface $object |
||
215 | * @param string $declaration |
||
216 | * @param bool $semicolon |
||
217 | * @param bool $newLine |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 468 | public function renderObjectAssignment( |
|
229 | |||
230 | /** |
||
231 | * @param VariableAwareInterface $root |
||
232 | * @param string $declaration |
||
233 | * @param string|null $propertyPath |
||
234 | * @param VariableAwareInterface|null $object |
||
235 | * @param bool $semicolon |
||
236 | * @param bool $newLine |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 312 | public function renderContainerAssignment( |
|
255 | |||
256 | /** |
||
257 | * @param VariableAwareInterface $root |
||
258 | * @param string|null $propertyPath |
||
259 | * @param VariableAwareInterface|null $object |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | 328 | public function renderContainerVariable( |
|
280 | |||
281 | /** |
||
282 | * @param string $variable |
||
283 | * @param string $declaration |
||
284 | * @param bool $semicolon |
||
285 | * @param bool $newLine |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 592 | public function renderAssignment($variable, $declaration, $semicolon = false, $newLine = false) |
|
295 | |||
296 | /** |
||
297 | * @param string $statement |
||
298 | * @param string $code |
||
299 | * @param string|null $condition |
||
300 | * @param string|null $next |
||
301 | * @param bool $newLine |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 320 | public function renderStatement($statement, $code, $condition = null, $next = null, $newLine = true) |
|
324 | |||
325 | /** |
||
326 | * @param string $code |
||
327 | * @param bool $semicolon |
||
328 | * @param bool $newLine |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 928 | public function renderCode($code, $semicolon = true, $newLine = true) |
|
340 | |||
341 | /** |
||
342 | * @param string|null $code |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | 584 | public function renderIndentation($code = null) |
|
355 | |||
356 | /** |
||
357 | * @param string[] $codes |
||
358 | * @param bool $newLine |
||
359 | * @param bool $eolLine |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 608 | public function renderLines(array $codes, $newLine = true, $eolLine = true) |
|
374 | |||
375 | /** |
||
376 | * @param string|null $code |
||
377 | * @param bool $newLine |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | 1128 | public function renderLine($code = null, $newLine = true) |
|
382 | { |
||
383 | 1128 | if ($newLine && !empty($code) && $this->debug) { |
|
384 | 184 | $code .= "\n"; |
|
385 | 92 | } |
|
386 | |||
387 | 1128 | return (string) $code; |
|
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param string $argument |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | 544 | public function renderEscape($argument) |
|
396 | { |
||
397 | 544 | return json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
398 | } |
||
399 | |||
400 | /** |
||
401 | * @return string |
||
402 | */ |
||
403 | 976 | public function renderSeparator() |
|
407 | |||
408 | /** |
||
409 | * @param string[] $arguments |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | 740 | private function renderArguments(array $arguments) |
|
417 | } |
||
418 |