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 |
||
11 | class AdminController extends UserController |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Get all users |
||
16 | * |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | 2 | public function getUsers() |
|
21 | { |
||
22 | 2 | $user = new User(); |
|
23 | 2 | $user->setDb($this->di->get("db")); |
|
24 | 2 | $users = $user->findAll(); |
|
25 | |||
26 | 2 | return array_map(function ($user) { |
|
27 | 2 | $user->setGravatar($user->email); |
|
28 | |||
29 | 2 | return $user; |
|
30 | 2 | }, $users); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * check if user is logged in |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | 1 | public function checkIsAdmin() |
|
39 | { |
||
40 | 1 | $this->checkIsLogin(); |
|
41 | |||
42 | 1 | $user = new User(); |
|
43 | 1 | $user->setDb($this->di->get("db")); |
|
44 | 1 | $user = $user->find("name", $this->di->get("session")->get("user")); |
|
45 | |||
46 | 1 | if ($user->authority != "admin") { |
|
47 | $views = [ |
||
48 | 1 | ["admin/fail", [], "main"] |
|
49 | 1 | ]; |
|
50 | 1 | $this->renderPage($views, "Not authorized"); |
|
51 | 1 | } |
|
52 | 1 | } |
|
53 | |||
54 | |||
55 | /** |
||
56 | * Description. |
||
57 | * |
||
58 | * @param datatype $variable Description |
||
|
|||
59 | * |
||
60 | * @throws Exception |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | 1 | public function getUsersIndex() |
|
65 | { |
||
66 | $views = [ |
||
67 | 1 | ["admin/navbar", [], "main"], |
|
68 | 1 | ["admin/crud/view-all", ["users" => $this->getUsers()], "main"] |
|
69 | 1 | ]; |
|
70 | |||
71 | 1 | $this->di->get("pageRenderComment")->renderPage([ |
|
72 | 1 | "views" => $views, |
|
73 | "title" => "A collection of items" |
||
74 | 1 | ]); |
|
75 | 1 | } |
|
76 | |||
77 | |||
78 | /** |
||
79 | * Description. |
||
80 | * |
||
81 | * @param datatype $variable Description |
||
82 | * |
||
83 | * @throws Exception |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | 1 | View Code Duplication | public function getPostAdminEditUser($id) |
88 | { |
||
89 | 1 | $form = new EditUserForm($this->di, $id); |
|
90 | 1 | $form->check(); |
|
91 | |||
92 | $views = [ |
||
93 | 1 | ["admin/navbar", [], "main"], |
|
94 | 1 | ["admin/crud/edit", ["form" => $form->getHTML()], "main"] |
|
95 | 1 | ]; |
|
96 | |||
97 | 1 | $this->renderPage($views, "A login page"); |
|
98 | 1 | } |
|
99 | |||
100 | /** |
||
101 | * Description. |
||
102 | * |
||
103 | * @param datatype $variable Description |
||
104 | * |
||
105 | * @throws Exception |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | View Code Duplication | public function getPostAdminCreateUser() |
|
121 | } |
||
122 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.