Complex classes like TargetRoute 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 TargetRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class TargetRoute extends Mapper |
||
| 34 | { |
||
| 35 | public static $parameters = [ |
||
| 36 | // File |
||
| 37 | 'filename' => null, |
||
| 38 | 'classname' => null, |
||
| 39 | // Call |
||
| 40 | 'module' => 'index', |
||
| 41 | 'controller' => null, |
||
| 42 | 'action' => 'list', |
||
| 43 | 'method' => null, |
||
| 44 | 'params' => [], |
||
| 45 | // Output |
||
| 46 | 'format' => 'html', |
||
| 47 | 'language' => 'en', |
||
| 48 | 'request' => 'get', |
||
| 49 | 'layout' => true, |
||
| 50 | 'ajax' => false, |
||
| 51 | 'renderer' => 'smarty', |
||
| 52 | 'themename' => 'default', |
||
| 53 | 'modrewrite' => false, |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get singleton instance of TargetRoute. |
||
| 58 | * |
||
| 59 | * @return \Koch\Router\TargetRoute |
||
|
|
|||
| 60 | */ |
||
| 61 | public static function instantiate() |
||
| 71 | |||
| 72 | public static function getApplicationNamespace() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $filename |
||
| 79 | */ |
||
| 80 | public static function setFilename($filename) |
||
| 84 | |||
| 85 | public static function getFilename() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $classname |
||
| 100 | */ |
||
| 101 | public static function setClassname($classname) |
||
| 105 | |||
| 106 | public static function getClassname() |
||
| 115 | |||
| 116 | public static function setController($controller) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns Name of the Controller. |
||
| 123 | * |
||
| 124 | * @return string Controller/Modulename |
||
| 125 | */ |
||
| 126 | public static function getController() |
||
| 136 | |||
| 137 | public static function getModule() |
||
| 141 | |||
| 142 | public static function setModule($module) |
||
| 146 | |||
| 147 | public static function setAction($action) |
||
| 151 | |||
| 152 | public static function getAction() |
||
| 156 | |||
| 157 | public static function getActionNameWithoutPrefix() |
||
| 163 | |||
| 164 | public static function setId($id) |
||
| 168 | |||
| 169 | public static function getId() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Method to get the Action with Prefix. |
||
| 176 | * |
||
| 177 | * @return $string |
||
| 178 | */ |
||
| 179 | public static function getActionName() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $method |
||
| 186 | */ |
||
| 187 | public static function setMethod($method) |
||
| 191 | |||
| 192 | public static function getMethod() |
||
| 200 | |||
| 201 | public static function setParameters($params) |
||
| 205 | |||
| 206 | public static function getParameters() |
||
| 218 | |||
| 219 | public static function getFormat() |
||
| 223 | |||
| 224 | public static function getRequestMethod() |
||
| 228 | |||
| 229 | public static function getLayoutMode() |
||
| 233 | |||
| 234 | public static function getAjaxMode() |
||
| 238 | |||
| 239 | public static function getRenderEngine() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $renderEngineName |
||
| 246 | */ |
||
| 247 | public static function setRenderEngine($renderEngineName) |
||
| 251 | |||
| 252 | public static function getBackendTheme() |
||
| 256 | |||
| 257 | public static function getFrontendTheme() |
||
| 261 | |||
| 262 | public static function getThemeName() |
||
| 276 | |||
| 277 | public static function setThemeName($themename) |
||
| 281 | |||
| 282 | public static function getModRewriteStatus() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Dispatchable ensures that the "logical" route is "physically" valid. |
||
| 289 | * The method checks, if the TargetRoute relates to correct file, controller and action. |
||
| 290 | * |
||
| 291 | * @return bool True if TargetRoute is dispatchable, false otherwise. |
||
| 292 | */ |
||
| 293 | public static function dispatchable() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * setSegmentsToTargetRoute. |
||
| 316 | * |
||
| 317 | * This takes the requirements array or the uri_segments array |
||
| 318 | * and sets the proper parameters on the Target Route, |
||
| 319 | * thereby making it dispatchable. |
||
| 320 | * |
||
| 321 | * URL Examples |
||
| 322 | * a) index.php?mod=news=action=archive |
||
| 323 | * b) index.php?mod=news&ctrl=admin&action=edit&id=77 |
||
| 324 | * |
||
| 325 | * mod => controller => <News>Controller.php |
||
| 326 | * ctrl => controller suffix => News<Admin>Controller.php |
||
| 327 | * action => method => action_<action> |
||
| 328 | * *id* => additional call params for the method |
||
| 329 | */ |
||
| 330 | public static function setSegmentsToTargetRoute($array) |
||
| 331 | { |
||
| 332 | /** |
||
| 333 | * if array is an found route, it has the following array structure: |
||
| 334 | * [regexp], [number_of_segments] and [requirements]. |
||
| 335 | * |
||
| 336 | * for getting the values module, controller, action only the |
||
| 337 | * [requirements] array is relevant. overwriting $array drops the keys |
||
| 338 | * [regexp] and [number_of_segments] because they are no longer needed. |
||
| 339 | */ |
||
| 340 | if ((isset($array['requirements'])) || (array_key_exists('requirements', $array))) { |
||
| 341 | $array = $array['requirements']; |
||
| 342 | } |
||
| 343 | |||
| 344 | // Module |
||
| 345 | if (isset($array['module'])) { |
||
| 346 | self::setModule($array['module']); |
||
| 347 | // yes, set the controller of the module, too |
||
| 348 | // if it is e.g. AdminController on Module News, then it will be overwritten below |
||
| 349 | self::setController($array['module']); |
||
| 350 | unset($array['module']); |
||
| 351 | } |
||
| 352 | |||
| 353 | // Controller |
||
| 354 | if (isset($array['controller'])) { |
||
| 355 | self::setController($array['controller']); |
||
| 356 | // if a module was not set yet, then set the current controller also as module |
||
| 357 | if (self::$parameters['module'] === 'index') { |
||
| 358 | self::setModule($array['controller']); |
||
| 359 | } |
||
| 360 | unset($array['controller']); |
||
| 361 | } |
||
| 362 | |||
| 363 | // Action |
||
| 364 | if (isset($array['action'])) { |
||
| 365 | self::setAction($array['action']); |
||
| 366 | unset($array['action']); |
||
| 367 | } |
||
| 368 | |||
| 369 | // Id |
||
| 370 | if (isset($array['id'])) { |
||
| 371 | self::setId($array['id']); |
||
| 372 | |||
| 373 | // if we set an ID and the action is still empty (=default: list), |
||
| 374 | // then we automatically set the action name according to the request method |
||
| 375 | if (self::$parameters['action'] === 'list') { |
||
| 376 | $request_method = self::getRequestMethod(); |
||
| 377 | |||
| 378 | if ($request_method === 'GET') { |
||
| 379 | self::setAction('show'); |
||
| 380 | } elseif ($request_method === 'PUT') { |
||
| 381 | self::setAction('update'); |
||
| 382 | } elseif ($request_method === 'DELETE') { |
||
| 383 | self::setAction('delete'); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | unset($array['id']); |
||
| 388 | } |
||
| 389 | |||
| 390 | // if the request method is POST then set the action INSERT |
||
| 391 | if ('POST' === self::getRequestMethod()) { |
||
| 392 | self::setAction('insert'); |
||
| 393 | } |
||
| 394 | |||
| 395 | // Parameters |
||
| 396 | if (count($array) > 0) { |
||
| 397 | self::setParameters($array); |
||
| 398 | unset($array); |
||
| 399 | } |
||
| 400 | |||
| 401 | # instantiate the target route |
||
| 402 | |||
| 403 | return self::instantiate(); |
||
| 404 | } |
||
| 405 | |||
| 406 | public static function reset() |
||
| 407 | { |
||
| 408 | $reset_params = [ |
||
| 409 | // File |
||
| 410 | 'filename' => null, |
||
| 411 | 'classname' => null, |
||
| 412 | // Call |
||
| 413 | 'module' => 'index', |
||
| 414 | 'controller' => 'index', |
||
| 415 | 'action' => 'list', |
||
| 416 | 'method' => 'actionList', |
||
| 417 | 'params' => [], |
||
| 418 | // Output |
||
| 419 | 'format' => 'html', |
||
| 420 | 'language' => 'en', |
||
| 421 | 'request' => 'get', |
||
| 422 | 'layout' => true, |
||
| 423 | 'ajax' => false, |
||
| 424 | 'renderer' => 'smarty', |
||
| 425 | 'themename' => 'default', |
||
| 426 | 'modrewrite' => false, |
||
| 427 | ]; |
||
| 428 | |||
| 429 | self::$parameters = $reset_params; |
||
| 430 | } |
||
| 431 | |||
| 432 | public static function getRoute() |
||
| 436 | } |
||
| 437 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.