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 |
||
| 24 | class UserController implements |
||
| 25 | ConfigureInterface, |
||
| 26 | InjectionAwareInterface |
||
| 27 | { |
||
| 28 | use ConfigureTrait, |
||
| 29 | InjectionAwareTrait; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @var $data description |
||
| 34 | */ |
||
| 35 | //private $data; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Description. |
||
| 40 | * |
||
| 41 | * @param datatype $variable Description |
||
|
|
|||
| 42 | * |
||
| 43 | * @throws Exception |
||
| 44 | * |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | public function getIndex() |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Description. |
||
| 65 | * |
||
| 66 | * @param datatype $variable Description |
||
| 67 | * |
||
| 68 | * @throws Exception |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function getPostLogin() |
||
| 73 | { |
||
| 74 | $title = "A login page"; |
||
| 75 | $view = $this->di->get("view"); |
||
| 76 | $pageRender = $this->di->get("pageRender"); |
||
| 77 | $url = $this->di->get("url"); |
||
| 78 | $response = $this->di->get("response"); |
||
| 79 | $session = $this->di->get("session"); |
||
| 80 | if ($session->has("email")) { |
||
| 81 | $url = $url->create("user/profile"); |
||
| 82 | $response->redirect($url); |
||
| 83 | } else { |
||
| 84 | $form = new UserLoginForm($this->di); |
||
| 85 | |||
| 86 | $form->check(); |
||
| 87 | |||
| 88 | $data = [ |
||
| 89 | "content" => $form->getHTML(), |
||
| 90 | ]; |
||
| 91 | |||
| 92 | $view->add("default1/article", $data); |
||
| 93 | |||
| 94 | $pageRender->renderLoginAndCreate(["title" => $title]); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Description. |
||
| 101 | * |
||
| 102 | * @param datatype $variable Description |
||
| 103 | * |
||
| 104 | * @throws Exception |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function getPostCreateUser() |
||
| 109 | { |
||
| 110 | $this->di->get("session")->set("create", "true"); |
||
| 111 | $title = "A create user page"; |
||
| 112 | $view = $this->di->get("view"); |
||
| 113 | $pageRender = $this->di->get("pageRender"); |
||
| 114 | $form = new CreateUserForm($this->di); |
||
| 115 | |||
| 116 | $form->check(); |
||
| 117 | |||
| 118 | $data = [ |
||
| 119 | "content" => $form->getHTML(), |
||
| 120 | ]; |
||
| 121 | |||
| 122 | $view->add("default1/article", $data); |
||
| 123 | |||
| 124 | $pageRender->renderLoginAndCreate(["title" => $title]); |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | View Code Duplication | public function getUserProfile() |
|
| 129 | { |
||
| 130 | $title = "Profile"; |
||
| 131 | $view = $this->di->get("view"); |
||
| 132 | $pageRender = $this->di->get("pageRender"); |
||
| 133 | $user = new User(); |
||
| 134 | $user->setDb($this->di->get("db")); |
||
| 135 | $session = $this->di->get("session"); |
||
| 136 | $data = [ |
||
| 137 | "content" => $user->getInformation($session->get("email")), |
||
| 138 | ]; |
||
| 139 | |||
| 140 | $view->add("users/profile", $data); |
||
| 141 | |||
| 142 | $pageRender->renderPage(["title" => $title]); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function logout() |
||
| 146 | { |
||
| 147 | $url = $this->di->get("url"); |
||
| 148 | $response = $this->di->get("response"); |
||
| 149 | $session = $this->di->get("session"); |
||
| 150 | $login = $url->create("user/login"); |
||
| 151 | |||
| 152 | if ($session->has("email")) { |
||
| 153 | $session->delete("email"); |
||
| 154 | $response->redirect($login); |
||
| 155 | } else { |
||
| 156 | $response->redirect($login); |
||
| 157 | } |
||
| 158 | |||
| 159 | $hasSession = session_status() == PHP_SESSION_ACTIVE; |
||
| 160 | |||
| 161 | if (!$hasSession) { |
||
| 162 | $response->redirect($login); |
||
| 163 | return true; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | public function checkLogin() |
||
| 179 | |||
| 180 | View Code Duplication | public function editProfile($id) |
|
| 199 | |||
| 200 | |||
| 201 | View Code Duplication | public function getAllUsers() |
|
| 202 | { |
||
| 203 | if ($this->checkAdminLoggedIn()) { |
||
| 204 | $title = "A collection of items"; |
||
| 205 | $view = $this->di->get("view"); |
||
| 206 | $pageRender = $this->di->get("pageRender"); |
||
| 207 | $db = $this->di->get("db"); |
||
| 208 | $user = new User(); |
||
| 209 | $user->setDb($db); |
||
| 210 | |||
| 211 | $data = [ |
||
| 212 | "items" => $user->findAll(), |
||
| 213 | ]; |
||
| 214 | |||
| 215 | $view->add("admin/viewUsers", $data); |
||
| 216 | |||
| 217 | $pageRender->renderPage(["title" => $title]); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | View Code Duplication | public function getAllUsersPublic() |
|
| 222 | { |
||
| 223 | $title = "All Users"; |
||
| 224 | $view = $this->di->get("view"); |
||
| 225 | $pageRender = $this->di->get("pageRender"); |
||
| 226 | $db = $this->di->get("db"); |
||
| 227 | $user = new User(); |
||
| 228 | $user->setDb($db); |
||
| 229 | |||
| 230 | $data = [ |
||
| 231 | "items" => $user->findAll(), |
||
| 232 | ]; |
||
| 233 | |||
| 234 | $view->add("users/showAll", $data); |
||
| 235 | |||
| 236 | $pageRender->renderPage(["title" => $title]); |
||
| 237 | } |
||
| 238 | |||
| 239 | View Code Duplication | public function createUser() |
|
| 259 | |||
| 260 | |||
| 261 | View Code Duplication | public function deleteUser() |
|
| 262 | { |
||
| 263 | if ($this->checkAdminLoggedIn()) { |
||
| 264 | $title = "Delete an item"; |
||
| 265 | $view = $this->di->get("view"); |
||
| 266 | $pageRender = $this->di->get("pageRender"); |
||
| 267 | $form = new AdminDeleteUserForm($this->di); |
||
| 268 | |||
| 269 | $form->check(); |
||
| 270 | |||
| 271 | $data = [ |
||
| 272 | "form" => $form->getHTML(), |
||
| 273 | ]; |
||
| 274 | |||
| 275 | $view->add("admin/delete", $data); |
||
| 276 | |||
| 277 | $pageRender->renderPage(["title" => $title]); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | View Code Duplication | public function updateUser($id) |
|
| 282 | { |
||
| 283 | if ($this->checkAdminLoggedIn()) { |
||
| 284 | $title = "Update an item"; |
||
| 285 | $view = $this->di->get("view"); |
||
| 286 | $pageRender = $this->di->get("pageRender"); |
||
| 287 | $form = new AdminUpdateUser($this->di, $id); |
||
| 288 | |||
| 289 | $form->check(); |
||
| 290 | |||
| 291 | $data = [ |
||
| 292 | "form" => $form->getHTML(), |
||
| 293 | ]; |
||
| 294 | |||
| 295 | $view->add("admin/update", $data); |
||
| 296 | |||
| 297 | $pageRender->renderPage(["title" => $title]); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | public function checkUserIdMatch($id) |
||
| 302 | { |
||
| 303 | $url = $this->di->get("url"); |
||
| 304 | $response = $this->di->get("response"); |
||
| 305 | $session = $this->di->get("session"); |
||
| 306 | $db = $this->di->get("db"); |
||
| 307 | |||
| 308 | if ($session->has("email")) { |
||
| 309 | $email = $session->get("email"); |
||
| 310 | $user = new User(); |
||
| 311 | $user->setDb($db); |
||
| 312 | $res = $user->find("email", $email); |
||
| 313 | if ($res->id != $id) { |
||
| 314 | $url = $url->create("user/profile"); |
||
| 315 | $response->redirect($url); |
||
| 316 | return false; |
||
| 317 | } |
||
| 318 | return true; |
||
| 319 | } else { |
||
| 320 | $url = $url->create("user/profile"); |
||
| 321 | $response->redirect($url); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | public function checkAdminLoggedIn() |
||
| 326 | { |
||
| 327 | $url = $this->di->get("url"); |
||
| 328 | $response = $this->di->get("response"); |
||
| 329 | $session = $this->di->get("session"); |
||
| 330 | $db = $this->di->get("db"); |
||
| 331 | |||
| 332 | if ($session->has("email")) { |
||
| 333 | $email = $session->get("email"); |
||
| 334 | $user = new User(); |
||
| 335 | $user->setDb($db); |
||
| 336 | $res = $user->find("email", $email); |
||
| 337 | |||
| 338 | if (!$res->permissions == "admin" || $res->permissions == "user") { |
||
| 339 | $url = $url->create("user/login"); |
||
| 340 | $response->redirect($url); |
||
| 341 | } |
||
| 342 | return true; |
||
| 343 | } else { |
||
| 344 | $url = $url->create("user/login"); |
||
| 345 | $response->redirect($url); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | |||
| 350 | public function getAllPostsAndCommentsFromSpecificUser($id) |
||
| 379 | |||
| 380 | public function checkLoginPage() |
||
| 392 | |||
| 393 | |||
| 394 | public function checkIfLoggedIn() |
||
| 398 | |||
| 399 | |||
| 400 | public function returnId($email) |
||
| 408 | } |
||
| 409 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.