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 |
||
13 | class Response { |
||
|
|||
14 | use Module, Events; |
||
15 | |||
16 | const TYPE_JSON = 'application/json', |
||
17 | TYPE_HTML = 'text/html', |
||
18 | TYPE_TEXT = 'text/plain', |
||
19 | TYPE_CSS = 'text/css', |
||
20 | TYPE_XML = 'text/xml', |
||
21 | TYPE_SVG = 'image/svg+xml', |
||
22 | TYPE_JS = 'application/javascript', |
||
23 | TYPE_BIN = 'application/octet-stream'; |
||
24 | |||
25 | protected static $payload = [], |
||
26 | $status = 200, |
||
27 | $charset = "utf-8", |
||
28 | $headers = ['Content-Type' => ['text/html; charset=utf-8']], |
||
29 | $buffer = null, |
||
30 | $force_dl = false, |
||
31 | $link = null, |
||
32 | $sent = false, |
||
33 | $links = []; |
||
34 | |||
35 | |||
36 | public static function charset($charset){ |
||
39 | |||
40 | public static function type($mime){ |
||
43 | |||
44 | /** |
||
45 | * Force download of Response body |
||
46 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
||
47 | * @return [type] [description] |
||
48 | */ |
||
49 | public static function download($filename){ |
||
52 | |||
53 | /** |
||
54 | * Start capturing output |
||
55 | */ |
||
56 | public static function start(){ |
||
59 | |||
60 | /** |
||
61 | * Enable CORS HTTP headers. |
||
62 | */ |
||
63 | public static function enableCORS($origin='*'){ |
||
92 | |||
93 | public static function sent() { |
||
96 | |||
97 | /** |
||
98 | * Finish the output buffer capturing. |
||
99 | * @return string The captured buffer |
||
100 | */ |
||
101 | public static function end(){ |
||
109 | |||
110 | /** |
||
111 | * Check if an response output buffering is active. |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public static function isBuffering(){ |
||
117 | |||
118 | /** |
||
119 | * Clear the response body |
||
120 | */ |
||
121 | public static function clean(){ |
||
125 | |||
126 | /** |
||
127 | * Append a JSON object to the buffer. |
||
128 | * @param mixed $payload Data to append to the response buffer |
||
129 | */ |
||
130 | public static function json($payload){ |
||
134 | |||
135 | /** |
||
136 | * Append a text to the buffer. |
||
137 | * @param mixed $payload Text to append to the response buffer |
||
138 | */ |
||
139 | public static function text(...$args){ |
||
143 | |||
144 | /** |
||
145 | * Append an XML string to the buffer. |
||
146 | * @param mixed $payload Data to append to the response buffer |
||
147 | */ |
||
148 | public static function xml(...$args){ |
||
152 | |||
153 | /** |
||
154 | * Append a SVG string to the buffer. |
||
155 | * @param mixed $payload Data to append to the response buffer |
||
156 | */ |
||
157 | public static function svg(...$args){ |
||
161 | |||
162 | /** |
||
163 | * Append an HTML string to the buffer. |
||
164 | * @param mixed $payload Data to append to the response buffer |
||
165 | */ |
||
166 | public static function html(...$args){ |
||
170 | |||
171 | /** |
||
172 | * Append data to the buffer. |
||
173 | * Rules : |
||
174 | * - Callables will be called and their results added (recursive) |
||
175 | * - Views will be rendered |
||
176 | * - Objects, arrays and bools will be JSON encoded |
||
177 | * - Strings and numbers will be appendend to the response |
||
178 | * |
||
179 | * @param mixed $payload Data to append to the response buffer |
||
180 | */ |
||
181 | public static function add(){ |
||
195 | |||
196 | public static function status($code,$message=''){ |
||
199 | |||
200 | public static function header($name,$value,$code=null){ |
||
207 | |||
208 | public static function error($code=500,$message='Application Error'){ |
||
213 | |||
214 | public static function body($setBody=null){ |
||
220 | |||
221 | public static function headers($setHeaders=null){ |
||
225 | |||
226 | /** |
||
227 | * Save response as an object, for serialization or cache storage |
||
228 | * |
||
229 | * @method save |
||
230 | * |
||
231 | * @return array Headers and body of the response |
||
232 | */ |
||
233 | public static function save(){ |
||
239 | |||
240 | /** |
||
241 | * Load response from a saved state |
||
242 | * |
||
243 | * @method load |
||
244 | * |
||
245 | * @param array $data head/body saved state |
||
246 | */ |
||
247 | public static function load($data){ |
||
252 | |||
253 | public static function send($force = false){ |
||
254 | if (!static::$sent || $force) { |
||
255 | static::$sent = true; |
||
256 | static::trigger('send'); |
||
257 | Event::trigger('core.response.send'); |
||
258 | if (false === headers_sent()) foreach (static::$headers as $name => $family) |
||
259 | foreach ($family as $value_code) { |
||
260 | |||
261 | if (is_array($value_code)) { |
||
262 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
263 | } else { |
||
264 | $value = $value_code; |
||
265 | $code = null; |
||
266 | } |
||
267 | |||
268 | switch($value){ |
||
269 | case "Status": |
||
270 | if (function_exists('http_response_code')){ |
||
271 | http_response_code($code); |
||
272 | } else { |
||
273 | header("Status: $code", true, $code); |
||
274 | } |
||
275 | break; |
||
276 | case "Link": |
||
277 | header("Link: $value", false); |
||
278 | break; |
||
279 | default: |
||
280 | if ($code) { |
||
281 | header("$name: $value", true, $code); |
||
282 | } else { |
||
283 | header("$name: $value", true); |
||
284 | } |
||
285 | break; |
||
286 | } |
||
287 | } |
||
288 | if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
289 | echo static::body(); |
||
290 | static::trigger('sent'); |
||
291 | } |
||
292 | } |
||
293 | |||
294 | |||
295 | /** |
||
296 | * Push resources to client (HTTP/2 spec) |
||
297 | * @param string/array $links The link(s) to the resources to push. |
||
298 | * @return Response The Route object |
||
299 | */ |
||
300 | public static function push($links, $type='text'){ |
||
319 | |||
320 | } |
||
321 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.