| Total Complexity | 50 |
| Total Lines | 261 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 2 |
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.
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 |
||
| 25 | class Request |
||
| 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) |
||
| 41 | { |
||
| 42 | $valid = false; |
||
| 43 | if (strstr($data, '|')) { |
||
| 44 | foreach (explode('|', $data) as $value) { |
||
| 45 | $valid = $this->checkMethods($value, $method); |
||
| 46 | if ($valid) { |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } else { |
||
| 51 | $valid = $this->checkMethods($data, $method); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $valid; |
||
| 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() |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get all request headers |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | protected function getRequestHeaders() |
||
| 127 | { |
||
| 128 | |||
| 129 | |||
| 130 | // Method getallheaders() not available: manually extract 'm |
||
| 131 | $headers = []; |
||
| 132 | foreach ($_SERVER as $name => $value) { |
||
| 133 | if (substr($name, 0, 5) == 'HTTP_' || $name === 'CONTENT_TYPE' || $name === 'CONTENT_LENGTH') { |
||
| 134 | $headerKey = str_replace( |
||
| 135 | [' ', 'Http'], |
||
| 136 | ['-', 'HTTP'], |
||
| 137 | ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))) |
||
| 138 | ); |
||
| 139 | $headers[$headerKey] = $value; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $headers; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * This static method will create a new Request object, based on the |
||
| 148 | * current PHP request. |
||
| 149 | */ |
||
| 150 | public static function getRequest(): RequestBuilder |
||
| 151 | { |
||
| 152 | $serverArr = $_SERVER; |
||
| 153 | // $serverArr[] = ['Content-Type' => 'application/json']; |
||
| 154 | |||
| 155 | $r = self::createFromServerArray($serverArr); |
||
| 156 | $r->setBody(fopen('php://input', 'r')); |
||
|
|
|||
| 157 | $r->setPostData($_POST); |
||
| 158 | |||
| 159 | return $r; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sends the HTTP response back to a HTTP client. |
||
| 164 | * |
||
| 165 | * This calls php's header() function and streams the body to php://output. |
||
| 166 | */ |
||
| 167 | public static function sendResponse(Response $response) : Response |
||
| 181 | |||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * This static method will create a new Request object, based on a PHP |
||
| 186 | * $_SERVER array. |
||
| 187 | * |
||
| 188 | * REQUEST_URI and REQUEST_METHOD are required. |
||
| 189 | */ |
||
| 190 | public static function createFromServerArray(array $serverArray): RequestBuilder |
||
| 286 | } |
||
| 287 | |||
| 288 | } |
||
| 289 |