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 | const AUTHORIZATION_TOKEN = 'Authorization-Token'; |
||
| 24 | |||
| 25 | protected $get = array(); |
||
| 26 | protected $post = array(); |
||
| 27 | protected $server = array(); |
||
| 28 | protected $files = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * mapped to HTTP values |
||
| 32 | */ |
||
| 33 | 27 | public function __construct() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Get a value in the request depending HTTP method |
||
| 43 | * @param string $name Name to retrieve |
||
| 44 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | 1 | public function get($name, $defaultValue = null) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Check if param exists |
||
| 56 | * @param string $name |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 1 | public function has($name) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Return requested value in GET method |
||
| 68 | * @param string $name Name to retrieve |
||
| 69 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | 2 | public function getGet($name, $defaultValue = null) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Return requested value in POST method |
||
| 79 | * @param string $name Name to retrieve |
||
| 80 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | 2 | public function getPost($name, $defaultValue = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Retrieve a value defined on server |
||
| 90 | * @param string $name Name to retrieve |
||
| 91 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | 7 | public function getServer($name, $defaultValue = null) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Retrieve a value defined on files |
||
| 101 | * @param int $name |
||
| 102 | * @param mixed $defaultValue Value returned if name is not defined in query |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 1 | public function getFiles($name, $defaultValue = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Check whether request is an Ajax request |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 1 | public function isAjax() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Get HTTP method (GET/POST/DELETE...) |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | 3 | public function getMethod() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Retrieve called URI |
||
| 131 | * @param bool $withQuerySting must return query string in the request |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 1 | public function getRequestUri($withQuerySting = false) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Define a POST value |
||
| 146 | * @param string $name Name to define |
||
| 147 | * @param mixed $value Value to define |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | 1 | View Code Duplication | public function setPostValue($name, $value) |
| 158 | |||
| 159 | /** |
||
| 160 | * Define a GET value |
||
| 161 | * @param string $name Name to define |
||
| 162 | * @param mixed $value Value to define |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | 1 | View Code Duplication | public function setGetValue($name, $value) |
| 173 | |||
| 174 | /** |
||
| 175 | * Check whether selected post exists |
||
| 176 | * @param string $name |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | 3 | public function hasPost($name) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Check whether selected get exists |
||
| 186 | * @param string $name |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | 3 | public function hasGet($name) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Check whether selected server value exists |
||
| 196 | * @param string $name |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 7 | public function hasServer($name) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Check whether selected files value exists |
||
| 206 | * @param string $name |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | 1 | public function hasFiles($name) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return request referer |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 1 | public function getReferer() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Return request headers |
||
| 225 | * @codeCoverageIgnore |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getHeaders() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return requested key in headers HTTP |
||
| 235 | * @param string $key |
||
| 236 | * @param mixed $default |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | 1 | public function getHeader($key, $default = null) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Check whether selected key exists |
||
| 246 | * @param string $key |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 2 | public function hasHeader($key) |
|
| 253 | } |
||
| 254 |
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: