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(){ |
||
56 | static::$buffer = ob_start(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Enable CORS HTTP headers. |
||
61 | */ |
||
62 | public static function enableCORS(){ |
||
63 | |||
64 | // Allow from any origin |
||
65 | if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) { |
||
66 | static::header('Access-Control-Allow-Origin', $origin); |
||
67 | static::header('Access-Control-Allow-Credentials', 'true'); |
||
68 | static::header('Access-Control-Max-Age', 86400); |
||
69 | } |
||
70 | |||
71 | // Access-Control headers are received during OPTIONS requests |
||
72 | if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') { |
||
73 | static::clean(); |
||
74 | |||
75 | if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) { |
||
76 | static::header('Access-Control-Allow-Methods', |
||
77 | 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE'); |
||
78 | } |
||
79 | if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) { |
||
80 | static::header('Access-Control-Allow-Headers',$req_h); |
||
81 | } |
||
82 | |||
83 | static::send(); |
||
84 | exit; |
||
85 | } |
||
86 | } |
||
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(){ |
||
97 | if (static::$buffer){ |
||
98 | static::$payload[] = ob_get_contents(); |
||
99 | ob_end_clean(); |
||
100 | static::$buffer = null; |
||
101 | return end(static::$payload); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Check if an response output buffering is active. |
||
107 | * @return boolean |
||
108 | */ |
||
109 | public static function isBuffering(){ |
||
110 | return static::$buffer; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Clear the response body |
||
115 | */ |
||
116 | public static function clean(){ |
||
117 | static::$payload = []; |
||
118 | } |
||
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(){ |
||
134 | static::type(static::TYPE_TEXT); |
||
135 | static::$payload[] = implode('',func_get_args()); |
||
136 | } |
||
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(){ |
||
143 | static::type(static::TYPE_XML); |
||
144 | static::$payload[] = implode('',func_get_args()); |
||
145 | } |
||
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(){ |
||
152 | static::type(static::TYPE_SVG); |
||
153 | static::$payload[] = implode('',func_get_args()); |
||
154 | } |
||
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 data to the buffer. |
||
167 | * Rules : |
||
168 | * - Callables will be called and their results added (recursive) |
||
169 | * - Views will be rendered |
||
170 | * - Objects, arrays and bools will be JSON encoded |
||
171 | * - Strings and numbers will be appendend to the response |
||
172 | * |
||
173 | * @param mixed $payload Data to append to the response buffer |
||
174 | */ |
||
175 | public static function add(){ |
||
189 | |||
190 | public static function status($code,$message=''){ |
||
193 | |||
194 | public static function header($name,$value,$code=null){ |
||
197 | |||
198 | public static function error($code=500,$message='Application Error'){ |
||
202 | |||
203 | public static function body($setBody=null){ |
||
209 | |||
210 | public static function headers($setHeaders=null){ |
||
214 | |||
215 | /** |
||
216 | * Save response as an object, for serialization or cache storage |
||
217 | * |
||
218 | * @method save |
||
219 | * |
||
220 | * @return array Headers and body of the response |
||
221 | */ |
||
222 | public static function save(){ |
||
228 | |||
229 | /** |
||
230 | * Load response from a saved state |
||
231 | * |
||
232 | * @method load |
||
233 | * |
||
234 | * @param array $data head/body saved state |
||
235 | */ |
||
236 | public static function load($data){ |
||
241 | |||
242 | public static function send($force = false){ |
||
243 | if (!static::$sent || $force) { |
||
244 | static::$sent = true; |
||
245 | Event::trigger('core.response.send'); |
||
246 | if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
||
247 | |||
248 | if (is_array($value_code)) { |
||
249 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
250 | } else { |
||
251 | $value = $value_code; |
||
252 | $code = null; |
||
253 | } |
||
254 | |||
255 | if ($value == 'Status'){ |
||
256 | if (function_exists('http_response_code')){ |
||
257 | http_response_code($code); |
||
258 | } else { |
||
259 | header("Status: $code", true, $code); |
||
260 | } |
||
261 | |||
262 | } else { |
||
263 | $code |
||
264 | ? header("$name: $value", true, $code) |
||
265 | : header("$name: $value", true); |
||
266 | } |
||
267 | } |
||
268 | if (static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
269 | echo static::body(); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | } |
||
274 |
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
.