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 | 1544 | public function __construct($debug = false, $indentationStep = 4) |
|
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 200 | public function isDebug() |
|
48 | |||
49 | /** |
||
50 | * @param bool $debug |
||
51 | */ |
||
52 | 1544 | public function setDebug($debug) |
|
56 | |||
57 | /** |
||
58 | * @return int |
||
59 | */ |
||
60 | 8 | public function getIndentationStep() |
|
64 | |||
65 | /** |
||
66 | * @param int $indentationStep |
||
67 | */ |
||
68 | 1544 | public function setIndentationStep($indentationStep) |
|
72 | |||
73 | /** |
||
74 | * @param string|null $name |
||
75 | * @param string|false|null $namespace |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 316 | 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 | 100 | 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 | 172 | public function renderObject( |
|
127 | |||
128 | /** |
||
129 | * @param string $object |
||
130 | * @param string|null $property |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 476 | public function renderProperty($object, $property = null) |
|
142 | |||
143 | /** |
||
144 | * @param string $method |
||
145 | * @param string[] $arguments |
||
146 | * @param bool $semicolon |
||
147 | * @param bool $newLine |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 88 | public function renderObjectCall( |
|
165 | |||
166 | /** |
||
167 | * @param string|null $method |
||
168 | * @param string[] $arguments |
||
169 | * @param bool $semicolon |
||
170 | * @param bool $newLine |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 404 | public function renderCall($method, array $arguments = [], $semicolon = false, $newLine = false) |
|
182 | |||
183 | /** |
||
184 | * @param string|null $code |
||
185 | * @param string[] $arguments |
||
186 | * @param string|null $name |
||
187 | * @param bool $semicolon |
||
188 | * @param bool $newLine |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 136 | public function renderClosure( |
|
211 | |||
212 | /** |
||
213 | * @param string $declaration |
||
214 | * @param bool $semicolon |
||
215 | * @param bool $newLine |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 204 | public function renderObjectAssignment( |
|
227 | |||
228 | /** |
||
229 | * @param string $declaration |
||
230 | * @param string|null $propertyPath |
||
231 | * @param bool $semicolon |
||
232 | * @param bool $newLine |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | 48 | public function renderContainerAssignment( |
|
251 | |||
252 | /** |
||
253 | * @param string|null $propertyPath |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 64 | public function renderContainerVariable( |
|
274 | |||
275 | /** |
||
276 | * @param string $variable |
||
277 | * @param string $declaration |
||
278 | * @param bool $semicolon |
||
279 | * @param bool $newLine |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 328 | public function renderAssignment($variable, $declaration, $semicolon = false, $newLine = false) |
|
289 | |||
290 | /** |
||
291 | * @param string $statement |
||
292 | * @param string $code |
||
293 | * @param string|null $condition |
||
294 | * @param string|null $next |
||
295 | * @param bool $newLine |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 56 | public function renderStatement($statement, $code, $condition = null, $next = null, $newLine = true) |
|
318 | |||
319 | /** |
||
320 | * @param string $code |
||
321 | * @param bool $semicolon |
||
322 | * @param bool $newLine |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 664 | public function renderCode($code, $semicolon = true, $newLine = true) |
|
334 | |||
335 | /** |
||
336 | * @param string|null $code |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 320 | public function renderIndentation($code = null) |
|
349 | |||
350 | /** |
||
351 | * @param string[] $codes |
||
352 | * @param bool $newLine |
||
353 | * @param bool $eolLine |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 344 | public function renderLines(array $codes, $newLine = true, $eolLine = true) |
|
368 | |||
369 | /** |
||
370 | * @param string|null $code |
||
371 | * @param bool $newLine |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | 864 | public function renderLine($code = null, $newLine = true) |
|
383 | |||
384 | /** |
||
385 | * @param string $argument |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | 280 | public function renderEscape($argument) |
|
393 | |||
394 | /** |
||
395 | * @return string |
||
396 | */ |
||
397 | 712 | public function renderSeparator() |
|
401 | |||
402 | /** |
||
403 | * @param string[] $arguments |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | 476 | private function renderArguments(array $arguments) |
|
411 | } |
||
412 |