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 |
||
| 22 | class Request |
||
| 23 | { |
||
| 24 | use SingletonTrait; |
||
| 25 | protected $server; |
||
| 26 | protected $cookies; |
||
| 27 | protected $upload; |
||
| 28 | protected $header; |
||
| 29 | protected $data; |
||
| 30 | protected $query; |
||
| 31 | |||
| 32 | /** |
||
| 33 | */ |
||
| 34 | 1 | public function __construct() |
|
| 35 | { |
||
| 36 | 1 | $this->server = $_SERVER or []; |
|
|
|
|||
| 37 | 1 | $this->cookies = $_COOKIE or []; |
|
| 38 | 1 | $this->upload = $_FILES or []; |
|
| 39 | 1 | $this->header = $this->parseHeaders(); |
|
| 40 | 1 | $this->data = $_REQUEST or []; |
|
| 41 | 1 | $this->query = $_GET or []; |
|
| 42 | 1 | $contentType = (array_key_exists('Content-Type', $this->header)) ? $this->header['Content-Type'] : "text/html"; |
|
| 43 | 1 | if (preg_match('/application\/json/i', $contentType)) { |
|
| 44 | $this->data += json_decode(file_get_contents("php://input"), true) ?: array(); |
||
| 45 | } |
||
| 46 | 1 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Método que devuelve las cabeceras de la petición |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | 1 | private function parseHeaders() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Método que verifica si existe una cabecera concreta |
||
| 59 | * @param $header |
||
| 60 | * |
||
| 61 | * @return boolean |
||
| 62 | */ |
||
| 63 | 1 | public function hasHeader($header) |
|
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Método que indica si una petición tiene cookies |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | 1 | public function hasCookies() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Método que indica si una petición tiene cookies |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | 1 | public function hasUpload() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Método que devuelve el TimeStamp de la petición |
||
| 89 | * |
||
| 90 | * @param boolean $formatted |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public static function ts($formatted = false) |
||
| 98 | |||
| 99 | 1 | public function getTs($formatted = false) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Método que devuelve el Método HTTP utilizado |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 3 | public function getMethod() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Método que devuelve una cabecera de la petición si existe |
||
| 115 | * @param $name |
||
| 116 | * |
||
| 117 | * @return string|null |
||
| 118 | */ |
||
| 119 | public static function header($name) |
||
| 123 | |||
| 124 | public function getHeader($name) |
||
| 125 | { |
||
| 126 | $header = null; |
||
| 127 | if ($this->hasHeader($name)) { |
||
| 128 | $header = $this->header[$name]; |
||
| 129 | } |
||
| 130 | return $header; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Método que devuelve la url solicitada |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | public static function requestUri() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 6 | public function getRequestUri() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Método que devuelve el idioma de la petición |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getLanguage() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Método que determina si se ha solicitado un fichero |
||
| 161 | * @return boolean |
||
| 162 | */ |
||
| 163 | 4 | public function isFile() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get query params |
||
| 171 | * |
||
| 172 | * @param string $queryParams |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function getQuery($queryParams) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get all query params |
||
| 183 | * |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | public function getQueryParams() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Método que devuelve un parámetro de la solicitud |
||
| 193 | * @param string $param |
||
| 194 | * |
||
| 195 | * @return string|null |
||
| 196 | */ |
||
| 197 | public function get($param) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Método que devuelve todos los datos del Request |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getData() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Método que realiza una redirección a la url dada |
||
| 213 | * @param string $url |
||
| 214 | */ |
||
| 215 | public function redirect($url = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Devuelve un parámetro de $_SERVER |
||
| 227 | * @param string $param |
||
| 228 | * |
||
| 229 | * @return string|null |
||
| 230 | */ |
||
| 231 | 2 | public function getServer($param) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Devuelve el nombre del servidor |
||
| 238 | * @return string|null |
||
| 239 | */ |
||
| 240 | public function getServerName() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Devuelve el protocolo de la conexión |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getProtocol() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Devuelve la url completa de base |
||
| 256 | * @param boolean $protocol |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getRootUrl($protocol = true) |
||
| 260 | { |
||
| 261 | $host = $this->getServerName(); |
||
| 262 | $protocol = $protocol ? $this->getProtocol() : ''; |
||
| 263 | $url = ''; |
||
| 264 | if (!empty($host) && !empty($protocol)) $url = $protocol . $host; |
||
| 265 | if (!in_array($this->getServer('SERVER_PORT'), [80, 443])) { |
||
| 266 | $url .= ':' . $this->getServer('SERVER_PORT'); |
||
| 267 | } |
||
| 268 | return $url; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Método que devuelve el valor de una cookie en caso de que exista |
||
| 273 | * @param string $name |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getCookie($name) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Método que devuelve los files subidos por POST |
||
| 284 | * @param $name |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getFile($name) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Método que devuelve si la petición es ajax o no |
||
| 295 | * @return boolean |
||
| 296 | */ |
||
| 297 | 1 | public function isAjax() |
|
| 302 | |||
| 303 | } |
||
| 304 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.