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; |
||
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){ |
||
38 | |||
39 | public static function type($mime){ |
||
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){ |
||
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(){ |
||
87 | |||
88 | public static function sent() { |
||
91 | |||
92 | /** |
||
93 | * Finish the output buffer capturing. |
||
94 | * @return string The captured buffer |
||
95 | */ |
||
96 | public static function end(){ |
||
104 | |||
105 | /** |
||
106 | * Check if an response output buffering is active. |
||
107 | * @return boolean |
||
108 | */ |
||
109 | public static function isBuffering(){ |
||
112 | |||
113 | /** |
||
114 | * Clear the response body |
||
115 | */ |
||
116 | public static function clean(){ |
||
119 | |||
120 | /** |
||
121 | * Append a JSON object to the buffer. |
||
122 | * @param mixed $payload Data to append to the response buffer |
||
123 | */ |
||
124 | public static function json($payload){ |
||
128 | |||
129 | /** |
||
130 | * Append a text to the buffer. |
||
131 | * @param mixed $payload Text to append to the response buffer |
||
132 | */ |
||
133 | public static function text(){ |
||
137 | |||
138 | /** |
||
139 | * Append an XML string to the buffer. |
||
140 | * @param mixed $payload Data to append to the response buffer |
||
141 | */ |
||
142 | public static function xml(){ |
||
146 | |||
147 | /** |
||
148 | * Append a SVG string to the buffer. |
||
149 | * @param mixed $payload Data to append to the response buffer |
||
150 | */ |
||
151 | public static function svg(){ |
||
155 | |||
156 | /** |
||
157 | * Append an HTML string to the buffer. |
||
158 | * @param mixed $payload Data to append to the response buffer |
||
159 | */ |
||
160 | public static function html(){ |
||
164 | |||
165 | /** |
||
166 | * Append a raw string to the buffer. |
||
167 | * @param mixed $payload Data to append to the response buffer |
||
168 | */ |
||
169 | public static function add(){ |
||
172 | |||
173 | public static function status($code,$message=''){ |
||
176 | |||
177 | public static function header($name,$value,$code=null){ |
||
180 | |||
181 | public static function error($code=500,$message='Application Error'){ |
||
185 | |||
186 | public static function body($setBody=null){ |
||
190 | |||
191 | public static function headers($setHeaders=null){ |
||
195 | |||
196 | /** |
||
197 | * Save response as an object, for serialization or cache storage |
||
198 | * |
||
199 | * @method save |
||
200 | * |
||
201 | * @return array Headers and body of the response |
||
202 | */ |
||
203 | public static function save(){ |
||
209 | |||
210 | /** |
||
211 | * Load response from a saved state |
||
212 | * |
||
213 | * @method load |
||
214 | * |
||
215 | * @param array $data head/body saved state |
||
216 | */ |
||
217 | public static function load($data){ |
||
222 | |||
223 | public static function send(){ |
||
224 | static::$sent = true; |
||
225 | Event::trigger('core.response.send'); |
||
226 | if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
||
227 | if (is_array($value_code)) { |
||
228 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
229 | } else { |
||
230 | $value = $value_code; |
||
231 | $code = null; |
||
232 | } |
||
233 | |||
234 | if ($value == 'Status'){ |
||
235 | if (function_exists('http_response_code')){ |
||
236 | http_response_code($code); |
||
237 | } else { |
||
238 | header("Status: $code", true, $code); |
||
239 | } |
||
240 | |||
241 | } else { |
||
242 | $code |
||
243 | ? header("$name: $value", true, $code) |
||
244 | : header("$name: $value", true); |
||
245 | } |
||
246 | } |
||
247 | if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
248 | echo static::body(); |
||
249 | } |
||
250 | |||
251 | } |
||
252 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.