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 | |||
34 | |||
35 | public static function charset($charset){ |
||
36 | static::$charset = $charset; |
||
37 | } |
||
38 | |||
39 | public static function type($mime){ |
||
40 | static::header('Content-Type',$mime . (static::$charset ? '; charset='.static::$charset : '')); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Force download of Response body |
||
45 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
||
46 | * @return [type] [description] |
||
47 | */ |
||
48 | public static function download($filename){ |
||
49 | static::$force_dl = $filename; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Start capturing output |
||
54 | */ |
||
55 | public static function start(){ |
||
58 | |||
59 | /** |
||
60 | * Enable CORS HTTP headers. |
||
61 | */ |
||
62 | public static function enableCORS($origin='*'){ |
||
91 | |||
92 | public static function sent() { |
||
95 | |||
96 | /** |
||
97 | * Finish the output buffer capturing. |
||
98 | * @return string The captured buffer |
||
99 | */ |
||
100 | public static function end(){ |
||
108 | |||
109 | /** |
||
110 | * Check if an response output buffering is active. |
||
111 | * @return boolean |
||
112 | */ |
||
113 | public static function isBuffering(){ |
||
116 | |||
117 | /** |
||
118 | * Clear the response body |
||
119 | */ |
||
120 | public static function clean(){ |
||
123 | |||
124 | /** |
||
125 | * Append a JSON object to the buffer. |
||
126 | * @param mixed $payload Data to append to the response buffer |
||
127 | */ |
||
128 | public static function json($payload){ |
||
132 | |||
133 | /** |
||
134 | * Append a text to the buffer. |
||
135 | * @param mixed $payload Text to append to the response buffer |
||
136 | */ |
||
137 | public static function text(...$args){ |
||
141 | |||
142 | /** |
||
143 | * Append an XML string to the buffer. |
||
144 | * @param mixed $payload Data to append to the response buffer |
||
145 | */ |
||
146 | public static function xml(...$args){ |
||
150 | |||
151 | /** |
||
152 | * Append a SVG string to the buffer. |
||
153 | * @param mixed $payload Data to append to the response buffer |
||
154 | */ |
||
155 | public static function svg(...$args){ |
||
159 | |||
160 | /** |
||
161 | * Append an HTML string to the buffer. |
||
162 | * @param mixed $payload Data to append to the response buffer |
||
163 | */ |
||
164 | public static function html(...$args){ |
||
168 | |||
169 | /** |
||
170 | * Append data to the buffer. |
||
171 | * Rules : |
||
172 | * - Callables will be called and their results added (recursive) |
||
173 | * - Views will be rendered |
||
174 | * - Objects, arrays and bools will be JSON encoded |
||
175 | * - Strings and numbers will be appendend to the response |
||
176 | * |
||
177 | * @param mixed $payload Data to append to the response buffer |
||
178 | */ |
||
179 | public static function add(){ |
||
193 | |||
194 | public static function status($code,$message=''){ |
||
197 | |||
198 | public static function header($name,$value,$code=null){ |
||
201 | |||
202 | public static function error($code=500,$message='Application Error'){ |
||
207 | |||
208 | public static function body($setBody=null){ |
||
214 | |||
215 | public static function headers($setHeaders=null){ |
||
219 | |||
220 | /** |
||
221 | * Save response as an object, for serialization or cache storage |
||
222 | * |
||
223 | * @method save |
||
224 | * |
||
225 | * @return array Headers and body of the response |
||
226 | */ |
||
227 | public static function save(){ |
||
233 | |||
234 | /** |
||
235 | * Load response from a saved state |
||
236 | * |
||
237 | * @method load |
||
238 | * |
||
239 | * @param array $data head/body saved state |
||
240 | */ |
||
241 | public static function load($data){ |
||
246 | |||
247 | public static function send($force = false){ |
||
248 | if (!static::$sent || $force) { |
||
249 | static::$sent = true; |
||
250 | static::trigger('send'); |
||
251 | Event::trigger('core.response.send'); |
||
252 | if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
||
253 | |||
254 | if (is_array($value_code)) { |
||
255 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
256 | } else { |
||
257 | $value = $value_code; |
||
258 | $code = null; |
||
259 | } |
||
260 | |||
261 | if ($value == 'Status'){ |
||
262 | if (function_exists('http_response_code')){ |
||
263 | http_response_code($code); |
||
264 | } else { |
||
265 | header("Status: $code", true, $code); |
||
266 | } |
||
267 | |||
268 | } else { |
||
269 | $code |
||
270 | ? header("$name: $value", true, $code) |
||
271 | : header("$name: $value", true); |
||
272 | } |
||
273 | } |
||
274 | if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
275 | echo static::body(); |
||
276 | static::trigger('sent'); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | } |
||
281 |
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.