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 runPathAliasing() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set trusted proxies from configs |
||
| 194 | */ |
||
| 195 | private function loadTrustedProxies() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Working with path array data |
||
| 212 | * @param array|null $pathArray |
||
| 213 | */ |
||
| 214 | private function setPathdata(array $pathArray = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get pathway as string |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getPathInfo() |
||
| 250 | |||
| 251 | public function languageInPath() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get current language |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function getLanguage() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get current controller name |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getController() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get current controller action() name |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getAction() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get current $id argument for controller action |
||
| 285 | * @return string|null |
||
| 286 | */ |
||
| 287 | public function getID() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get current $add argument for controller action |
||
| 294 | * @return string|null |
||
| 295 | */ |
||
| 296 | public function getAdd() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get callback class alias if exist |
||
| 303 | * @return bool|string |
||
| 304 | */ |
||
| 305 | public function getCallbackAlias() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get static alias of pathway if exist |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function getStaticAlias() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check if current request is aliased by dynamic or static rule |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isPathInjected() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get pathway without current controller/action path |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function getPathWithoutControllerAction() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get current full request URI |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getFullUrl() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get base path from current environment without basePath of subdirectories |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getInterfaceSlug() |
||
| 373 | |||
| 374 | } |