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 |
||
17 | class Request extends FoundationRequest |
||
18 | { |
||
19 | protected $language; |
||
20 | protected $languageInPath = false; |
||
21 | |||
22 | // special variable for route aliasing |
||
23 | protected $aliasPathTarget = false; |
||
24 | // special variable for route callback binding |
||
25 | protected $callbackClass = false; |
||
26 | |||
27 | // fast access for controller building |
||
28 | protected $controller; |
||
29 | protected $action; |
||
30 | protected $argumentId; |
||
31 | protected $argumentAdd; |
||
32 | |||
33 | public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
||
40 | |||
41 | /** |
||
42 | * Sets the parameters for this request. |
||
43 | * |
||
44 | * This method also re-initializes all properties. |
||
45 | * |
||
46 | * @param array $query The GET parameters |
||
47 | * @param array $request The POST parameters |
||
48 | * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...) |
||
49 | * @param array $cookies The COOKIE parameters |
||
50 | * @param array $files The FILES parameters |
||
51 | * @param array $server The SERVER parameters |
||
52 | * @param string $content The raw body data |
||
53 | * |
||
54 | * @api |
||
55 | */ |
||
56 | public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) |
||
72 | |||
73 | /** |
||
74 | * Build multi language pathway binding. |
||
75 | */ |
||
76 | private function runMultiLanguage() |
||
130 | |||
131 | /** |
||
132 | * Build static and dynamic path aliases for working set |
||
133 | */ |
||
134 | private function runPathBinding() |
||
163 | |||
164 | /** |
||
165 | * Prepare static pathway aliasing for routing |
||
166 | * @param array|null $map |
||
167 | * @param string|null $pathway |
||
168 | * @return string |
||
169 | */ |
||
170 | private function findStaticAliases(array $map = null, $pathway = null) |
||
200 | |||
201 | /** |
||
202 | * Prepare dynamic callback data for routing |
||
203 | * @param array|null $map |
||
204 | * @param string|null $controller |
||
205 | */ |
||
206 | private function findDynamicCallbacks(array $map = null, $controller = null) |
||
220 | |||
221 | /** |
||
222 | * Set trusted proxies from configs |
||
223 | */ |
||
224 | private function loadTrustedProxies() |
||
238 | |||
239 | /** |
||
240 | * Working with path array data |
||
241 | * @param array|null $pathArray |
||
242 | */ |
||
243 | private function setPathdata(array $pathArray = null) |
||
264 | |||
265 | /** |
||
266 | * Get pathway as string |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getPathInfo() |
||
277 | |||
278 | public function languageInPath() |
||
282 | |||
283 | /** |
||
284 | * Get current language |
||
285 | * @return string|null |
||
286 | */ |
||
287 | public function getLanguage() |
||
291 | |||
292 | /** |
||
293 | * Get current controller name |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getController() |
||
300 | |||
301 | /** |
||
302 | * Get current controller action() name |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getAction() |
||
309 | |||
310 | /** |
||
311 | * Get current $id argument for controller action |
||
312 | * @return string|null |
||
313 | */ |
||
314 | public function getID() |
||
318 | |||
319 | /** |
||
320 | * Get current $add argument for controller action |
||
321 | * @return string|null |
||
322 | */ |
||
323 | public function getAdd() |
||
327 | |||
328 | /** |
||
329 | * Get callback class alias if exist |
||
330 | * @return bool|string |
||
331 | */ |
||
332 | public function getCallbackAlias() |
||
336 | |||
337 | /** |
||
338 | * Get static alias of pathway if exist |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function getStaticAlias() |
||
345 | |||
346 | /** |
||
347 | * Check if current request is aliased by dynamic or static rule |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function isPathInjected() |
||
354 | |||
355 | /** |
||
356 | * Get pathway without current controller/action path |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getPathWithoutControllerAction() |
||
376 | |||
377 | /** |
||
378 | * Get current full request URI |
||
379 | * @return string |
||
380 | */ |
||
381 | public function getFullUrl() |
||
385 | |||
386 | /** |
||
387 | * Get base path from current environment without basePath of subdirectories |
||
388 | * @return string |
||
389 | */ |
||
390 | public function getInterfaceSlug() |
||
400 | |||
401 | } |