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 | 1 | 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 | 14 | 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 | * Returns the HTTP referer if it is on the current host |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | 4 | public function getLocalReferer() |
|
355 | |||
356 | /** |
||
357 | * Output result |
||
358 | * |
||
359 | * @param mixed $data |
||
360 | * @param string $format |
||
361 | * @return ResponseInterface $response |
||
362 | */ |
||
363 | 9 | public function output($data, $format) |
|
374 | |||
375 | /** |
||
376 | * Encode data to send to client |
||
377 | * |
||
378 | * @param mixed $data |
||
379 | * @param string $format |
||
380 | * @return string |
||
381 | */ |
||
382 | 11 | public function encodeData($data, $format) |
|
393 | |||
394 | /** |
||
395 | * Encode data as xml |
||
396 | * |
||
397 | * @param \SimpleXMLElement $data |
||
398 | * @return string |
||
399 | */ |
||
400 | 2 | protected function encodeDataAsXml(\SimpleXMLElement $data) |
|
404 | |||
405 | /** |
||
406 | * Encode data as json |
||
407 | * |
||
408 | * @param mixed |
||
409 | * @return string |
||
410 | */ |
||
411 | 7 | protected function encodeDataAsJson($data) |
|
419 | |||
420 | /** |
||
421 | * Check if we should respond with jsonp |
||
422 | * |
||
423 | * @return boolean |
||
424 | */ |
||
425 | 7 | protected function isJsonp() |
|
431 | |||
432 | /** |
||
433 | * Get status code of response |
||
434 | * |
||
435 | * @return int |
||
436 | */ |
||
437 | 14 | protected function getResponseStatusCode() |
|
443 | |||
444 | /** |
||
445 | * Get valid content type by simple word description |
||
446 | * |
||
447 | * @param string $format |
||
448 | * @return string |
||
449 | */ |
||
450 | 18 | protected function getContentType($format) |
|
454 | } |
||
455 | |||
456 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: