Total Complexity | 66 |
Total Lines | 329 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like RouterRequest 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.
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 RouterRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class RouterRequest |
||
26 | { |
||
27 | /** |
||
28 | * @var string $validMethods Valid methods for Requests |
||
29 | */ |
||
30 | public $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH'; |
||
31 | |||
32 | /** |
||
33 | * Request method validation |
||
34 | * |
||
35 | * @param string $data |
||
36 | * @param string $method |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | public function validMethod($data, $method) |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get the request method used, taking overrides into account |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getRequestMethod() |
||
63 | { |
||
64 | // Take the method as found in $_SERVER |
||
65 | $method = $_SERVER['REQUEST_METHOD']; |
||
66 | // If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification |
||
67 | // @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 |
||
68 | if ($method === 'HEAD') { |
||
69 | ob_start(); |
||
70 | $method = 'GET'; |
||
71 | } elseif ($method === 'POST') { |
||
72 | $headers = $this->getRequestHeaders(); |
||
73 | if (isset($headers['X-HTTP-Method-Override']) && |
||
74 | in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'])) { |
||
75 | $method = $headers['X-HTTP-Method-Override']; |
||
76 | } elseif (!empty($_POST['_method'])) { |
||
77 | $method = strtoupper($_POST['_method']); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $method; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * check method valid |
||
86 | * |
||
87 | * @param string $value |
||
88 | * @param string $method |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | protected function checkMethods($value, $method) |
||
93 | { |
||
94 | if (in_array($value, explode('|', $this->validMethods))) { |
||
95 | if ($this->isAjax() && $value === 'AJAX') { |
||
96 | return true; |
||
97 | } |
||
98 | |||
99 | if ($this->isAjax() && strpos($value, 'X') === 0 && $method === ltrim($value, 'X')) { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | if (in_array($value, [$method, 'ANY'])) { |
||
104 | return true; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return false; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Check ajax request |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | protected function isAjax() |
||
117 | { |
||
118 | return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Get all request headers |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | protected function getRequestHeaders() |
||
127 | { |
||
128 | // If getallheaders() is available, use that |
||
129 | if (function_exists('getallheaders')) { |
||
130 | return getallheaders(); |
||
|
|||
131 | } |
||
132 | |||
133 | // Method getallheaders() not available: manually extract 'm |
||
134 | $headers = []; |
||
135 | foreach ($_SERVER as $name => $value) { |
||
136 | if (substr($name, 0, 5) == 'HTTP_' || $name === 'CONTENT_TYPE' || $name === 'CONTENT_LENGTH') { |
||
137 | $headerKey = str_replace( |
||
138 | [' ', 'Http'], |
||
139 | ['-', 'HTTP'], |
||
140 | ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))) |
||
141 | ); |
||
142 | $headers[$headerKey] = $value; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return $headers; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * This static method will create a new Request object, based on the |
||
151 | * current PHP request. |
||
152 | */ |
||
153 | public static function getRequest(): Request |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Sends the HTTP response back to a HTTP client. |
||
173 | * |
||
174 | * This calls php's header() function and streams the body to php://output. |
||
175 | */ |
||
176 | public static function sendResponse(Response $response) |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * This static method will create a new Request object, based on a PHP |
||
254 | * $_SERVER array. |
||
255 | * |
||
256 | * REQUEST_URI and REQUEST_METHOD are required. |
||
257 | */ |
||
258 | public static function createFromServerArray(array $serverArray): Request |
||
354 | } |
||
355 | |||
357 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.