Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class Request |
||
5 | { |
||
6 | const HTTP_METHOD_GET = 'GET'; |
||
7 | const HTTP_METHOD_POST = 'POST'; |
||
8 | const HTTP_METHOD_PUT = 'PUT'; |
||
9 | const HTTP_METHOD_DELETE = 'DELETE'; |
||
10 | const HTTP_METHOD_HEAD = 'HEAD'; |
||
11 | const HTTP_METHOD_OPTIONS = 'OPTIONS'; |
||
12 | |||
13 | private $method = self::HTTP_METHOD_GET; |
||
14 | private $methods = array( |
||
15 | self::HTTP_METHOD_GET => 'GET', |
||
16 | self::HTTP_METHOD_POST => 'POST', |
||
17 | self::HTTP_METHOD_PUT => 'PUT', |
||
18 | self::HTTP_METHOD_DELETE => 'DELETE', |
||
19 | self::HTTP_METHOD_HEAD => 'HEAD', |
||
20 | self::HTTP_METHOD_OPTIONS => 'OPTIONS' |
||
21 | ); |
||
22 | |||
23 | private $httpCodeString = array( |
||
24 | 100 => 'Continue', |
||
25 | 101 => 'Switching Protocols', |
||
26 | 102 => 'Processing', |
||
27 | 200 => 'OK', |
||
28 | 201 => 'Created', |
||
29 | 202 => 'Accepted', |
||
30 | 203 => 'Non-Authoritative Information', |
||
31 | 204 => 'No Content', |
||
32 | 205 => 'Reset Content', |
||
33 | 206 => 'Partial Content', |
||
34 | 207 => 'Multi-Status', |
||
35 | 208 => 'Already Reported', |
||
36 | 226 => 'IM Used', |
||
37 | 250 => 'Low on Storage Space', |
||
38 | 300 => 'Multiple Choices', |
||
39 | 301 => 'Moved Permanently', |
||
40 | 302 => 'Found', |
||
41 | 303 => 'See Other', |
||
42 | 304 => 'Not Modified', |
||
43 | 305 => 'Use Proxy', |
||
44 | 306 => '306 Switch Proxy', |
||
45 | 307 => 'Temporary Redirect', |
||
46 | 308 => 'Permanent Redirect', |
||
47 | 400 => 'Bad Request', |
||
48 | 401 => 'Unauthorized', |
||
49 | 402 => 'Payment Required', |
||
50 | 403 => 'Forbidden', |
||
51 | 404 => 'Not Found', |
||
52 | 405 => 'Method Not Allowed', |
||
53 | 406 => 'Not Acceptable', |
||
54 | 407 => 'Proxy Authentication Required', |
||
55 | 408 => 'Request Timeout', |
||
56 | 409 => 'Conflict', |
||
57 | 410 => 'Gone', |
||
58 | 411 => 'Length Required', |
||
59 | 412 => 'Precondition Failed', |
||
60 | 413 => 'Request Entity Too Large', |
||
61 | 414 => 'Request-URI Too Long', |
||
62 | 415 => 'Unsupported Media Type', |
||
63 | 416 => 'Requested Range Not Satisfiable', |
||
64 | 417 => 'Expectation Failed', |
||
65 | 422 => 'Unprocessable Entity', |
||
66 | 423 => 'Locked', |
||
67 | 424 => 'Failed Dependency', |
||
68 | 425 => 'Unordered Collection', |
||
69 | 426 => 'Upgrade Required', |
||
70 | 428 => 'Precondition Required', |
||
71 | 429 => 'Too Many Requests', |
||
72 | 431 => 'Request Header Fields Too Large', |
||
73 | 444 => 'No Response', |
||
74 | 449 => 'Retry With', |
||
75 | 450 => 'Blocked by Windows Parental Controls', |
||
76 | 494 => 'Request Header Too Large', |
||
77 | 495 => 'Cert Error', |
||
78 | 496 => 'No Cert', |
||
79 | 497 => 'HTTP to HTTPS', |
||
80 | 499 => 'Client Closed Request', |
||
81 | 500 => 'Internal Server Error', |
||
82 | 501 => 'Not Implemented', |
||
83 | 502 => 'Bad Gateway', |
||
84 | 503 => 'Service Unavailable', |
||
85 | 504 => 'Gateway Timeout', |
||
86 | 505 => 'HTTP Version Not Supported', |
||
87 | 506 => 'Variant Also Negotiates', |
||
88 | 507 => 'Insufficient Storage', |
||
89 | 508 => 'Loop Detected', |
||
90 | 509 => 'Bandwidth Limit Exceeded', |
||
91 | 510 => 'Not Extended', |
||
92 | 511 => 'Network Authentication Required' |
||
93 | ); |
||
94 | |||
95 | private $httpCode; |
||
96 | private $headers; |
||
97 | private $requestUri; |
||
98 | private $url; |
||
99 | private $body; |
||
100 | private $flashData = array(); |
||
|
|||
101 | private $path; |
||
102 | private $query; |
||
103 | |||
104 | 8 | public function __construct() |
|
109 | |||
110 | 1 | public function parse() |
|
111 | { |
||
112 | 1 | if (isset($_SERVER['REQUEST_URI'])) { |
|
113 | 1 | $this->setRequestUri($_SERVER['REQUEST_URI']); |
|
114 | 1 | $parseResult = parse_url($_SERVER['REQUEST_URI']); |
|
115 | 1 | $this->path = dataGet($parseResult, 'path'); |
|
116 | 1 | $this->query = dataGet($parseResult, 'query'); |
|
117 | 1 | } |
|
118 | |||
119 | 1 | if (isset($_SERVER['REQUEST_METHOD'])) { |
|
120 | 1 | $this->setMethod($_SERVER['REQUEST_METHOD']); |
|
121 | 1 | } |
|
122 | 1 | if (isset($_POST['_method'])) { |
|
123 | 1 | $this->setMethod($_POST['_method']); |
|
124 | 1 | } |
|
125 | |||
126 | 1 | } |
|
127 | |||
128 | 2 | public function setMethod($method) |
|
138 | |||
139 | 2 | public function getMethod() |
|
143 | |||
144 | public function setUrl($url) |
||
154 | |||
155 | 2 | public function setRequestUri($uri) |
|
161 | |||
162 | 3 | public function getRequestUri() |
|
166 | |||
167 | 1 | public function getPath() |
|
168 | { |
||
169 | 1 | return $this->path; |
|
170 | } |
||
171 | |||
172 | 1 | public function getQuery() |
|
173 | { |
||
174 | 1 | return $this->query; |
|
175 | } |
||
176 | |||
177 | 1 | public static function getPostParam($variable, $defaultValue = null) |
|
184 | |||
185 | 1 | public static function getParam($variable, $defaultValue = null) |
|
195 | |||
196 | 1 | public static function hasParam($variable) |
|
200 | |||
201 | |||
202 | 1 | public function setHeaders($headers) |
|
208 | |||
209 | 1 | public function addHeader($header, $value) |
|
215 | |||
216 | 1 | public function getHeaders() |
|
220 | |||
221 | |||
222 | 1 | public function setContentType($content_type, $encoding = null) |
|
231 | |||
232 | 1 | public function setBody($body) |
|
238 | |||
239 | 1 | public function getBody() |
|
243 | |||
244 | |||
245 | public function write() |
||
272 | |||
273 | // |
||
274 | // HTTP Code |
||
275 | // |
||
276 | |||
277 | 1 | public function setHttpCode($code) |
|
283 | |||
284 | 1 | public function getHttpCode() |
|
288 | |||
289 | public function flash($type, $data) |
||
295 | |||
296 | public function flashData($name, $value) |
||
302 | |||
303 | public function redirect($url, $httpCode = 302) |
||
311 | |||
312 | public function redirectWithSuccess($url, $message) |
||
318 | |||
319 | public function redirectWithInfo($url, $message) |
||
325 | |||
326 | public function redirectWithError($url, $message) |
||
327 | { |
||
328 | $this |
||
329 | ->flash('error', $message) |
||
330 | ->redirect($url); |
||
331 | } |
||
332 | |||
333 | public function redirectWithData($url, $key, $value) |
||
334 | { |
||
335 | $this |
||
336 | ->flashData($key, $value) |
||
337 | ->redirect($url); |
||
338 | } |
||
339 | |||
340 | 1 | public function isOK() |
|
344 | |||
345 | 1 | public function isRedirect() |
|
349 | |||
350 | 1 | public function isClientError() |
|
354 | |||
355 | 1 | public function isServerError() |
|
359 | |||
360 | 1 | private function getStringForHttpCode() |
|
366 | } |
||
367 |
This check marks private properties in classes that are never used. Those properties can be removed.