Complex classes like Response 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 Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Response |
||
18 | { |
||
19 | /** |
||
20 | * Output format |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $output_format = 'json'; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Possible output formats |
||
29 | */ |
||
30 | const OUTPUT_FORMATS = [ |
||
31 | 'json' => 'application/json; charset=utf-8', |
||
32 | 'xml' => 'application/xml; charset=utf-8', |
||
33 | 'text' => 'text/html; charset=utf-8' |
||
34 | ]; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Human readable output |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $pretty_format = false; |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Headers |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $headers = [ |
||
51 | 'Content-Type' => self::OUTPUT_FORMATS['json'] |
||
52 | ]; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Code |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $code = 200; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Body |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $body; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * body only |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $body_only = false; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Init response |
||
81 | * |
||
82 | * @return void |
||
|
|||
83 | */ |
||
84 | public function __construct() |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Set header |
||
92 | * |
||
93 | * @param string $header |
||
94 | * @param string $value |
||
95 | * @return Response |
||
96 | */ |
||
97 | public function setHeader(string $header, string $value): Response |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Delete header |
||
106 | * |
||
107 | * @param string $header |
||
108 | * @return Response |
||
109 | */ |
||
110 | public function removeHeader(string $header): Response |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Get headers |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getHeaders(): array |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Send headers |
||
133 | * |
||
134 | * @return Response |
||
135 | */ |
||
136 | public function sendHeaders(): Response |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Set response code |
||
148 | * |
||
149 | * @param int $code |
||
150 | * @return Response |
||
151 | */ |
||
152 | public function setCode(int $code): Response |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Get response code |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | public function getCode(): int |
||
172 | |||
173 | |||
174 | /** |
||
175 | * Set body |
||
176 | * |
||
177 | * @param mixed $body |
||
178 | * @param bool $body_only |
||
179 | * @return Response |
||
180 | */ |
||
181 | public function setBody($body, bool $body_only = false): Response |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Get body |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getBody() |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Sends the actual response. |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function send(): void |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Get output format |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getOutputFormat(): string |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Convert response to human readable output |
||
258 | * |
||
259 | * @param bool $format |
||
260 | * @return Response |
||
261 | */ |
||
262 | public function setPrettyFormat(bool $format): Response |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Set header Content-Length $body. |
||
271 | * |
||
272 | * @param string $body |
||
273 | * @return Response |
||
274 | */ |
||
275 | public function setContentLength(string $body): Response |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Converts $body to pretty json. |
||
284 | * |
||
285 | * @param mixed $body |
||
286 | * @return string |
||
287 | */ |
||
288 | public function asJSON($body): string |
||
300 | |||
301 | |||
302 | /** |
||
303 | * Converts mixed data to XML |
||
304 | * |
||
305 | * @param mixed $data |
||
306 | * @param SimpleXMLElement $xml |
||
307 | * @param string $child_name |
||
308 | * @return string |
||
309 | */ |
||
310 | public function toXML($data, Config $xml, string $child_name): string |
||
326 | |||
327 | |||
328 | /** |
||
329 | * Converts response to xml. |
||
330 | * |
||
331 | * @param mixed $body |
||
332 | * @return string |
||
333 | */ |
||
334 | public function asXML($body): string |
||
346 | |||
347 | |||
348 | /** |
||
349 | * Pretty formatted xml |
||
350 | * |
||
351 | * @param string $xml |
||
352 | * @return string |
||
353 | */ |
||
354 | public function prettyXml(string $xml): string |
||
363 | |||
364 | |||
365 | /** |
||
366 | * Set the current output format. |
||
367 | * |
||
368 | * @param string $format |
||
369 | * @return Response |
||
370 | */ |
||
371 | public function setOutputFormat(?string $format=null): Response |
||
386 | |||
387 | |||
388 | /** |
||
389 | * Abort after response |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | public function terminate(): void |
||
397 | |||
398 | |||
399 | /** |
||
400 | * Setup formats. |
||
401 | * |
||
402 | * @return Response |
||
403 | */ |
||
404 | public function setupFormats(): Response |
||
421 | } |
||
422 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.