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 |
||
| 32 | class Table extends AbstractTable |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Time that the token remains valid |
||
| 36 | */ |
||
| 37 | const TOKEN_EXPIRATION_TIME = 1800; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Authenticate user by login/pass |
||
| 41 | * |
||
| 42 | * @param string $username |
||
| 43 | * @param string $password |
||
| 44 | * @throws AuthException |
||
| 45 | * @throws Exception |
||
| 46 | */ |
||
| 47 | 2 | public function authenticateEquals($username, $password) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Check user by login/pass |
||
| 62 | * |
||
| 63 | * @param string $username |
||
| 64 | * @param string $password |
||
| 65 | * @throws AuthException |
||
| 66 | * @return Row |
||
| 67 | */ |
||
| 68 | 4 | public function checkEquals($username, $password) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Authenticate user by login/pass |
||
| 87 | * |
||
| 88 | * @param Users\Row $user |
||
| 89 | * @param string $password |
||
| 90 | * @throws AuthException |
||
| 91 | * @return Row |
||
| 92 | */ |
||
| 93 | public function generateEquals($user, $password) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Generates cookie for authentication |
||
| 124 | * |
||
| 125 | * @throws \Bluz\Db\Exception\DbException |
||
| 126 | */ |
||
| 127 | public function generateCookie() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Authenticate via cookie |
||
| 159 | * |
||
| 160 | * @param $userId |
||
| 161 | * @param $token |
||
| 162 | * @throws AuthException |
||
| 163 | * @throws Exception |
||
| 164 | */ |
||
| 165 | public function authenticateCookie($userId, $token) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if supplied cookie is valid |
||
| 178 | * |
||
| 179 | * @param $userId |
||
| 180 | * @param $token |
||
| 181 | * @return Row |
||
| 182 | * @throws AuthException |
||
| 183 | */ |
||
| 184 | public function checkCookie($userId, $token) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Removes a cookie-token from database |
||
| 204 | * |
||
| 205 | * @param $userId |
||
| 206 | * @throws \Bluz\Db\Exception\DbException |
||
| 207 | */ |
||
| 208 | 1 | public function removeCookieToken($userId) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Call Hash Function |
||
| 220 | * |
||
| 221 | * @param string $password |
||
| 222 | * @throws \Application\Exception |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | 1 | View Code Duplication | protected function callHashFunction($password) |
| 240 | |||
| 241 | /** |
||
| 242 | * Call Verify Function |
||
| 243 | * |
||
| 244 | * @param string $password |
||
| 245 | * @param string $hash |
||
| 246 | * @throws \Application\Exception |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 4 | View Code Duplication | protected function callVerifyFunction($password, $hash) |
| 264 | |||
| 265 | /** |
||
| 266 | * authenticate user by token |
||
| 267 | * |
||
| 268 | * @param string $token |
||
| 269 | * @throws \Bluz\Auth\AuthException |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function authenticateToken($token) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * authenticate user by token |
||
| 285 | * |
||
| 286 | * @param string $token |
||
| 287 | * @throws \Bluz\Auth\AuthException |
||
| 288 | * @return Row |
||
| 289 | */ |
||
| 290 | public function checkToken($token) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param $equalAuth |
||
| 305 | * @return Row |
||
| 306 | * @throws Exception |
||
| 307 | * @throws \Bluz\Db\Exception\DbException |
||
| 308 | */ |
||
| 309 | 1 | public function generateToken($equalAuth) |
|
| 339 | } |
||
| 340 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.