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 | |||
| 33 | |||
| 34 | public static function charset($charset){ |
||
| 37 | |||
| 38 | public static function type($mime){ |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Force download of Response body |
||
| 44 | * @param string/bool $filename Pass a falsy value to disable download or pass a filename for exporting content |
||
|
|
|||
| 45 | * @return [type] [description] |
||
| 46 | */ |
||
| 47 | public static function download($filename){ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Start capturing output |
||
| 53 | */ |
||
| 54 | public static function start(){ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Enable CORS HTTP headers. |
||
| 60 | */ |
||
| 61 | public static function enableCORS(){ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Finish the output buffer capturing. |
||
| 89 | * @return string The captured buffer |
||
| 90 | */ |
||
| 91 | public static function end(){ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Check if an response output buffering is active. |
||
| 102 | * @return boolean |
||
| 103 | */ |
||
| 104 | public static function isBuffering(){ |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Clear the response body |
||
| 110 | */ |
||
| 111 | public static function clean(){ |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Append a JSON object to the buffer. |
||
| 117 | * @param mixed $payload Data to append to the response buffer |
||
| 118 | */ |
||
| 119 | public static function json($payload){ |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Append a text to the buffer. |
||
| 126 | * @param mixed $payload Text to append to the response buffer |
||
| 127 | */ |
||
| 128 | public static function text(){ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Append an XML string to the buffer. |
||
| 135 | * @param mixed $payload Data to append to the response buffer |
||
| 136 | */ |
||
| 137 | public static function xml(){ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Append a SVG string to the buffer. |
||
| 144 | * @param mixed $payload Data to append to the response buffer |
||
| 145 | */ |
||
| 146 | public static function svg(){ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Append an HTML string to the buffer. |
||
| 153 | * @param mixed $payload Data to append to the response buffer |
||
| 154 | */ |
||
| 155 | public static function html(){ |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Append a raw string to the buffer. |
||
| 162 | * @param mixed $payload Data to append to the response buffer |
||
| 163 | */ |
||
| 164 | public static function add(){ |
||
| 167 | |||
| 168 | public static function status($code,$message=''){ |
||
| 171 | |||
| 172 | public static function header($name,$value,$code=null){ |
||
| 175 | |||
| 176 | public static function error($code=500,$message='Application Error'){ |
||
| 180 | |||
| 181 | public static function body($setBody=null){ |
||
| 185 | |||
| 186 | public static function headers($setHeaders=null){ |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Save response as an object, for serialization or cache storage |
||
| 193 | * |
||
| 194 | * @method save |
||
| 195 | * |
||
| 196 | * @return array Headers and body of the response |
||
| 197 | */ |
||
| 198 | public static function save(){ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Load response from a saved state |
||
| 207 | * |
||
| 208 | * @method load |
||
| 209 | * |
||
| 210 | * @param array $data head/body saved state |
||
| 211 | */ |
||
| 212 | public static function load($data){ |
||
| 217 | |||
| 218 | public static function send(){ |
||
| 219 | Event::trigger('core.response.send'); |
||
| 220 | if (false === headers_sent()) foreach (static::$headers as $name => $value_code) { |
||
| 221 | if (is_array($value_code)) { |
||
| 222 | list($value, $code) = (count($value_code) > 1) ? $value_code : [current($value_code), 200]; |
||
| 223 | } else { |
||
| 224 | $value = $value_code; |
||
| 225 | $code = null; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ($value == 'Status'){ |
||
| 229 | if (function_exists('http_response_code')){ |
||
| 230 | http_response_code($code); |
||
| 231 | } else { |
||
| 232 | header("Status: $code", true, $code); |
||
| 233 | } |
||
| 234 | |||
| 235 | } else { |
||
| 236 | $code |
||
| 237 | ? header("$name: $value", true, $code) |
||
| 238 | : header("$name: $value", true); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | if (!static::$force_dl) header('Content-Disposition: attachment; filename="'.static::$force_dl.'"'); |
||
| 242 | echo static::body(); |
||
| 243 | } |
||
| 244 | |||
| 245 | } |
||
| 246 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.