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 |
||
22 | class UserController implements ContainerInjectableInterface |
||
23 | { |
||
24 | use ContainerInjectableTrait; |
||
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * @var $data description |
||
30 | */ |
||
31 | //private $data; |
||
32 | |||
33 | |||
34 | |||
35 | // /** |
||
36 | // * The initialize method is optional and will always be called before the |
||
37 | // * target method/action. This is a convienient method where you could |
||
38 | // * setup internal properties that are commonly used by several methods. |
||
39 | // * |
||
40 | // * @return void |
||
41 | // */ |
||
42 | // public function initialize() : void |
||
43 | // { |
||
44 | // ; |
||
45 | // } |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * Description. |
||
51 | * |
||
52 | * @param datatype $variable Description |
||
|
|||
53 | * |
||
54 | * @throws Exception |
||
55 | * |
||
56 | * @return object as a response object |
||
57 | */ |
||
58 | public function indexActionGet() : object |
||
59 | { |
||
60 | $page = $this->di->get("page"); |
||
61 | $user = new User(); |
||
62 | $user->setDb($this->di->get("dbqb")); |
||
63 | $page->add("user/login"); |
||
64 | |||
65 | return $page->render([ |
||
66 | "title" => "A index page", |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Description. |
||
74 | * |
||
75 | * @param datatype $variable Description |
||
76 | * |
||
77 | * @throws Exception |
||
78 | * |
||
79 | * @return object as a response object |
||
80 | */ |
||
81 | View Code Duplication | public function loginAction() : object |
|
82 | { |
||
83 | $page = $this->di->get("page"); |
||
84 | $form = new UserLoginForm($this->di); |
||
85 | $form->check(); |
||
86 | $data = [ |
||
87 | "form" => $form->getHTML(), |
||
88 | "title" => "A login page", |
||
89 | ]; |
||
90 | $page->add("user/login", $data); |
||
91 | return $page->render($data); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Description. |
||
96 | * |
||
97 | * @param datatype $variable Description |
||
98 | * |
||
99 | * @throws Exception |
||
100 | * |
||
101 | * @return object as a response object |
||
102 | */ |
||
103 | View Code Duplication | public function createAction() : object |
|
104 | { |
||
105 | $page = $this->di->get("page"); |
||
106 | $form = new CreateUserForm($this->di); |
||
107 | $form->check(); |
||
108 | |||
109 | $data = [ |
||
110 | "form" => $form->getHTML(), |
||
111 | "title" => "Create user" |
||
112 | ]; |
||
113 | |||
114 | $page->add("user/create", $data); |
||
115 | return $page->render($data); |
||
116 | } |
||
117 | |||
118 | public function viewAction($userId): object |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Handler with form to update an item. |
||
148 | * |
||
149 | * @param int $id the id to update. |
||
150 | * |
||
151 | * @return object as a response object |
||
152 | */ |
||
153 | View Code Duplication | public function updateAction(int $id) : object |
|
167 | } |
||
168 |
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.