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 |
||
| 34 | class Request |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Available masks for cleaning variables |
||
| 38 | */ |
||
| 39 | const MASK_NO_TRIM = 1; |
||
| 40 | const MASK_ALLOW_RAW = 2; |
||
| 41 | const MASK_ALLOW_HTML = 4; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Gets the request method |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | 2 | public static function getMethod() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Fetches and returns a given variable. |
||
| 57 | * |
||
| 58 | * The default behaviour is fetching variables depending on the |
||
| 59 | * current request method: GET and HEAD will result in returning |
||
| 60 | * an entry from $_GET, POST and PUT will result in returning an |
||
| 61 | * entry from $_POST. |
||
| 62 | * |
||
| 63 | * You can force the source by setting the $hash parameter: |
||
| 64 | * |
||
| 65 | * - post $_POST |
||
| 66 | * - get $_GET |
||
| 67 | * - files $_FILES |
||
| 68 | * - cookie $_COOKIE |
||
| 69 | * - env $_ENV |
||
| 70 | * - server $_SERVER |
||
| 71 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
| 72 | * - default $_REQUEST |
||
| 73 | * |
||
| 74 | * @param string $name Variable name |
||
| 75 | * @param mixed $default Default value if the variable does not exist |
||
| 76 | * @param string $hash Source of variable value (POST, GET, FILES, COOKIE, METHOD) |
||
| 77 | * @param string $type Return type for the variable (INT, FLOAT, BOOLEAN, WORD, |
||
| 78 | * ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more |
||
| 79 | * information see FilterInput::clean(). |
||
| 80 | * @param int $mask Filter mask for the variable |
||
| 81 | * |
||
| 82 | * @return mixed Requested variable |
||
| 83 | */ |
||
| 84 | 3 | public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Fetches and returns a given filtered variable. The integer |
||
| 135 | * filter will allow only digits to be returned. This is currently |
||
| 136 | * only a proxy function for getVar(). |
||
| 137 | * |
||
| 138 | * See getVar() for more in-depth documentation on the parameters. |
||
| 139 | * |
||
| 140 | * @param string $name Variable name |
||
| 141 | * @param int $default Default value if the variable does not exist |
||
| 142 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 143 | * |
||
| 144 | * @return int Requested variable |
||
| 145 | */ |
||
| 146 | 2 | public static function getInt($name, $default = 0, $hash = 'default') |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Fetches and returns a given filtered variable. The float |
||
| 153 | * filter only allows digits and periods. This is currently |
||
| 154 | * only a proxy function for getVar(). |
||
| 155 | * |
||
| 156 | * See getVar() for more in-depth documentation on the parameters. |
||
| 157 | * |
||
| 158 | * @param string $name Variable name |
||
| 159 | * @param float $default Default value if the variable does not exist |
||
| 160 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 161 | * |
||
| 162 | * @return float Requested variable |
||
| 163 | */ |
||
| 164 | 2 | public static function getFloat($name, $default = 0.0, $hash = 'default') |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Fetches and returns a given filtered variable. The bool |
||
| 171 | * filter will only return true/false bool values. This is |
||
| 172 | * currently only a proxy function for getVar(). |
||
| 173 | * |
||
| 174 | * See getVar() for more in-depth documentation on the parameters. |
||
| 175 | * |
||
| 176 | * @param string $name Variable name |
||
| 177 | * @param bool $default Default value if the variable does not exist |
||
| 178 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 179 | * |
||
| 180 | * @return bool Requested variable |
||
| 181 | */ |
||
| 182 | 2 | public static function getBool($name, $default = false, $hash = 'default') |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Fetches and returns a given filtered variable. The word |
||
| 189 | * filter only allows the characters [A-Za-z_]. This is currently |
||
| 190 | * only a proxy function for getVar(). |
||
| 191 | * |
||
| 192 | * See getVar() for more in-depth documentation on the parameters. |
||
| 193 | * |
||
| 194 | * @param string $name Variable name |
||
| 195 | * @param string $default Default value if the variable does not exist |
||
| 196 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 197 | * |
||
| 198 | * @return string Requested variable |
||
| 199 | */ |
||
| 200 | 2 | public static function getWord($name, $default = '', $hash = 'default') |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Fetches and returns a given filtered variable. The cmd filter only allows the characters |
||
| 207 | * [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar(). |
||
| 208 | * |
||
| 209 | * See getVar() for more in-depth documentation on the parameters. |
||
| 210 | * |
||
| 211 | * @param string $name Variable name |
||
| 212 | * @param string $default Default value if the variable does not exist |
||
| 213 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 214 | * |
||
| 215 | * @return string Requested variable |
||
| 216 | */ |
||
| 217 | 2 | public static function getCmd($name, $default = '', $hash = 'default') |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Fetches and returns a given filtered variable. The string |
||
| 224 | * filter deletes 'bad' HTML code, if not overridden by the mask. |
||
| 225 | * This is currently only a proxy function for getVar(). |
||
| 226 | * |
||
| 227 | * See getVar() for more in-depth documentation on the parameters. |
||
| 228 | * |
||
| 229 | * @param string $name Variable name |
||
| 230 | * @param string $default Default value if the variable does not exist |
||
| 231 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 232 | * @param int $mask Filter mask for the variable |
||
| 233 | * |
||
| 234 | * @return string Requested variable |
||
| 235 | */ |
||
| 236 | 3 | public static function getString($name, $default = '', $hash = 'default', $mask = 0) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Fetches and returns an array |
||
| 244 | * |
||
| 245 | * @param string $name Variable name |
||
| 246 | * @param mixed $default Default value if the variable does not exist |
||
| 247 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 2 | public static function getArray($name, $default = array(), $hash = 'default') |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Fetches and returns raw text |
||
| 258 | * |
||
| 259 | * @param string $name Variable name |
||
| 260 | * @param string $default Default value if the variable does not exist |
||
| 261 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 262 | * |
||
| 263 | * @return string Requested variable |
||
| 264 | */ |
||
| 265 | 2 | public static function getText($name, $default = '', $hash = 'default') |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Fetches and returns a web url |
||
| 272 | * |
||
| 273 | * @param string $name Variable name |
||
| 274 | * @param string $default Default value if the variable does not exist |
||
| 275 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 276 | * |
||
| 277 | * @return string Requested variable |
||
| 278 | */ |
||
| 279 | 1 | public static function getUrl($name, $default = '', $hash = 'default') |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Fetches and returns a file (or web) path |
||
| 286 | * |
||
| 287 | * @param string $name Variable name |
||
| 288 | * @param string $default Default value if the variable does not exist |
||
| 289 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 290 | * |
||
| 291 | * @return string Requested variable |
||
| 292 | */ |
||
| 293 | 1 | public static function getPath($name, $default = '', $hash = 'default') |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Fetches and returns an email address |
||
| 300 | * |
||
| 301 | * @param string $name Variable name |
||
| 302 | * @param string $default Default value if the variable does not exist |
||
| 303 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 304 | * |
||
| 305 | * @return string email address or default if invalid |
||
| 306 | */ |
||
| 307 | 1 | public static function getEmail($name, $default = '', $hash = 'default') |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Fetches and returns an IP address |
||
| 315 | * |
||
| 316 | * @param string $name Variable name |
||
| 317 | * @param string $default Default value if the variable does not exist |
||
| 318 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 319 | * |
||
| 320 | * @return string IP address or default if invalid |
||
| 321 | */ |
||
| 322 | 2 | public static function getIP($name, $default = '', $hash = 'default') |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Return a DateTime object from a Xoops\Form\DateSelect of Xoops\Form\DateTime field |
||
| 330 | * |
||
| 331 | * @param string $name Variable name |
||
| 332 | * @param mixed $default Default value if the variable does not exist |
||
| 333 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 334 | * |
||
| 335 | * @return \DateTime object |
||
| 336 | */ |
||
| 337 | 1 | public static function getDateTime($name, $default = null, $hash = 'default') |
|
| 338 | { |
||
| 339 | 1 | $values = self::getVar($name, [], $hash, 'array'); |
|
| 340 | 1 | $count = count($values); |
|
| 341 | 1 | if ($count === 1) { |
|
| 342 | 1 | $date = reset($values); |
|
| 343 | 1 | $ret = (empty($date)) ? $default : Time::inputToDateTime($date); |
|
| 344 | 1 | } elseif (isset($values['date']) && isset($values['time'])) { |
|
| 345 | 1 | $ret = (empty($values['date'])) ? $default : Time::inputToDateTime($values); |
|
| 346 | } else { |
||
| 347 | $ret = $default; |
||
| 348 | } |
||
| 349 | 1 | return $ret; |
|
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * get request header |
||
| 354 | * |
||
| 355 | * @param string $headerName name of header to retrieve, case insensitive |
||
| 356 | * @param string|null $default default to return if named header is not found |
||
| 357 | * |
||
| 358 | * @return string header value or default if header was not found |
||
| 359 | */ |
||
| 360 | public static function getHeader($headerName, $default = '') |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set a variable in one of the request variables |
||
| 391 | * |
||
| 392 | * @param string $name Name |
||
| 393 | * @param string $value Value |
||
| 394 | * @param string $hash Hash |
||
| 395 | * @param boolean $overwrite Boolean |
||
| 396 | * |
||
| 397 | * @return string Previous value |
||
| 398 | */ |
||
| 399 | 2 | public static function setVar($name, $value = null, $hash = 'method', $overwrite = true) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Fetches and returns a request array. |
||
| 451 | * |
||
| 452 | * The default behaviour is fetching variables depending on the |
||
| 453 | * current request method: GET and HEAD will result in returning |
||
| 454 | * $_GET, POST and PUT will result in returning $_POST. |
||
| 455 | * |
||
| 456 | * You can force the source by setting the $hash parameter: |
||
| 457 | * |
||
| 458 | * - post $_POST |
||
| 459 | * - get $_GET |
||
| 460 | * - files $_FILES |
||
| 461 | * - cookie $_COOKIE |
||
| 462 | * - env $_ENV |
||
| 463 | * - server $_SERVER |
||
| 464 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
| 465 | * - default $_REQUEST |
||
| 466 | * |
||
| 467 | * @param string $hash to get (POST, GET, FILES, METHOD) |
||
| 468 | * @param int $mask Filter mask for the variable |
||
| 469 | * |
||
| 470 | * @return mixed Request hash |
||
| 471 | */ |
||
| 472 | 2 | public static function get($hash = 'default', $mask = 0) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Sets a request variable |
||
| 511 | * |
||
| 512 | * @param array $array An associative array of key-value pairs |
||
| 513 | * @param string $hash The request variable to set (POST, GET, FILES, METHOD) |
||
| 514 | * @param boolean $overwrite If true and an existing key is found, the value is overwritten, |
||
| 515 | * otherwise it is ignored |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | 2 | public static function set($array, $hash = 'method', $overwrite = true) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Clean up an input variable. |
||
| 528 | * |
||
| 529 | * @param mixed $var The input variable. |
||
| 530 | * @param int $mask Filter bit mask. |
||
| 531 | * - 1=no trim: If this flag is cleared and the input is a string, |
||
| 532 | * the string will have leading and trailing whitespace trimmed. |
||
| 533 | * - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored. |
||
| 534 | * - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first. |
||
| 535 | * If set, no more filtering is performed. |
||
| 536 | * - If no bits other than the 1 bit is set, a strict filter is applied. |
||
| 537 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | 1 | protected static function cleanVar($var, $mask = 0, $type = null) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Clean up an array of variables. |
||
| 580 | * |
||
| 581 | * @param mixed $var The input variable. |
||
| 582 | * @param int $mask Filter bit mask. See {@link Request::cleanVar()} |
||
| 583 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | protected static function cleanVars($var, $mask = 0, $type = null) |
||
| 599 | } |
||
| 600 |
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: