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 | public function parse() |
||
111 | { |
||
112 | if (isset($_SERVER['REQUEST_URI'])) { |
||
113 | $this->setRequestUri($_SERVER['REQUEST_URI']); |
||
114 | $parseResult = parse_url($_SERVER['REQUEST_URI']); |
||
115 | $this->path = dataGet($parseResult, 'path'); |
||
116 | $this->query = dataGet($parseResult, 'query'); |
||
117 | } |
||
118 | |||
119 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
120 | $this->setMethod($_SERVER['REQUEST_METHOD']); |
||
121 | } |
||
122 | if (isset($_POST['_method'])) { |
||
123 | $this->setMethod($_POST['_method']); |
||
124 | } |
||
125 | |||
126 | } |
||
127 | |||
128 | 1 | public function setMethod($method) |
|
129 | { |
||
130 | if (!isset($this->methods[$method])) { |
||
131 | throw new \InvalidArgumentException('Invalid HTTP Method ' . $method); |
||
132 | } |
||
133 | |||
134 | 1 | $this->method = $method; |
|
135 | |||
136 | 1 | return $this; |
|
137 | } |
||
138 | |||
139 | 1 | public function getMethod() |
|
143 | |||
144 | public function setUrl($url) |
||
145 | { |
||
146 | $this->url = $url; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | public function getUrl() |
||
151 | { |
||
152 | return $this->url; |
||
153 | } |
||
154 | |||
155 | 1 | 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) |
|
178 | { |
||
179 | if (isset($_POST[$variable])) { |
||
180 | return $_POST[$variable]; |
||
181 | } |
||
182 | 1 | return $defaultValue; |
|
183 | } |
||
184 | |||
185 | 1 | public static function getParam($variable, $defaultValue = null) |
|
186 | { |
||
187 | if (isset($_GET[$variable])) { |
||
188 | 1 | return $_GET[$variable]; |
|
189 | } elseif (isset($_POST[$variable])) { |
||
190 | return $_POST[$variable]; |
||
191 | } else { |
||
192 | 1 | return $defaultValue; |
|
193 | } |
||
194 | } |
||
195 | |||
196 | public static function hasParam($variable) |
||
197 | { |
||
198 | return isset($_GET[$variable]) || isset($_POST[$variable]); |
||
199 | } |
||
200 | |||
201 | |||
202 | public function setHeaders($headers) |
||
203 | { |
||
204 | $this->headers = $headers; |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | public function addHeader($header, $value) |
||
210 | { |
||
211 | $this->headers[$header] = $value; |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | public function getHeaders() |
||
217 | { |
||
218 | return $this->headers; |
||
219 | } |
||
220 | |||
221 | |||
222 | 1 | public function setContentType($content_type, $encoding = null) |
|
223 | { |
||
224 | if ($encoding !== null) { |
||
225 | 1 | $content_type .= '; charset=' . $encoding; |
|
226 | } |
||
227 | $this->addHeader('Content-type', $content_type); |
||
228 | |||
229 | return $this; |
||
230 | 1 | } |
|
231 | |||
232 | 1 | public function setBody($body) |
|
238 | |||
239 | 1 | public function getBody() |
|
243 | |||
244 | |||
245 | public function write() |
||
246 | { |
||
247 | if (!headers_sent()) { |
||
248 | if (substr(php_sapi_name(), 0, 3) == 'cgi') { |
||
249 | $headerString = 'Status: ' . self::getStringForHttpCode(); |
||
250 | } else { |
||
251 | $headerString = 'HTTP/1.1 ' . self::getStringForHttpCode(); |
||
252 | } |
||
253 | |||
254 | header($headerString); |
||
255 | |||
256 | // Send headers |
||
257 | foreach ($this->headers as $headerName => $headerValue) { |
||
258 | header($headerName . ':' . $headerValue); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | TODO HANDLE HTTP RESPONSE CODE |
||
264 | */ |
||
265 | if ($this->httpCode !== null) { |
||
266 | |||
267 | } |
||
268 | |||
269 | // Send body |
||
270 | echo $this->body; |
||
271 | } |
||
272 | |||
273 | // |
||
274 | // HTTP Code |
||
275 | // |
||
276 | |||
277 | public function setHttpCode($code) |
||
278 | { |
||
279 | $this->httpCode = $code; |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | 1 | public function getHttpCode() |
|
288 | |||
289 | public function flash($type, $data) |
||
290 | { |
||
291 | Flash::write($type, $data); |
||
292 | |||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | public function flashData($name, $value) |
||
297 | { |
||
298 | Flash::write('data', array($name => $value)); |
||
299 | |||
300 | return $this; |
||
301 | } |
||
302 | |||
303 | public function redirect($url, $httpCode = 302) |
||
304 | { |
||
305 | $this->setHttpCode($httpCode); |
||
311 | |||
312 | public function redirectWithSuccess($url, $message) |
||
318 | |||
319 | public function redirectWithInfo($url, $message) |
||
325 | |||
326 | public function redirectWithError($url, $message) |
||
332 | |||
333 | public function redirectWithData($url, $key, $value) |
||
339 | |||
340 | public function isOK() |
||
344 | |||
345 | public function isRedirect() |
||
349 | |||
350 | public function isClientError() |
||
354 | |||
355 | 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.