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 |
||
13 | class Request extends Message implements RequestContract, ServerRequestInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var ServerRequestInterface |
||
17 | */ |
||
18 | protected $wrapped; |
||
19 | |||
20 | /** |
||
21 | * @param ServerRequestInterface $wrapper |
||
22 | */ |
||
23 | 82 | public function __construct(ServerRequestInterface $wrapper) |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 4 | public function header(string $key = null, $default = null) |
|
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 27 | public function method() : string |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 19 | public function get(string $key, $default = null) |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 26 | public function all() |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 5 | public function only($keys) |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 2 | public function except($keys) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 4 | public function exists($key) |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 7 | public function has($key) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 4 | public function server(string $key = null, $default = null) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 9 | public function segments() |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | 3 | public function segment($index, $default = null) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 3 | public function file(string $key = null, $default = null) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 1 | public function hasFile(string $key) |
|
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 1 | public function cookies() |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 2 | public function cookie(string $key = null, $default = null) |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 1 | public function getRequestTarget() |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 1 | public function withRequestTarget($requestTarget) |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 1 | public function getMethod() |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | 1 | public function withMethod($method) |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | 9 | public function getUri() |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 1 | public function withUri(UriInterface $uri, $preserveHost = false) |
|
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | 1 | public function getServerParams() |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 3 | public function getCookieParams() |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 1 | public function withCookieParams(array $cookies) |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 26 | public function getQueryParams() |
|
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 1 | public function withQueryParams(array $query) |
|
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | 3 | public function getUploadedFiles() |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | 6 | public function getParsedBody() |
|
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | 1 | public function withParsedBody($data) |
|
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 1 | public function getAttributes() |
|
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 1 | public function getAttribute($name, $default = null) |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 1 | public function withAttribute($name, $value) |
|
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | 1 | public function withoutAttribute($name) |
|
323 | |||
324 | /** |
||
325 | * @param string $method |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 26 | private function isMethod(string $method) |
|
333 | |||
334 | /** |
||
335 | * @param string $key |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | 7 | private function isEmptyString(string $key) |
|
349 | |||
350 | /** |
||
351 | * @return string |
||
352 | */ |
||
353 | 1 | public function uri() : string |
|
357 | |||
358 | /** |
||
359 | * @return string |
||
360 | */ |
||
361 | 1 | public function path() : string |
|
365 | |||
366 | /** |
||
367 | * @return ServerRequestInterface |
||
368 | */ |
||
369 | 1 | public function toPsr7() : ServerRequestInterface |
|
373 | |||
374 | /** |
||
375 | * @return Response |
||
376 | */ |
||
377 | 1 | public function prepareResponse() : Response |
|
383 | |||
384 | /** |
||
385 | * @return ServerRequestInterface |
||
386 | */ |
||
387 | 11 | protected function getWrapped() |
|
391 | } |
||
392 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: