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 | * Reference to instantiated controller object. |
||
| 13 | * |
||
| 14 | * @var object |
||
| 15 | */ |
||
| 16 | protected static $instance; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * System configuration |
||
| 20 | * |
||
| 21 | * @var object |
||
| 22 | */ |
||
| 23 | private $configuration; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * normalized $_GET superglobal |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | * @access private |
||
| 31 | */ |
||
| 32 | |||
| 33 | private $getContainer = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * normalized $_POST superglobal |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | * @access private |
||
| 40 | */ |
||
| 41 | |||
| 42 | private $postContainer = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * normalized $_DELETE superglobal |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | * @access private |
||
| 49 | */ |
||
| 50 | |||
| 51 | private $deleteContainer = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * normalized $_PUT superglobal |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | |||
| 60 | private $putContainer = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * normalized $_SESSION superglobal |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | * @access private |
||
| 67 | */ |
||
| 68 | |||
| 69 | private $sessionContainer = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * normalized $_COOKIE superglobal |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | * @access private |
||
| 76 | */ |
||
| 77 | |||
| 78 | private $cookieContainer = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * normalized $_FILES superglobal |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | * @access private |
||
| 85 | */ |
||
| 86 | |||
| 87 | private $filesContainer = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * normalized $_SERVER superglobal |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | * @access private |
||
| 94 | */ |
||
| 95 | |||
| 96 | private $serverContainer = []; |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Flashdata container |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | * @access private |
||
| 104 | */ |
||
| 105 | |||
| 106 | private $flashdata = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Flashdata identifier |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | * @access private |
||
| 113 | * @TODO: move flashdata to sessionhandler, make available here with other request vars still |
||
| 114 | */ |
||
| 115 | |||
| 116 | |||
| 117 | private $flashdataId = '_z_session_flashdata'; |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Normalizes superglobals, handles flashdata |
||
| 122 | */ |
||
| 123 | |||
| 124 | 18 | public function __construct() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Processes current requests flashdata, recycles old. |
||
| 160 | * |
||
| 161 | * @access private |
||
| 162 | */ |
||
| 163 | 18 | private function registerFlashdata() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Sets flashdata |
||
| 198 | * |
||
| 199 | * @access public |
||
| 200 | * |
||
| 201 | * @params string $name |
||
| 202 | * @params mixed $value |
||
| 203 | */ |
||
| 204 | |||
| 205 | public function setFlashdata($name, $value) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Gets flashdata |
||
| 222 | * |
||
| 223 | * @access public |
||
| 224 | * |
||
| 225 | * @params string $name |
||
| 226 | */ |
||
| 227 | |||
| 228 | public function getFlashdata($name = false, $default = false) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Remove session data |
||
| 244 | * |
||
| 245 | * @access public |
||
| 246 | * |
||
| 247 | * @params string $index |
||
| 248 | */ |
||
| 249 | public function removeSession($index) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set session data |
||
| 258 | * |
||
| 259 | * @access public |
||
| 260 | * |
||
| 261 | * @params string $index |
||
| 262 | * @params mixed $value |
||
| 263 | */ |
||
| 264 | |||
| 265 | public function setSession($index = false, $value = false) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Dumps all session data |
||
| 285 | * |
||
| 286 | * @access public |
||
| 287 | */ |
||
| 288 | public function destroySession() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Normalizes data |
||
| 311 | * |
||
| 312 | * @access public |
||
| 313 | * @TODO: expand functionality, set/perform based on configuration |
||
| 314 | */ |
||
| 315 | 18 | public function normalize($data) |
|
| 362 | |||
| 363 | public function __call($name, $arguments) |
||
| 392 | } |
||
| 393 |
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: