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 | 18 | public function __construct(Config $config) |
|
112 | { |
||
113 | 18 | $sessionConfig = $config->get('session'); |
|
114 | |||
115 | 18 | if (!empty($sessionConfig) && !empty($sessionConfig->flashdataId)) { |
|
116 | $this->flashdataId = $sessionConfig->flashdataId; |
||
117 | } |
||
118 | |||
119 | 18 | if (!empty($_SESSION)) { |
|
120 | $this->sessionContainer = $this->normalize($_SESSION); |
||
121 | } |
||
122 | //echo "<PRE>"; |
||
123 | // print_r($_SESSION);die(); |
||
124 | 18 | $this->registerFlashdata(); |
|
125 | |||
126 | 18 | $this->getContainer = $this->normalize($_GET); |
|
127 | 18 | $this->postContainer = $this->normalize($_POST); |
|
128 | 18 | $this->cookieContainer = $this->normalize($_COOKIE); |
|
129 | 18 | $this->filesContainer = $this->normalize($_FILES); |
|
130 | 18 | $this->serverContainer = $this->normalize($_SERVER); |
|
131 | 18 | if ($this->serverContainer['REQUEST_METHOD'] === 'PUT') { |
|
132 | parse_str(file_get_contents('php://input', "r"), $PUT); |
||
133 | $this->putContainer = $this->normalize($PUT); |
||
134 | 18 | } elseif ($this->serverContainer['REQUEST_METHOD'] === 'DELETE') { |
|
135 | parse_str(file_get_contents('php://input', "r"), $DELETE); |
||
136 | $this->deleteContainer = $this->normalize($DELETE); |
||
137 | } |
||
138 | 18 | } |
|
139 | |||
140 | /** |
||
141 | * Processes current requests flashdata, recycles old. |
||
142 | * |
||
143 | * @access private |
||
144 | */ |
||
145 | 18 | private function registerFlashdata() |
|
146 | { |
||
147 | 18 | if (!empty($this->sessionContainer[$this->flashdataId])) { |
|
148 | $this->flashdata = unserialize(base64_decode($this->session($this->flashdataId))); |
||
149 | // and destroy the temporary session variable |
||
150 | unset($_SESSION[$this->flashdataId]); |
||
151 | |||
152 | |||
153 | if (!empty($this->flashdata)) { |
||
154 | // iterate through all the entries |
||
155 | foreach ($this->flashdata as $variable => $data) { |
||
156 | // increment counter representing server requests |
||
157 | $this->flashdata[$variable]['inc'] ++; |
||
158 | |||
159 | // if we're past the first server request |
||
160 | if ($this->flashdata[$variable]['inc'] > 1) { |
||
161 | // unset the session variable |
||
162 | unset($_SESSION[$variable]); |
||
163 | |||
164 | // stop tracking |
||
165 | unset($this->flashdata[$variable]); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | // if there is any flashdata left to be handled |
||
170 | if (!empty($this->flashdata)) { |
||
171 | // store data in a temporary session variable |
||
172 | $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata)); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | 18 | } |
|
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) |
||
188 | { |
||
189 | |||
190 | // set session variable |
||
191 | $this->sessionContainer[$name] = $value; |
||
192 | |||
193 | // initialize the counter for this flashdata |
||
194 | $this->flashdata[$name] = [ |
||
195 | 'value' => $value, |
||
196 | 'inc' => 0 |
||
197 | ]; |
||
198 | |||
199 | $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata)); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Gets flashdata |
||
204 | * |
||
205 | * @access public |
||
206 | * |
||
207 | * @params string $name |
||
208 | */ |
||
209 | |||
210 | public function getFlashdata($name = false, $default = false) |
||
211 | { |
||
212 | if ($name === false && !empty($this->flashdata)) { |
||
213 | return $this->flashdata; |
||
214 | } |
||
215 | if ($name !== false) { |
||
216 | if (!empty($this->flashdata[$name]['value'])) { |
||
217 | return $this->flashdata[$name]['value']; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | return $default; |
||
222 | } |
||
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 | 18 | public function normalize($data) |
|
344 | |||
345 | public function __call($name, $arguments) |
||
374 | } |
||
375 |
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: