Complex classes like Output 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 Output, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 11 | trait Output  | 
            ||
| 12 | { | 
            ||
| 13 | /**  | 
            ||
| 14 | * @var string  | 
            ||
| 15 | */  | 
            ||
| 16 | protected $defaultFormat;  | 
            ||
| 17 | |||
| 18 | /**  | 
            ||
| 19 | * Get response. set for controller  | 
            ||
| 20 | *  | 
            ||
| 21 | * @return ResponseInterface  | 
            ||
| 22 | */  | 
            ||
| 23 | abstract public function getResponse();  | 
            ||
| 24 | |||
| 25 | /**  | 
            ||
| 26 | * Get response. set for controller  | 
            ||
| 27 | *  | 
            ||
| 28 | * @return ResponseInterface  | 
            ||
| 29 | */  | 
            ||
| 30 | abstract public function setResponse(ResponseInterface $response);  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * Returns the HTTP referer if it is on the current host  | 
            ||
| 34 | *  | 
            ||
| 35 | * @return string  | 
            ||
| 36 | */  | 
            ||
| 37 | abstract public function getLocalReferer();  | 
            ||
| 38 | |||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Set a response header  | 
            ||
| 42 | *  | 
            ||
| 43 | * @param string $header  | 
            ||
| 44 | * @param string $value  | 
            ||
| 45 | * @param boolean $overwrite  | 
            ||
| 46 | */  | 
            ||
| 47 | 5 | public function setResponseHeader($header, $value, $overwrite = true)  | 
            |
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Set the headers with HTTP status code and content type.  | 
            ||
| 57 | * @link http://en.wikipedia.org/wiki/List_of_HTTP_status_codes  | 
            ||
| 58 | *  | 
            ||
| 59 | * Examples:  | 
            ||
| 60 | * <code>  | 
            ||
| 61 | * $this->respondWith(200, 'json');  | 
            ||
| 62 | * $this->respondWith(200, 'application/json');  | 
            ||
| 63 | * $this->respondWith(204);  | 
            ||
| 64 |      *   $this->respondWith("204 Created"); | 
            ||
| 65 |      *   $this->respondWith('json'); | 
            ||
| 66 | * </code>  | 
            ||
| 67 | *  | 
            ||
| 68 | * @param int|string $status HTTP status (may be omitted)  | 
            ||
| 69 | * @param string|array $format Mime or content format  | 
            ||
| 70 | */  | 
            ||
| 71 | 46 | public function respondWith($status, $format = null)  | 
            |
| 92 | |||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Response with 200 OK  | 
            ||
| 96 | *  | 
            ||
| 97 | * @return ResponseInterface $response  | 
            ||
| 98 | */  | 
            ||
| 99 | 1 | public function ok()  | 
            |
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Response with created 201 code, and optionally the created location  | 
            ||
| 106 | *  | 
            ||
| 107 | * @param string $location Url of created resource  | 
            ||
| 108 | */  | 
            ||
| 109 | 2 | public function created($location = null)  | 
            |
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * Response with 203 Accepted  | 
            ||
| 120 | */  | 
            ||
| 121 | 1 | public function accepted()  | 
            |
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Response with 204 No Content  | 
            ||
| 128 | *  | 
            ||
| 129 | * @param int $code 204 (No Content) or 205 (Reset Content)  | 
            ||
| 130 | */  | 
            ||
| 131 | 2 | public function noContent($code = 204)  | 
            |
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * Respond with a 206 Partial content with `Content-Range` header  | 
            ||
| 138 | *  | 
            ||
| 139 | * @param int $rangeFrom Beginning of the range in bytes  | 
            ||
| 140 | * @param int $rangeTo End of the range in bytes  | 
            ||
| 141 | * @param int $totalSize Total size in bytes  | 
            ||
| 142 | */  | 
            ||
| 143 | 2 | public function partialContent($rangeFrom, $rangeTo, $totalSize)  | 
            |
| 150 | |||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Redirect to url and output a short message with the link  | 
            ||
| 154 | *  | 
            ||
| 155 | * @param string $url  | 
            ||
| 156 | * @param int $code 301 (Moved Permanently), 302 (Found), 303 (See Other) or 307 (Temporary Redirect)  | 
            ||
| 157 | */  | 
            ||
| 158 | 10 | public function redirect($url, $code = 303)  | 
            |
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * Redirect to previous page, or to home page  | 
            ||
| 169 | *  | 
            ||
| 170 | * @return ResponseInterface $response  | 
            ||
| 171 | */  | 
            ||
| 172 | 4 | public function back()  | 
            |
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * Respond with 304 Not Modified  | 
            ||
| 179 | */  | 
            ||
| 180 | 1 | public function notModified()  | 
            |
| 184 | |||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * Respond with 400 Bad Request  | 
            ||
| 188 | *  | 
            ||
| 189 | * @param string $message  | 
            ||
| 190 | * @param int $code HTTP status code  | 
            ||
| 191 | */  | 
            ||
| 192 | 3 | public function badRequest($message, $code = 400)  | 
            |
| 197 | |||
| 198 | /**  | 
            ||
| 199 | * Respond with a 401 Unauthorized  | 
            ||
| 200 | */  | 
            ||
| 201 | 2 | public function requireAuth()  | 
            |
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * Alias of requireAuth  | 
            ||
| 208 | * @deprecated  | 
            ||
| 209 | */  | 
            ||
| 210 | 1 | final public function requireLogin()  | 
            |
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * Respond with 402 Payment Required  | 
            ||
| 217 | *  | 
            ||
| 218 | * @param string $message  | 
            ||
| 219 | */  | 
            ||
| 220 | 3 | public function paymentRequired($message = "Payment required")  | 
            |
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * Respond with 403 Forbidden  | 
            ||
| 228 | *  | 
            ||
| 229 | * @param string $message  | 
            ||
| 230 | */  | 
            ||
| 231 | 3 | public function forbidden($message = "Access denied")  | 
            |
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Respond with 404 Not Found  | 
            ||
| 239 | *  | 
            ||
| 240 | * @param string $message  | 
            ||
| 241 | * @param int $code 404 (Not Found), 405 (Method not allowed) or 410 (Gone)  | 
            ||
| 242 | */  | 
            ||
| 243 | 4 | public function notFound($message = "Not found", $code = 404)  | 
            |
| 248 | |||
| 249 | /**  | 
            ||
| 250 | * Respond with 409 Conflict  | 
            ||
| 251 | *  | 
            ||
| 252 | * @param string $message  | 
            ||
| 253 | */  | 
            ||
| 254 | 2 | public function conflict($message)  | 
            |
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * Respond with 429 Too Many Requests  | 
            ||
| 262 | *  | 
            ||
| 263 | * @param string $message  | 
            ||
| 264 | */  | 
            ||
| 265 | 3 | public function tooManyRequests($message = "Too many requests")  | 
            |
| 270 | |||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Respond with a server error  | 
            ||
| 274 | *  | 
            ||
| 275 | * @param string $message  | 
            ||
| 276 | * @param int $code HTTP status code  | 
            ||
| 277 | */  | 
            ||
| 278 | 4 | public function error($message = "An unexpected error occured", $code = 500)  | 
            |
| 283 | |||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Get MIME type for extension  | 
            ||
| 287 | *  | 
            ||
| 288 | * @param string $format  | 
            ||
| 289 | * @return string  | 
            ||
| 290 | */  | 
            ||
| 291 | 35 | protected function getContentType($format)  | 
            |
| 306 | |||
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * If a non scalar value is passed without an format, use this format  | 
            ||
| 310 | *  | 
            ||
| 311 | * @param string $format Format by extention or MIME  | 
            ||
| 312 | */  | 
            ||
| 313 | 18 | public function byDefaultSerializeTo($format)  | 
            |
| 317 | |||
| 318 | /**  | 
            ||
| 319 | * Serialize data  | 
            ||
| 320 | *  | 
            ||
| 321 | * @param mixed $data  | 
            ||
| 322 | * @param string $contentType  | 
            ||
| 323 | * @return string  | 
            ||
| 324 | */  | 
            ||
| 325 | 52 | protected function serializeData($data, $contentType)  | 
            |
| 343 | |||
| 344 | /**  | 
            ||
| 345 | * Serialize data to JSON  | 
            ||
| 346 | * @internal made private because this will likely move to a library  | 
            ||
| 347 | *  | 
            ||
| 348 | * @param mixed $data  | 
            ||
| 349 | * @return string  | 
            ||
| 350 | */  | 
            ||
| 351 | 4 | private function serializeDataToJson($data)  | 
            |
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * Serialize data to XML.  | 
            ||
| 358 | * @internal made private because this will likely move to a library  | 
            ||
| 359 | *  | 
            ||
| 360 | * @param mixed $data  | 
            ||
| 361 | * @return string  | 
            ||
| 362 | */  | 
            ||
| 363 | 13 | private function serializeDataToXml($data)  | 
            |
| 377 | |||
| 378 | /**  | 
            ||
| 379 | * Output result  | 
            ||
| 380 | *  | 
            ||
| 381 | * @param mixed $data  | 
            ||
| 382 | * @param string $format Output format as MIME or extension  | 
            ||
| 383 | */  | 
            ||
| 384 | 52 | public function output($data, $format = null)  | 
            |
| 412 | }  | 
            ||
| 413 |