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 mixed $data Pass a falsy value to disable download, pass a filename for exporting content or array with raw string data |
||
47 | * @return void |
||
48 | */ |
||
49 | public static function download($data){ |
||
57 | |||
58 | /** |
||
59 | * Start capturing output |
||
60 | */ |
||
61 | public static function start(){ |
||
64 | |||
65 | /** |
||
66 | * Enable CORS HTTP headers. |
||
67 | */ |
||
68 | public static function enableCORS($origin='*'){ |
||
97 | |||
98 | public static function sent() { |
||
101 | |||
102 | /** |
||
103 | * Finish the output buffer capturing. |
||
104 | * @return string The captured buffer |
||
105 | */ |
||
106 | public static function end(){ |
||
114 | |||
115 | /** |
||
116 | * Check if an response output buffering is active. |
||
117 | * @return boolean |
||
118 | */ |
||
119 | public static function isBuffering(){ |
||
122 | |||
123 | /** |
||
124 | * Clear the response body |
||
125 | */ |
||
126 | public static function clean(){ |
||
130 | |||
131 | /** |
||
132 | * Append a JSON object to the buffer. |
||
133 | * @param mixed $payload Data to append to the response buffer |
||
134 | */ |
||
135 | public static function json($payload){ |
||
139 | |||
140 | /** |
||
141 | * Append a text to the buffer. |
||
142 | * @param mixed $payload Text to append to the response buffer |
||
143 | */ |
||
144 | public static function text(...$args){ |
||
148 | |||
149 | /** |
||
150 | * Append an XML string to the buffer. |
||
151 | * @param mixed $payload Data to append to the response buffer |
||
|
|||
152 | */ |
||
153 | public static function xml(...$args){ |
||
157 | |||
158 | /** |
||
159 | * Append a SVG string to the buffer. |
||
160 | * @param mixed $payload Data to append to the response buffer |
||
161 | */ |
||
162 | public static function svg(...$args){ |
||
166 | |||
167 | /** |
||
168 | * Append an HTML string to the buffer. |
||
169 | * @param mixed $payload Data to append to the response buffer |
||
170 | */ |
||
171 | public static function html(...$args){ |
||
175 | |||
176 | /** |
||
177 | * Append data to the buffer. |
||
178 | * Rules : |
||
179 | * - Callables will be called and their results added (recursive) |
||
180 | * - Views will be rendered |
||
181 | * - Objects, arrays and bools will be JSON encoded |
||
182 | * - Strings and numbers will be appendend to the response |
||
183 | * |
||
184 | * @param mixed $payload Data to append to the response buffer |
||
185 | */ |
||
186 | public static function add(){ |
||
200 | |||
201 | public static function status($code,$message=''){ |
||
204 | |||
205 | public static function header($name,$value,$code=null){ |
||
212 | |||
213 | public static function error($code=500,$message='Application Error'){ |
||
218 | |||
219 | public static function body($setBody=null){ |
||
225 | |||
226 | public static function headers($setHeaders=null){ |
||
230 | |||
231 | /** |
||
232 | * Save response as an object, for serialization or cache storage |
||
233 | * |
||
234 | * @method save |
||
235 | * |
||
236 | * @return array Headers and body of the response |
||
237 | */ |
||
238 | public static function save(){ |
||
244 | |||
245 | /** |
||
246 | * Load response from a saved state |
||
247 | * |
||
248 | * @method load |
||
249 | * |
||
250 | * @param array $data head/body saved state |
||
251 | */ |
||
252 | public static function load($data){ |
||
257 | |||
258 | public static function send($force = false){ |
||
259 | if (!static::$sent || $force) { |
||
260 | static::$sent = true; |
||
261 | static::trigger('send'); |
||
262 | Event::trigger('core.response.send'); |
||
263 | if (false === headers_sent()) foreach (static::$headers as $name => $family) |
||
264 | foreach ($family as $value_code) { |
||
265 | |||
266 | if (is_array($value_code)) { |
||
267 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
268 | } else { |
||
269 | $value = $value_code; |
||
270 | $code = null; |
||
271 | } |
||
272 | |||
273 | switch($value){ |
||
274 | case "Status": |
||
275 | if (function_exists('http_response_code')){ |
||
276 | http_response_code($code); |
||
277 | } else { |
||
278 | header("Status: $code", true, $code); |
||
279 | } |
||
280 | break; |
||
281 | case "Link": |
||
282 | header("Link: $value", false); |
||
283 | break; |
||
284 | default: |
||
285 | if ($code) { |
||
286 | header("$name: $value", true, $code); |
||
287 | } else { |
||
288 | header("$name: $value", true); |
||
289 | } |
||
290 | break; |
||
291 | } |
||
292 | } |
||
293 | if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
294 | echo static::body(); |
||
295 | static::trigger('sent'); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Push resources to client (HTTP/2 spec) |
||
302 | * @param string/array $links The link(s) to the resources to push. |
||
303 | * @return Response The Route object |
||
304 | */ |
||
305 | public static function push($links, $type='text'){ |
||
324 | |||
325 | } |
||
326 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.