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 |
||
| 10 | class Request |
||
| 11 | { |
||
| 12 | use SingletonTrait; |
||
| 13 | protected $server; |
||
| 14 | protected $cookies; |
||
| 15 | protected $upload; |
||
| 16 | protected $header; |
||
| 17 | protected $data; |
||
| 18 | protected $query; |
||
| 19 | private $isLoaded = false; |
||
| 20 | |||
| 21 | 2 | public function init() |
|
| 22 | { |
||
| 23 | 2 | $this->server = $_SERVER or []; |
|
| 24 | 2 | $this->cookies = $_COOKIE or []; |
|
| 25 | 2 | $this->upload = $_FILES or []; |
|
| 26 | 2 | $this->header = $this->parseHeaders(); |
|
| 27 | 2 | $this->data = $_REQUEST or []; |
|
| 28 | 2 | $this->query = $_GET or []; |
|
| 29 | 2 | $contentType = $this->getHeader('Content-Type'); |
|
| 30 | 2 | if(null === $contentType) { |
|
| 31 | 2 | $contentType = $this->getServer('CONTENT_TYPE') ?: 'text/html'; |
|
| 32 | 2 | } |
|
| 33 | 2 | if (preg_match('/application\/json/i', $contentType)) { |
|
| 34 | $this->data += json_decode(file_get_contents("php://input"), true) ?: array(); |
||
| 35 | } |
||
| 36 | 2 | $this->isLoaded = true; |
|
| 37 | 2 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | 1 | public function isLoaded() { |
|
| 43 | 1 | return $this->isLoaded; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Método que devuelve las cabeceras de la petición |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | 2 | private function parseHeaders() |
|
| 51 | { |
||
| 52 | 2 | return getallheaders(); |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Método que verifica si existe una cabecera concreta |
||
| 57 | * @param $header |
||
| 58 | * |
||
| 59 | * @return boolean |
||
| 60 | */ |
||
| 61 | 3 | public function hasHeader($header) |
|
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Método que indica si una petición tiene cookies |
||
| 69 | * @return boolean |
||
| 70 | */ |
||
| 71 | 1 | public function hasCookies() |
|
| 72 | { |
||
| 73 | 1 | return (null !== $this->cookies && 0 !== count($this->cookies)); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Método que indica si una petición tiene cookies |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | 1 | public function hasUpload() |
|
| 81 | { |
||
| 82 | 1 | return (null !== $this->upload && 0 !== count($this->upload)); |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Método que devuelve el TimeStamp de la petición |
||
| 87 | * |
||
| 88 | * @param boolean $formatted |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public static function ts($formatted = false) |
||
| 93 | { |
||
| 94 | return self::getInstance()->getTs($formatted); |
||
| 95 | } |
||
| 96 | |||
| 97 | 1 | public function getTs($formatted = false) |
|
| 98 | { |
||
| 99 | 1 | return ($formatted) ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT']; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Método que devuelve el Método HTTP utilizado |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 4 | public function getMethod() |
|
| 107 | { |
||
| 108 | 4 | return (array_key_exists('REQUEST_METHOD', $this->server)) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET'; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Método que devuelve una cabecera de la petición si existe |
||
| 113 | * @param $name |
||
| 114 | * |
||
| 115 | * @return string|null |
||
| 116 | */ |
||
| 117 | public static function header($name) |
||
| 118 | { |
||
| 119 | return self::getInstance()->getHeader($name); |
||
| 120 | } |
||
| 121 | |||
| 122 | 2 | public function getHeader($name) |
|
| 123 | { |
||
| 124 | 2 | $header = null; |
|
| 125 | 2 | if ($this->hasHeader($name)) { |
|
| 126 | $header = $this->header[$name]; |
||
| 127 | } |
||
| 128 | 2 | return $header; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Método que devuelve la url solicitada |
||
| 133 | * @return string|null |
||
| 134 | */ |
||
| 135 | 1 | public static function requestUri() |
|
| 136 | { |
||
| 137 | 1 | return self::getInstance()->getRequestUri(); |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 8 | public function getRequestUri() |
|
| 144 | { |
||
| 145 | 8 | return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : ''; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Método que devuelve el idioma de la petición |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function getLanguage() |
||
| 153 | { |
||
| 154 | return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES'; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Método que determina si se ha solicitado un fichero |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 4 | public function isFile() |
|
| 162 | { |
||
| 163 | 4 | $file = (preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0); |
|
| 164 | 4 | return $file; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get query params |
||
| 169 | * |
||
| 170 | * @param string $queryParams |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function getQuery($queryParams) |
||
| 175 | { |
||
| 176 | return (array_key_exists($queryParams, $this->query)) ? $this->query[$queryParams] : null; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get all query params |
||
| 181 | * |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | 1 | public function getQueryParams() |
|
| 185 | { |
||
| 186 | 1 | return $this->query; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Método que devuelve un parámetro de la solicitud |
||
| 191 | * @param string $param |
||
| 192 | * |
||
| 193 | * @return string|null |
||
| 194 | */ |
||
| 195 | public function get($param) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Método que devuelve todos los datos del Request |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 1 | public function getData() |
|
| 205 | { |
||
| 206 | 1 | return $this->data; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Método que realiza una redirección a la url dada |
||
| 211 | * @param string $url |
||
| 212 | */ |
||
| 213 | public function redirect($url = null) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Devuelve un parámetro de $_SERVER |
||
| 225 | * @param string $param |
||
| 226 | * |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | 3 | public function getServer($param) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Devuelve el nombre del servidor |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function getServerName() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Devuelve el protocolo de la conexión |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getProtocol() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Devuelve la url completa de base |
||
| 254 | * @param boolean $protocol |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getRootUrl($protocol = true) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Método que devuelve el valor de una cookie en caso de que exista |
||
| 271 | * @param string $name |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 1 | public function getCookie($name) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Método que devuelve los files subidos por POST |
||
| 282 | * @param $name |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getFile($name) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Método que devuelve si la petición es ajax o no |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | 1 | public function isAjax() |
|
| 300 | |||
| 301 | } |
||
| 302 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: