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 = ['json', 'xml', 'text']; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Human readable output |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $pretty_format = false; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Headers |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $headers = []; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Code |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $code = 200; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Body |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $body; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * body only |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $body_only = false; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Init response |
||
75 | * |
||
76 | * @return void |
||
|
|||
77 | */ |
||
78 | public function __construct() |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Set header |
||
86 | * |
||
87 | * @param string $header |
||
88 | * @param string $value |
||
89 | * @return Response |
||
90 | */ |
||
91 | public function setHeader(string $header, string $value): Response |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Get headers |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getHeaders(): array |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Send headers |
||
111 | * |
||
112 | * @return Response |
||
113 | */ |
||
114 | public function sendHeaders(): Response |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Set response code |
||
126 | * |
||
127 | * @param int $code |
||
128 | * @return Response |
||
129 | */ |
||
130 | public function setCode(int $code): Response |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Get response code |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | public function getCode(): int |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Set body |
||
154 | * |
||
155 | * @param mixed $body |
||
156 | * @param bool $body_only |
||
157 | * @return Response |
||
158 | */ |
||
159 | public function setBody($body, bool $body_only = false): Response |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Get body |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getBody() |
||
176 | |||
177 | |||
178 | /** |
||
179 | * Sends the actual response. |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function send(): void |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Get output format |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getOutputFormat(): string |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Convert response to human readable output |
||
237 | * |
||
238 | * @param bool $format |
||
239 | * @return Response |
||
240 | */ |
||
241 | public function setPrettyFormat(bool $format): Response |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Set header Content-Length $body. |
||
250 | * |
||
251 | * @param string $body |
||
252 | * @return Response |
||
253 | */ |
||
254 | public function setContentLength(string $body): Response |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Converts $body to pretty json. |
||
263 | * |
||
264 | * @param mixed $body |
||
265 | * @return string |
||
266 | */ |
||
267 | public function asJSON($body): string |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Converts mixed data to XML |
||
285 | * |
||
286 | * @param mixed $data |
||
287 | * @param SimpleXMLElement $xml |
||
288 | * @param string $child_name |
||
289 | * @return string |
||
290 | */ |
||
291 | public function toXML($data, Config $xml, string $child_name): string |
||
307 | |||
308 | |||
309 | /** |
||
310 | * Converts response to xml. |
||
311 | * |
||
312 | * @param mixed $body |
||
313 | * @return string |
||
314 | */ |
||
315 | public function asXML($body): string |
||
328 | |||
329 | |||
330 | /** |
||
331 | * Pretty formatted xml |
||
332 | * |
||
333 | * @param string $xml |
||
334 | * @return string |
||
335 | */ |
||
336 | public function prettyXml(string $xml): string |
||
345 | |||
346 | |||
347 | /** |
||
348 | * Set the current output format. |
||
349 | * |
||
350 | * @param string $format a key of $outputForms |
||
351 | * @return Response |
||
352 | */ |
||
353 | public function setOutputFormat(string $format): Response |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Abort after response |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | public function terminate(): void |
||
369 | |||
370 | |||
371 | /** |
||
372 | * Setup formats. |
||
373 | * |
||
374 | * @return Response |
||
375 | */ |
||
376 | public function setupFormats(): Response |
||
393 | } |
||
394 |
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.