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 |
||
| 32 | class Request |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Available masks for cleaning variables |
||
| 36 | */ |
||
| 37 | const MASK_NO_TRIM = 1; |
||
| 38 | const MASK_ALLOW_RAW = 2; |
||
| 39 | const MASK_ALLOW_HTML = 4; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Gets the request method |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | 2 | public static function getMethod() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Fetches and returns a given variable. |
||
| 55 | * |
||
| 56 | * The default behaviour is fetching variables depending on the |
||
| 57 | * current request method: GET and HEAD will result in returning |
||
| 58 | * an entry from $_GET, POST and PUT will result in returning an |
||
| 59 | * entry from $_POST. |
||
| 60 | * |
||
| 61 | * You can force the source by setting the $hash parameter: |
||
| 62 | * |
||
| 63 | * - post $_POST |
||
| 64 | * - get $_GET |
||
| 65 | * - files $_FILES |
||
| 66 | * - cookie $_COOKIE |
||
| 67 | * - env $_ENV |
||
| 68 | * - server $_SERVER |
||
| 69 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
| 70 | * - default $_REQUEST |
||
| 71 | * |
||
| 72 | * @param string $name Variable name |
||
| 73 | * @param mixed $default Default value if the variable does not exist |
||
| 74 | * @param string $hash Source of variable value (POST, GET, FILES, COOKIE, METHOD) |
||
| 75 | * @param string $type Return type for the variable (INT, FLOAT, BOOLEAN, WORD, |
||
| 76 | * ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more |
||
| 77 | * information see FilterInput::clean(). |
||
| 78 | * @param int $mask Filter mask for the variable |
||
| 79 | * |
||
| 80 | * @return mixed Requested variable |
||
| 81 | */ |
||
| 82 | 3 | public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0) |
|
| 83 | { |
||
| 84 | // Ensure hash and type are uppercase |
||
| 85 | 3 | $hash = strtoupper($hash); |
|
| 86 | 3 | if ($hash === 'METHOD') { |
|
| 87 | $hash = static::getMethod(); |
||
| 88 | } |
||
| 89 | 3 | $type = strtoupper($type); |
|
| 90 | |||
| 91 | // Get the input hash |
||
| 92 | 3 | switch ($hash) { |
|
| 93 | 3 | case 'GET': |
|
| 94 | $input = &$_GET; |
||
| 95 | break; |
||
| 96 | 3 | case 'POST': |
|
| 97 | 1 | $input = &$_POST; |
|
| 98 | 1 | break; |
|
| 99 | 2 | case 'FILES': |
|
| 100 | $input = &$_FILES; |
||
| 101 | break; |
||
| 102 | 2 | case 'COOKIE': |
|
| 103 | $input = &$_COOKIE; |
||
| 104 | break; |
||
| 105 | 2 | case 'ENV': |
|
| 106 | $input = &$_ENV; |
||
| 107 | break; |
||
| 108 | 2 | case 'SERVER': |
|
| 109 | $input = &$_SERVER; |
||
| 110 | break; |
||
| 111 | default: |
||
| 112 | 2 | $input = &$_REQUEST; |
|
| 113 | 2 | break; |
|
| 114 | } |
||
| 115 | |||
| 116 | 3 | if (isset($input[$name]) && $input[$name] !== null) { |
|
| 117 | // Get the variable from the input hash and clean it |
||
| 118 | 3 | $var = static::cleanVar($input[$name], $mask, $type); |
|
| 119 | } else { |
||
| 120 | 3 | if ($default !== null) { |
|
| 121 | // Clean the default value |
||
| 122 | 1 | $var = static::cleanVar($default, $mask, $type); |
|
| 123 | } else { |
||
| 124 | 2 | $var = $default; |
|
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | 3 | return $var; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Fetches and returns a given filtered variable. The integer |
||
| 133 | * filter will allow only digits to be returned. This is currently |
||
| 134 | * only a proxy function for getVar(). |
||
| 135 | * |
||
| 136 | * See getVar() for more in-depth documentation on the parameters. |
||
| 137 | * |
||
| 138 | * @param string $name Variable name |
||
| 139 | * @param int $default Default value if the variable does not exist |
||
| 140 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 141 | * |
||
| 142 | * @return int Requested variable |
||
| 143 | */ |
||
| 144 | 2 | public static function getInt($name, $default = 0, $hash = 'default') |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Fetches and returns a given filtered variable. The float |
||
| 151 | * filter only allows digits and periods. This is currently |
||
| 152 | * only a proxy function for getVar(). |
||
| 153 | * |
||
| 154 | * See getVar() for more in-depth documentation on the parameters. |
||
| 155 | * |
||
| 156 | * @param string $name Variable name |
||
| 157 | * @param float $default Default value if the variable does not exist |
||
| 158 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 159 | * |
||
| 160 | * @return float Requested variable |
||
| 161 | */ |
||
| 162 | 2 | public static function getFloat($name, $default = 0.0, $hash = 'default') |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Fetches and returns a given filtered variable. The bool |
||
| 169 | * filter will only return true/false bool values. This is |
||
| 170 | * currently only a proxy function for getVar(). |
||
| 171 | * |
||
| 172 | * See getVar() for more in-depth documentation on the parameters. |
||
| 173 | * |
||
| 174 | * @param string $name Variable name |
||
| 175 | * @param bool $default Default value if the variable does not exist |
||
| 176 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 177 | * |
||
| 178 | * @return bool Requested variable |
||
| 179 | */ |
||
| 180 | 2 | public static function getBool($name, $default = false, $hash = 'default') |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Fetches and returns a given filtered variable. The word |
||
| 187 | * filter only allows the characters [A-Za-z_]. This is currently |
||
| 188 | * only a proxy function for getVar(). |
||
| 189 | * |
||
| 190 | * See getVar() for more in-depth documentation on the parameters. |
||
| 191 | * |
||
| 192 | * @param string $name Variable name |
||
| 193 | * @param string $default Default value if the variable does not exist |
||
| 194 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 195 | * |
||
| 196 | * @return string Requested variable |
||
| 197 | */ |
||
| 198 | 2 | public static function getWord($name, $default = '', $hash = 'default') |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Fetches and returns a given filtered variable. The cmd filter only allows the characters |
||
| 205 | * [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar(). |
||
| 206 | * |
||
| 207 | * See getVar() for more in-depth documentation on the parameters. |
||
| 208 | * |
||
| 209 | * @param string $name Variable name |
||
| 210 | * @param string $default Default value if the variable does not exist |
||
| 211 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 212 | * |
||
| 213 | * @return string Requested variable |
||
| 214 | */ |
||
| 215 | 2 | public static function getCmd($name, $default = '', $hash = 'default') |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Fetches and returns a given filtered variable. The string |
||
| 222 | * filter deletes 'bad' HTML code, if not overridden by the mask. |
||
| 223 | * This is currently only a proxy function for getVar(). |
||
| 224 | * |
||
| 225 | * See getVar() for more in-depth documentation on the parameters. |
||
| 226 | * |
||
| 227 | * @param string $name Variable name |
||
| 228 | * @param string $default Default value if the variable does not exist |
||
| 229 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 230 | * @param int $mask Filter mask for the variable |
||
| 231 | * |
||
| 232 | * @return string Requested variable |
||
| 233 | */ |
||
| 234 | 6 | public static function getString($name, $default = '', $hash = 'default', $mask = 0) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Fetches and returns an array |
||
| 242 | * |
||
| 243 | * @param string $name Variable name |
||
| 244 | * @param mixed $default Default value if the variable does not exist |
||
| 245 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 2 | public static function getArray($name, $default = array(), $hash = 'default') |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Fetches and returns raw text |
||
| 256 | * |
||
| 257 | * @param string $name Variable name |
||
| 258 | * @param string $default Default value if the variable does not exist |
||
| 259 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 260 | * |
||
| 261 | * @return string Requested variable |
||
| 262 | */ |
||
| 263 | 2 | public static function getText($name, $default = '', $hash = 'default') |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Fetches and returns a web url |
||
| 270 | * |
||
| 271 | * @param string $name Variable name |
||
| 272 | * @param string $default Default value if the variable does not exist |
||
| 273 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 274 | * |
||
| 275 | * @return string Requested variable |
||
| 276 | */ |
||
| 277 | 1 | public static function getUrl($name, $default = '', $hash = 'default') |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Fetches and returns a file (or web) path |
||
| 284 | * |
||
| 285 | * @param string $name Variable name |
||
| 286 | * @param string $default Default value if the variable does not exist |
||
| 287 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 288 | * |
||
| 289 | * @return string Requested variable |
||
| 290 | */ |
||
| 291 | 1 | public static function getPath($name, $default = '', $hash = 'default') |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Fetches and returns an email address |
||
| 298 | * |
||
| 299 | * @param string $name Variable name |
||
| 300 | * @param string $default Default value if the variable does not exist |
||
| 301 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 302 | * |
||
| 303 | * @return string email address or default if invalid |
||
| 304 | */ |
||
| 305 | 1 | public static function getEmail($name, $default = '', $hash = 'default') |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Fetches and returns an IP address |
||
| 313 | * |
||
| 314 | * @param string $name Variable name |
||
| 315 | * @param string $default Default value if the variable does not exist |
||
| 316 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 317 | * |
||
| 318 | * @return string IP address or default if invalid |
||
| 319 | */ |
||
| 320 | 2 | public static function getIP($name, $default = '', $hash = 'default') |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Return a DateTime object from a Xoops\Form\DateSelect or Xoops\Form\DateTimeSelect field |
||
| 328 | * |
||
| 329 | * @param string $name Variable name |
||
| 330 | * @param mixed $default Default value if the variable does not exist |
||
| 331 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
| 332 | * |
||
| 333 | * @return \DateTime object |
||
| 334 | */ |
||
| 335 | 1 | public static function getDateTime($name, $default = null, $hash = 'default') |
|
| 349 | |||
| 350 | /** |
||
| 351 | * get request header |
||
| 352 | * |
||
| 353 | * @param string $headerName name of header to retrieve, case insensitive |
||
| 354 | * @param string|null $default default to return if named header is not found |
||
| 355 | * |
||
| 356 | * @return string header value or default if header was not found |
||
| 357 | */ |
||
| 358 | public static function getHeader($headerName, $default = '') |
||
| 386 | |||
| 387 | /** |
||
| 388 | * See if a variable exists in one of the request hashes |
||
| 389 | * |
||
| 390 | * @param string $name variable to look for |
||
| 391 | * @param string $hash hash to check |
||
| 392 | * |
||
| 393 | * @return boolean True if hash has an element 'name', otherwise false |
||
| 394 | */ |
||
| 395 | 1 | public static function hasVar($name, $hash = 'method') |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Set a variable in one of the request variables |
||
| 412 | * |
||
| 413 | * @param string $name Name |
||
| 414 | * @param string $value Value |
||
| 415 | * @param string $hash Hash |
||
| 416 | * @param boolean $overwrite Boolean |
||
| 417 | * |
||
| 418 | * @return string Previous value |
||
| 419 | */ |
||
| 420 | 2 | public static function setVar($name, $value = null, $hash = 'method', $overwrite = true) |
|
| 421 | { |
||
| 422 | 2 | $hash = strtoupper($hash); |
|
| 423 | 2 | if ($hash === 'METHOD') { |
|
| 424 | $hash = strtoupper($_SERVER['REQUEST_METHOD']); |
||
| 425 | } |
||
| 426 | |||
| 427 | // Get the requested hash and determine existing value |
||
| 428 | 2 | $original = static::get($hash, static::MASK_ALLOW_RAW); |
|
| 429 | 2 | if (isset($original[$name])) { |
|
| 430 | 1 | $previous = $original[$name]; |
|
| 431 | // don't overwrite value unless asked |
||
| 432 | 1 | if (!$overwrite) { |
|
| 433 | 1 | return $previous; |
|
| 434 | } |
||
| 435 | } else { |
||
| 436 | 1 | $previous = null; |
|
| 437 | } |
||
| 438 | |||
| 439 | // set the value |
||
| 440 | 2 | switch ($hash) { |
|
| 441 | 2 | case 'GET': |
|
| 442 | 2 | $_GET[$name] = $value; |
|
| 443 | 2 | $_REQUEST[$name] = $value; |
|
| 444 | 2 | break; |
|
| 445 | case 'POST': |
||
| 446 | $_POST[$name] = $value; |
||
| 447 | $_REQUEST[$name] = $value; |
||
| 448 | break; |
||
| 449 | case 'REQUEST': |
||
| 450 | $_REQUEST[$name] = $value; |
||
| 451 | break; |
||
| 452 | case 'COOKIE': |
||
| 453 | $_COOKIE[$name] = $value; |
||
| 454 | $_REQUEST[$name] = $value; |
||
| 455 | break; |
||
| 456 | case 'FILES': |
||
| 457 | $_FILES[$name] = $value; |
||
| 458 | break; |
||
| 459 | case 'ENV': |
||
| 460 | $_ENV['name'] = $value; |
||
| 461 | break; |
||
| 462 | case 'SERVER': |
||
| 463 | $_SERVER['name'] = $value; |
||
| 464 | break; |
||
| 465 | } |
||
| 466 | |||
| 467 | 2 | return $previous; |
|
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Fetches and returns a request array. |
||
| 472 | * |
||
| 473 | * The default behaviour is fetching variables depending on the |
||
| 474 | * current request method: GET and HEAD will result in returning |
||
| 475 | * $_GET, POST and PUT will result in returning $_POST. |
||
| 476 | * |
||
| 477 | * You can force the source by setting the $hash parameter: |
||
| 478 | * |
||
| 479 | * - post $_POST |
||
| 480 | * - get $_GET |
||
| 481 | * - files $_FILES |
||
| 482 | * - cookie $_COOKIE |
||
| 483 | * - env $_ENV |
||
| 484 | * - server $_SERVER |
||
| 485 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
| 486 | * - default $_REQUEST |
||
| 487 | * |
||
| 488 | * @param string $hash to get (POST, GET, FILES, METHOD) |
||
| 489 | * @param int $mask Filter mask for the variable |
||
| 490 | * |
||
| 491 | * @return mixed Request hash |
||
| 492 | */ |
||
| 493 | 2 | public static function get($hash = 'default', $mask = 0) |
|
| 494 | { |
||
| 495 | 2 | $hash = strtoupper($hash); |
|
| 496 | |||
| 497 | 2 | if ($hash === 'METHOD') { |
|
| 498 | $hash = strtoupper($_SERVER['REQUEST_METHOD']); |
||
| 499 | } |
||
| 500 | |||
| 501 | 2 | switch ($hash) { |
|
| 502 | 2 | case 'GET': |
|
| 503 | $input = $_GET; |
||
| 504 | break; |
||
| 505 | 2 | case 'POST': |
|
| 506 | $input = $_POST; |
||
| 507 | break; |
||
| 508 | 2 | case 'FILES': |
|
| 509 | $input = $_FILES; |
||
| 510 | break; |
||
| 511 | 2 | case 'COOKIE': |
|
| 512 | $input = $_COOKIE; |
||
| 513 | break; |
||
| 514 | 2 | case 'ENV': |
|
| 515 | $input = &$_ENV; |
||
| 516 | break; |
||
| 517 | 2 | case 'SERVER': |
|
| 518 | $input = &$_SERVER; |
||
| 519 | break; |
||
| 520 | default: |
||
| 521 | 2 | $input = $_REQUEST; |
|
| 522 | 2 | break; |
|
| 523 | } |
||
| 524 | |||
| 525 | 2 | $result = static::cleanVars($input, $mask); |
|
| 526 | |||
| 527 | 2 | return $result; |
|
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Sets a request variable |
||
| 532 | * |
||
| 533 | * @param array $array An associative array of key-value pairs |
||
| 534 | * @param string $hash The request variable to set (POST, GET, FILES, METHOD) |
||
| 535 | * @param boolean $overwrite If true and an existing key is found, the value is overwritten, |
||
| 536 | * otherwise it is ignored |
||
| 537 | * |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | 2 | public static function set($array, $hash = 'method', $overwrite = true) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Clean up an input variable. |
||
| 549 | * |
||
| 550 | * @param mixed $var The input variable. |
||
| 551 | * @param int $mask Filter bit mask. |
||
| 552 | * - 1=no trim: If this flag is cleared and the input is a string, |
||
| 553 | * the string will have leading and trailing whitespace trimmed. |
||
| 554 | * - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored. |
||
| 555 | * - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first. |
||
| 556 | * If set, no more filtering is performed. |
||
| 557 | * - If no bits other than the 1 bit is set, a strict filter is applied. |
||
| 558 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | 1 | protected static function cleanVar($var, $mask = 0, $type = null) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Clean up an array of variables. |
||
| 601 | * |
||
| 602 | * @param mixed $var The input variable. |
||
| 603 | * @param int $mask Filter bit mask. See {@link Request::cleanVar()} |
||
| 604 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | protected static function cleanVars($var, $mask = 0, $type = null) |
||
| 620 | } |
||
| 621 |
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: