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:
| 1 | <?php |
||
| 34 | class Bootstrap extends Application |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | * |
||
| 39 | * @param string $module |
||
| 40 | * @param string $controller |
||
| 41 | * @param array $params |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | 37 | protected function preDispatch($module, $controller, $params = array()) |
|
|
|
|||
| 45 | { |
||
| 46 | // example of setup default title |
||
| 47 | 37 | Layout::title("Bluz Skeleton"); |
|
| 48 | |||
| 49 | // apply "remember me" function |
||
| 50 | 37 | if (!AuthProxy::getIdentity()) { |
|
| 51 | 11 | if ($token = Request::getHeader('Bluz-Token')) { |
|
| 52 | Auth\Table::getInstance()->authenticateToken($token); |
||
| 53 | 11 | } elseif (!empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) { |
|
| 54 | // try to login |
||
| 55 | try { |
||
| 56 | Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']); |
||
| 57 | } catch (AuthException $e) { |
||
| 58 | $this->getResponse()->setCookie('rId', '', 1, '/'); |
||
| 59 | $this->getResponse()->setCookie('rToken', '', 1, '/'); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | 37 | parent::preDispatch($module, $controller, $params); |
|
| 65 | 37 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | * |
||
| 70 | * @param string $module |
||
| 71 | * @param string $controller |
||
| 72 | * @param array $params |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | 31 | protected function postDispatch($module, $controller, $params = array()) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Denied access |
||
| 82 | * @param ForbiddenException $exception |
||
| 83 | * @return \Bluz\Controller\Controller|null |
||
| 84 | */ |
||
| 85 | 2 | public function forbidden(ForbiddenException $exception) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Render with debug headers |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function render() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Finish it |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function end() |
|
| 151 | } |
||
| 152 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: