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:
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 |
||
| 9 | class Request |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * normalized $_GET superglobal |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | * @access private |
||
| 16 | */ |
||
| 17 | |||
| 18 | private $getContainer = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * normalized $_POST superglobal |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | * @access private |
||
| 25 | */ |
||
| 26 | |||
| 27 | private $postContainer = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * normalized $_DELETE superglobal |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | * @access private |
||
| 34 | */ |
||
| 35 | |||
| 36 | private $deleteContainer = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * normalized $_PUT superglobal |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | * @access private |
||
| 43 | */ |
||
| 44 | |||
| 45 | private $putContainer = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * normalized $_SESSION superglobal |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | * @access private |
||
| 52 | */ |
||
| 53 | |||
| 54 | private $sessionContainer = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * normalized $_COOKIE superglobal |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | * @access private |
||
| 61 | */ |
||
| 62 | |||
| 63 | private $cookieContainer = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * normalized $_FILES superglobal |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | * @access private |
||
| 70 | */ |
||
| 71 | |||
| 72 | private $filesContainer = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * normalized $_SERVER superglobal |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | * @access private |
||
| 79 | */ |
||
| 80 | |||
| 81 | private $serverContainer = []; |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Flashdata container |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | * @access private |
||
| 89 | */ |
||
| 90 | |||
| 91 | private $flashdata = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Flashdata identifier |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | * @access private |
||
| 98 | * @TODO: move flashdata to sessionhandler, make available here with other request vars still |
||
| 99 | */ |
||
| 100 | |||
| 101 | |||
| 102 | private $flashdataId = '_z_session_flashdata'; |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Normalizes superglobals, handles flashdata |
||
| 107 | * |
||
| 108 | * @param $config |
||
| 109 | * @todo: pass PSR7 HTTP messages to constructor. |
||
| 110 | */ |
||
| 111 | public function __construct(Config $config) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Processes current requests flashdata, recycles old. |
||
| 142 | * |
||
| 143 | * @access private |
||
| 144 | */ |
||
| 145 | private function registerFlashdata() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets flashdata |
||
| 180 | * |
||
| 181 | * @access public |
||
| 182 | * |
||
| 183 | * @params string $name |
||
| 184 | * @params mixed $value |
||
| 185 | */ |
||
| 186 | |||
| 187 | public function setFlashdata($name, $value) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets flashdata |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * |
||
| 207 | * @params string $name |
||
| 208 | */ |
||
| 209 | |||
| 210 | public function getFlashdata($name = false, $default = false) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Remove session data |
||
| 226 | * |
||
| 227 | * @access public |
||
| 228 | * |
||
| 229 | * @params string $index |
||
| 230 | */ |
||
| 231 | public function removeSession($index) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set session data |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * |
||
| 243 | * @params string $index |
||
| 244 | * @params mixed $value |
||
| 245 | */ |
||
| 246 | |||
| 247 | public function setSession($index = false, $value = false) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Dumps all session data |
||
| 267 | * |
||
| 268 | * @access public |
||
| 269 | */ |
||
| 270 | public function destroySession() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Normalizes data |
||
| 293 | * |
||
| 294 | * @access public |
||
| 295 | * @TODO: expand functionality, set/perform based on configuration |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function normalize($data) |
|
| 338 | |||
| 339 | public function __call($name, $arguments) |
||
| 368 | } |
||
| 369 |
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: