Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Controller |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Server request |
||
| 15 | * @var ServerRequestInterface |
||
| 16 | **/ |
||
| 17 | protected $request = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Response |
||
| 21 | * @var ResponseInterface |
||
| 22 | **/ |
||
| 23 | protected $response = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Common input and output formats with associated MIME |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $contentFormats = [ |
||
| 30 | 'text/html' => 'html', |
||
| 31 | 'application/json' => 'json', |
||
| 32 | 'application/xml' => 'xml', |
||
| 33 | 'text/xml' => 'xml', |
||
| 34 | 'text/plain' => 'text', |
||
| 35 | 'application/javascript' => 'js', |
||
| 36 | 'text/css' => 'css', |
||
| 37 | 'image/png' => 'png', |
||
| 38 | 'image/gif' => 'gif', |
||
| 39 | 'image/jpeg' => 'jpeg', |
||
| 40 | 'image/x-icon' => 'ico', |
||
| 41 | 'application/x-www-form-urlencoded' => 'post', |
||
| 42 | 'multipart/form-data' => 'post' |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Run the controller |
||
| 47 | * |
||
| 48 | * @return ResponseInterface |
||
| 49 | */ |
||
| 50 | abstract public function run(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get request, set for controller |
||
| 54 | * |
||
| 55 | * @return ServerRequestInterface |
||
| 56 | */ |
||
| 57 | 6 | public function getRequest() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Get response. set for controller |
||
| 64 | * |
||
| 65 | * @return ResponseInterface |
||
| 66 | */ |
||
| 67 | 15 | public function getResponse() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Run the controller as function |
||
| 74 | * |
||
| 75 | * @param ServerRequestInterface $request |
||
| 76 | * @param ResponseInterface $response |
||
| 77 | * @return ResponseInterface |
||
| 78 | */ |
||
| 79 | 19 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Set the headers with HTTP status code and content type. |
||
| 89 | * @link http://en.wikipedia.org/wiki/List_of_HTTP_status_codes |
||
| 90 | * |
||
| 91 | * Examples: |
||
| 92 | * <code> |
||
| 93 | * $this->responseWith(200, 'json'); |
||
| 94 | * $this->responseWith(200, 'application/json'); |
||
| 95 | * $this->responseWith(204); |
||
| 96 | * $this->responseWith("204 Created"); |
||
| 97 | * $this->responseWith('json'); |
||
| 98 | * </code> |
||
| 99 | * |
||
| 100 | * @param int $code HTTP status code (may be omitted) |
||
| 101 | * @param string|array $format Mime or content format |
||
| 102 | * @return ResponseInterface $response |
||
| 103 | */ |
||
| 104 | 15 | public function responseWith($code, $format = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Response with success 200 code |
||
| 127 | * |
||
| 128 | * @return ResponseInterface $response |
||
| 129 | */ |
||
| 130 | 1 | public function ok() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Response with created 201 code, and optionaly redirect to created location |
||
| 137 | * |
||
| 138 | * @param string $location Url of created resource |
||
| 139 | * @return ResponseInterface $response |
||
| 140 | */ |
||
| 141 | 2 | public function created($location = '') |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Response with 204 'No Content' |
||
| 154 | * |
||
| 155 | * @return ResponseInterface $response |
||
| 156 | */ |
||
| 157 | 1 | public function noContent() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Redirect to url |
||
| 164 | * |
||
| 165 | * @param string $url |
||
| 166 | * @param int $code 301 (Moved Permanently), 303 (See Other) or 307 (Temporary Redirect) |
||
| 167 | * @return ResponseInterface $response |
||
| 168 | */ |
||
| 169 | 6 | public function redirect($url, $code = 303) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Redirect to previous page, or to home page |
||
| 180 | * |
||
| 181 | * @return ResponseInterface $response |
||
| 182 | */ |
||
| 183 | 2 | public function back() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Route to 401 |
||
| 190 | * Note: While the 401 route is used, we don't respond with a 401 http status code. |
||
| 191 | * |
||
| 192 | * @return ResponseInterface $response |
||
| 193 | */ |
||
| 194 | 2 | public function requireLogin() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Alias of requireLogin |
||
| 201 | * |
||
| 202 | * @return ResponseInterface $response |
||
| 203 | */ |
||
| 204 | 1 | public function requireAuth() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Set response to error 'Bad Request' state |
||
| 211 | * |
||
| 212 | * @param string $message |
||
| 213 | * @param int $code HTTP status code |
||
| 214 | * @return ResponseInterface $response |
||
| 215 | */ |
||
| 216 | 2 | public function badRequest($message, $code = 400) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Set response to error 'Forbidden' state |
||
| 223 | * |
||
| 224 | * @param string $message |
||
| 225 | * @param int $code HTTP status code |
||
| 226 | * @return ResponseInterface $response |
||
| 227 | */ |
||
| 228 | 2 | public function forbidden($message, $code = 403) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Set response to error 'Not Found' state |
||
| 235 | * |
||
| 236 | * @param string $message |
||
| 237 | * @param int $code HTTP status code |
||
| 238 | * @return ResponseInterface $response |
||
| 239 | */ |
||
| 240 | 2 | public function notFound($message, $code = 404) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Set response to error 'Conflict' state |
||
| 247 | * |
||
| 248 | * @param string $message |
||
| 249 | * @param int $code HTTP status code |
||
| 250 | * @return ResponseInterface $response |
||
| 251 | */ |
||
| 252 | 2 | public function conflict($message, $code = 409) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Set response to error 'Too Many Requests' state |
||
| 259 | * |
||
| 260 | * @param string $message |
||
| 261 | * @param int $code HTTP status code |
||
| 262 | * @return ResponseInterface $response |
||
| 263 | */ |
||
| 264 | 2 | public function tooManyRequests($message, $code = 429) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set response to error state |
||
| 271 | * |
||
| 272 | * @param string $message |
||
| 273 | * @param int $code HTTP status code |
||
| 274 | * @return ResponseInterface $response |
||
| 275 | */ |
||
| 276 | 12 | public function error($message, $code = 400) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Check if response is 2xx succesful, or empty |
||
| 288 | * |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | 14 | public function isSuccessful() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Check if response is a 3xx redirect |
||
| 300 | * |
||
| 301 | * @return boolean |
||
| 302 | */ |
||
| 303 | 14 | public function isRedirection() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Check if response is a 4xx client error |
||
| 312 | * |
||
| 313 | * @return boolean |
||
| 314 | */ |
||
| 315 | 14 | public function isClientError() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Check if response is a 5xx redirect |
||
| 324 | * |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | 14 | public function isServerError() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Check if response is 4xx or 5xx error |
||
| 334 | * |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | 13 | public function isError() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Check if request is GET request |
||
| 344 | * |
||
| 345 | * @return boolean |
||
| 346 | */ |
||
| 347 | 5 | public function isGetRequest() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Check if request is POST request |
||
| 356 | * |
||
| 357 | * @return boolean |
||
| 358 | */ |
||
| 359 | 5 | public function isPostRequest() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Check if request is PUT request |
||
| 366 | * |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | 5 | public function isPutRequest() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Check if request is DELETE request |
||
| 376 | * |
||
| 377 | * @return boolean |
||
| 378 | */ |
||
| 379 | 5 | public function isDeleteRequest() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Check if request is HEAD request |
||
| 386 | * |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | 5 | public function isHeadRequest() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Returns the HTTP referer if it is on the current host |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 4 | public function getLocalReferer() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Output result |
||
| 410 | * |
||
| 411 | * @param mixed $data |
||
| 412 | * @param string $format |
||
| 413 | * @return ResponseInterface $response |
||
| 414 | */ |
||
| 415 | 9 | public function output($data, $format) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Encode data to send to client |
||
| 429 | * |
||
| 430 | * @param mixed $data |
||
| 431 | * @param string $format |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | 11 | public function encodeData($data, $format) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Encode data as xml |
||
| 448 | * |
||
| 449 | * @param \SimpleXMLElement $data |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | 2 | protected function encodeDataAsXml(\SimpleXMLElement $data) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Encode data as json |
||
| 459 | * |
||
| 460 | * @param mixed |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | 7 | protected function encodeDataAsJson($data) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Check if we should respond with jsonp |
||
| 474 | * |
||
| 475 | * @return boolean |
||
| 476 | */ |
||
| 477 | 7 | protected function isJsonp() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Get status code of response |
||
| 486 | * |
||
| 487 | * @return int |
||
| 488 | */ |
||
| 489 | 14 | protected function getResponseStatusCode() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get valid content type by simple word description |
||
| 498 | * |
||
| 499 | * @param string $format |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | 18 | protected function getContentType($format) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Get method of request |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | 5 | protected function getRequestMethod() |
|
| 518 | } |
||
| 519 | |||
| 520 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: