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 |
||
14 | class CommentModel implements |
||
15 | ConfigureInterface, |
||
16 | InjectionAwareInterface |
||
17 | { |
||
18 | use ConfigureTrait, |
||
19 | InjectionAwareTrait; |
||
20 | |||
21 | /** |
||
22 | * @var array $session inject a reference to the session. |
||
23 | */ |
||
24 | //private $session; |
||
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * @var string $key to use when storing in session. |
||
30 | */ |
||
31 | const KEY = "commentsystem"; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Inject dependencies. |
||
37 | * |
||
38 | * @param array $dependency key/value array with dependencies. |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | //public function inject($dependency) |
||
43 | //{ |
||
44 | // $this->session = $dependency; |
||
45 | // return $this; |
||
46 | //} |
||
47 | |||
48 | |||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Get ALL comments from session |
||
54 | * |
||
55 | * @param string $key for data subset. |
||
|
|||
56 | * |
||
57 | * @return object with the dataset |
||
58 | */ |
||
59 | 5 | public function getComments() |
|
69 | |||
70 | |||
71 | /** |
||
72 | * Get ONE comment from session |
||
73 | * |
||
74 | * @param string $key for dataset. |
||
75 | * @param int $id for comment. |
||
76 | * |
||
77 | * @return array with the comment, name, email, id, or null if not exists |
||
78 | */ |
||
79 | 5 | public function getComment($id) |
|
93 | |||
94 | |||
95 | /** |
||
96 | * Add a comment to a dataset. |
||
97 | * |
||
98 | * @param array $post variables from posted comment |
||
99 | * (article, name, email, comment) |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | 5 | public function addComment($post) |
|
116 | |||
117 | |||
118 | /** |
||
119 | * Update old comment with new comment |
||
120 | * |
||
121 | * @param int $id id for comment |
||
122 | * @param array $comment the comment-array (name, email, comment, id) |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | 2 | View Code Duplication | public function updateComment($id, $comment) |
138 | |||
139 | |||
140 | /** |
||
141 | * Delete comment with key and id |
||
142 | * |
||
143 | * @param int $id to delete |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | 1 | View Code Duplication | public function deleteComment($id) |
161 | } |
||
162 |
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.