Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class Http extends Request |
||
| 7 | { |
||
| 8 | const HTTP_X_REQUESTED_WITH = 'HTTP_X_REQUESTED_WITH'; |
||
| 9 | const HTTP_X_REQUESTED_WITH_AJAX = 'XMLHttpRequest'; |
||
| 10 | |||
| 11 | const REQUEST_URI = 'REQUEST_URI'; |
||
| 12 | const REQUEST_SCHEME = 'REQUEST_SCHEME'; |
||
| 13 | const QUERY_STRING = 'QUERY_STRING'; |
||
| 14 | |||
| 15 | const REQUEST_METHOD = 'REQUEST_METHOD'; |
||
| 16 | const REQUEST_METHOD_GET = 'GET'; |
||
| 17 | const REQUEST_METHOD_POST = 'POST'; |
||
| 18 | |||
| 19 | const SERVER_NAME = 'SERVER_NAME'; |
||
| 20 | const HTTP_REFERER = 'HTTP_REFERER'; |
||
| 21 | const HTTP_HOST = 'HTTP_HOST'; |
||
| 22 | |||
| 23 | protected $get = array(); |
||
| 24 | protected $post = array(); |
||
| 25 | protected $server = array(); |
||
| 26 | protected $files = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * mapped to HTTP values |
||
| 30 | */ |
||
| 31 | 25 | public function __construct() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Get a value in the request depending HTTP method |
||
| 41 | * @param string $name Name to retrieve |
||
| 42 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | 1 | public function get($name, $defaultValue = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Check if param exists |
||
| 54 | * @param string $name |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | 1 | public function has($name) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Return requested value in GET method |
||
| 66 | * @param string $name Name to retrieve |
||
| 67 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 68 | * @return mixed |
||
| 69 | */ |
||
| 70 | 2 | public function getGet($name, $defaultValue = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Return requested value in POST method |
||
| 77 | * @param string $name Name to retrieve |
||
| 78 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | 2 | public function getPost($name, $defaultValue = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Retrieve a value defined on server |
||
| 88 | * @param string $name Name to retrieve |
||
| 89 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | 7 | public function getServer($name, $defaultValue = null) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieve a value defined on files |
||
| 99 | * @param int $name |
||
| 100 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | 1 | public function getFiles($name, $defaultValue = null) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Check whether request is an Ajax request |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 1 | public function isAjax() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get HTTP method (GET/POST/DELETE...) |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 3 | public function getMethod() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Retrieve called URI |
||
| 129 | * @param bool $withQuerySting must return query string in the request |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 1 | public function getRequestUri($withQuerySting = false) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Define a POST value |
||
| 144 | * @param string $name Name to define |
||
| 145 | * @param mixed $value Value to define |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | 1 | View Code Duplication | public function setPostValue($name, $value) |
| 156 | |||
| 157 | /** |
||
| 158 | * Define a GET value |
||
| 159 | * @param string $name Name to define |
||
| 160 | * @param mixed $value Value to define |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | 1 | View Code Duplication | public function setGetValue($name, $value) |
| 171 | |||
| 172 | /** |
||
| 173 | * Check whether selected post exists |
||
| 174 | * @param string $name |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | 3 | public function hasPost($name) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Check whether selected get exists |
||
| 184 | * @param string $name |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 3 | public function hasGet($name) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Check whether selected server value exists |
||
| 194 | * @param string $name |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | 7 | public function hasServer($name) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Check whether selected files value exists |
||
| 204 | * @param string $name |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | 1 | public function hasFiles($name) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Return request referer |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 1 | public function getReferer() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Return request headers |
||
| 223 | * @codeCoverageIgnore |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getHeaders() |
||
| 230 | } |
||
| 231 |
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: