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() |
|
125 | { |
||
126 | 18 | self::$instance = $this; |
|
127 | 18 | $app = App::getInstance(); |
|
128 | 18 | $this->configuration = $app->getConfiguration(); |
|
129 | |||
130 | 18 | if( !empty($this->configuration->session) ) { |
|
131 | if ($this->configuration->session !== false && $this->configuration->session->flashdataId) { |
||
132 | $this->flashdataId = $this->configuration->session->flashdataId; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // $config = \HTMLPurifier_Config::createDefault(); |
||
137 | // $this->purifier = new \HTMLPurifier($config); |
||
138 | |||
139 | 18 | if (!empty($_SESSION)) { |
|
140 | $this->sessionContainer = $this->normalize($_SESSION); |
||
141 | } |
||
142 | 18 | $this->registerFlashdata(); |
|
143 | |||
144 | 18 | $this->getContainer = $this->normalize($_GET); |
|
145 | 18 | $this->postContainer = $this->normalize($_POST); |
|
146 | 18 | $this->cookieContainer = $this->normalize($_COOKIE); |
|
147 | 18 | $this->filesContainer = $this->normalize($_FILES); |
|
148 | 18 | $this->serverContainer = $this->normalize($_SERVER); |
|
149 | 18 | if ($this->serverContainer['REQUEST_METHOD'] === 'PUT') { |
|
150 | parse_str(file_get_contents('php://input', "r"), $PUT); |
||
151 | $this->putContainer = $this->normalize($PUT); |
||
152 | 18 | } elseif ($this->serverContainer['REQUEST_METHOD'] === 'DELETE') { |
|
153 | parse_str(file_get_contents('php://input', "r"), $DELETE); |
||
154 | $this->deleteContainer = $this->normalize($DELETE); |
||
155 | } |
||
156 | 18 | } |
|
157 | |||
158 | /** |
||
159 | * Processes current requests flashdata, recycles old. |
||
160 | * |
||
161 | * @access private |
||
162 | */ |
||
163 | 18 | private function registerFlashdata() |
|
164 | { |
||
165 | 18 | if (!empty($this->sessionContainer[$this->flashdataId])) { |
|
166 | $this->flashdata = unserialize(base64_decode($this->session($this->flashdataId))); |
||
167 | // and destroy the temporary session variable |
||
168 | unset($_SESSION[$this->flashdataId]); |
||
169 | |||
170 | |||
171 | if (!empty($this->flashdata)) { |
||
172 | // iterate through all the entries |
||
173 | foreach ($this->flashdata as $variable => $data) { |
||
174 | // increment counter representing server requests |
||
175 | $this->flashdata[$variable]['inc'] ++; |
||
176 | |||
177 | // if we're past the first server request |
||
178 | if ($this->flashdata[$variable]['inc'] > 1) { |
||
179 | // unset the session variable |
||
180 | unset($_SESSION[$variable]); |
||
181 | |||
182 | // stop tracking |
||
183 | unset($this->flashdata[$variable]); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // if there is any flashdata left to be handled |
||
188 | if (!empty($this->flashdata)) { |
||
189 | // store data in a temporary session variable |
||
190 | $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata)); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | 18 | } |
|
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) |
||
229 | { |
||
230 | if ($name === false && !empty($this->flashdata)) { |
||
231 | return $this->flashdata; |
||
232 | } |
||
233 | if ($name !== false) { |
||
234 | if (!empty($this->flashdata[$name]['value'])) { |
||
235 | return $this->flashdata[$name]['value']; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | return $default; |
||
240 | } |
||
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 private |
||
313 | * @TODO: expand functionality, set/perform based on configuration |
||
314 | */ |
||
315 | 18 | private function normalize($data) |
|
366 | |||
367 | public function __call($name, $arguments) |
||
396 | } |
||
397 |
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: