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 |
||
| 12 | class Request extends FoundationRequest |
||
| 13 | { |
||
| 14 | protected $language; |
||
| 15 | protected $languageInPath = false; |
||
| 16 | |||
| 17 | // special variable for route aliasing |
||
| 18 | protected $aliasPathTarget = false; |
||
| 19 | // special variable for route callback binding |
||
| 20 | protected $callbackClass = false; |
||
| 21 | |||
| 22 | // fast access for controller building |
||
| 23 | protected $controller; |
||
| 24 | protected $action; |
||
| 25 | protected $argumentId; |
||
| 26 | protected $argumentAdd; |
||
| 27 | |||
| 28 | public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Sets the parameters for this request. |
||
| 36 | * |
||
| 37 | * This method also re-initializes all properties. |
||
| 38 | * |
||
| 39 | * @param array $query The GET parameters |
||
| 40 | * @param array $request The POST parameters |
||
| 41 | * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...) |
||
| 42 | * @param array $cookies The COOKIE parameters |
||
| 43 | * @param array $files The FILES parameters |
||
| 44 | * @param array $server The SERVER parameters |
||
| 45 | * @param string $content The raw body data |
||
| 46 | * |
||
| 47 | * @api |
||
| 48 | */ |
||
| 49 | public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) |
||
| 65 | |||
| 66 | protected function afterInitialize() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Working with path array data |
||
| 176 | * @param array|null $pathArray |
||
| 177 | */ |
||
| 178 | private function setPathdata(array $pathArray = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get pathway as string |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getPathInfo() |
||
| 214 | |||
| 215 | public function languageInPath() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get current language |
||
| 222 | * @return string|null |
||
| 223 | */ |
||
| 224 | public function getLanguage() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get current controller name |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getController() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get current controller action() name |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getAction() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get current $id argument for controller action |
||
| 249 | * @return string|null |
||
| 250 | */ |
||
| 251 | public function getID() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get current $add argument for controller action |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function getAdd() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get callback class alias if exist |
||
| 267 | * @return bool|string |
||
| 268 | */ |
||
| 269 | public function getCallbackAlias() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get static alias of pathway if exist |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function getStaticAlias() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Check if current request is aliased by dynamic or static rule |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function isPathInjected() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get pathway without current controller/action path |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getPathWithoutControllerAction() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get current full request URI |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getFullUrl() |
||
| 322 | |||
| 323 | } |