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 |
||
8 | class User extends ActiveRecordModelExtender |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var string $tableName name of the database table. |
||
13 | */ |
||
14 | protected $tableName = "coffee_users"; |
||
15 | |||
16 | /** |
||
17 | * Columns in the table. |
||
18 | * |
||
19 | * @var integer $id primary key auto incremented. |
||
20 | */ |
||
21 | public $id; |
||
22 | public $name; |
||
23 | public $email; |
||
24 | public $pass; |
||
25 | public $authority = "user"; |
||
26 | public $question; |
||
27 | |||
28 | public function getReputation($user) |
||
29 | { |
||
30 | // Get all the vote->scores connected to users posts |
||
31 | $questionPoints = array_reduce($user->questions, function ($carry, $item) { |
||
32 | $carry += $item->question->vote->score; |
||
33 | return $carry; |
||
34 | }); |
||
35 | |||
36 | $postPoints = array_reduce($user->posts, function ($carry, $item) { |
||
37 | $carry += $item->vote->score; |
||
38 | return $carry; |
||
39 | }); |
||
40 | |||
41 | $commentPoints = array_reduce($user->comments, function ($carry, $item) { |
||
42 | $carry += $item->vote->score; |
||
43 | return $carry; |
||
44 | }); |
||
45 | |||
46 | $points = |
||
47 | ($questionPoints * 2) + |
||
48 | ($postPoints * 3) + |
||
49 | ($commentPoints * 1); |
||
50 | |||
51 | // Algorithm for reputation points |
||
52 | $reputation = |
||
53 | (count($user->questions) * 2) + |
||
54 | (count($user->posts) * 3) + |
||
55 | (count($user->comments) * 1) + $points; |
||
56 | |||
57 | return $reputation; |
||
58 | } |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Sets up user |
||
63 | * @param object $user |
||
64 | * |
||
65 | * @return object |
||
66 | */ |
||
67 | public function setupUser($user) |
||
68 | { |
||
69 | $question = new Question($this->db); |
||
70 | $post = new Post($this->db); |
||
71 | $comment = new Comment($this->db); |
||
72 | $vote = new Vote($this->db); |
||
73 | |||
74 | // Basic setup for user |
||
75 | $user->img = $this->getGravatar($user->name); |
||
76 | |||
77 | // Get all posts/votes user made |
||
78 | $user->questions = $question->getQuestions("user = ?", $user->name); |
||
79 | $user->comments = $comment->getComments("user = ?", $user->name); |
||
80 | $user->posts = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]); |
||
81 | $user->votes = $vote->getVote("user = ?", [$user->name]); |
||
82 | |||
83 | // Amount of posts |
||
84 | $user->postAmount = count($user->questions) + count($user->posts) + count($user->comments); |
||
85 | |||
86 | // Algorithm for reputation points |
||
87 | $user->reputation = $this->getReputation($user); |
||
88 | |||
89 | // Get all the questions connected to answers |
||
90 | $user->answeredQuestions = array_map(function ($answer) { |
||
91 | $question = new Question($this->db); |
||
92 | return $question->getQuestion($answer->questionId); |
||
93 | }, $user->posts); |
||
94 | |||
95 | return $user; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns post with markdown and gravatar |
||
100 | * @param string $sql |
||
101 | * @param array $param |
||
|
|||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 2 | View Code Duplication | public function getAllUsers($sql = null, $params = null) |
106 | { |
||
107 | 2 | if ($sql == null) { |
|
108 | 2 | $users = $this->findAll(); |
|
109 | } |
||
110 | if ($sql != null) { |
||
111 | $users = $this->findAllWhere($sql, $params); |
||
112 | } |
||
113 | |||
114 | return array_map(array($this, 'setupUser'), $users); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * return question/answer, three attributes are set, comments connected to them is an array. |
||
119 | * @param string $name |
||
120 | * |
||
121 | * @return object |
||
122 | */ |
||
123 | 3 | public function getUser($name) |
|
124 | { |
||
125 | 3 | $user = $this->find("name", $name); |
|
126 | $user = $this->setupUser($user); |
||
127 | return $user; |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Check if user exists |
||
133 | * |
||
134 | * @param string $name |
||
135 | * |
||
136 | * @return boolean true if user exists in database else false |
||
137 | */ |
||
138 | 1 | public function userExists($name) |
|
139 | { |
||
140 | 1 | $user = $this->find("name", $name); |
|
141 | return !$user ? false : true; |
||
142 | } |
||
143 | /** |
||
144 | * Returns gravatar link |
||
145 | * |
||
146 | * @param string $email |
||
147 | * |
||
148 | * @return string as gravatar link |
||
149 | */ |
||
150 | 1 | public function setGravatar() |
|
154 | |||
155 | /** |
||
156 | * Returns gravatar link |
||
157 | * |
||
158 | * @param string $name |
||
159 | * |
||
160 | * @return string as gravatar link |
||
161 | */ |
||
162 | public function getGravatar($name) |
||
163 | { |
||
164 | $this->find("name", $name); |
||
165 | return $this->gravatar($this->email); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Set the pass. |
||
170 | * |
||
171 | * @param string $pass the pass to use. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | 4 | public function setPassword($pass) |
|
179 | |||
180 | /** |
||
181 | * Verify the name and the pass, if successful the object contains |
||
182 | * all details from the database row. |
||
183 | * |
||
184 | * @param string $name name to check. |
||
185 | * @param string $pass the pass to use. |
||
186 | * |
||
187 | * @return boolean true if name and pass matches, else false. |
||
188 | */ |
||
189 | 2 | public function verifyPassword($name, $pass) |
|
194 | |||
195 | /** |
||
196 | * Verify the name and the anaswer, if successful the object contains |
||
197 | * all details from the database row. |
||
198 | * |
||
199 | * @param string $name name to check. |
||
200 | * @param string $answer the answer. |
||
201 | * |
||
202 | * @return boolean true if name and pass matches, else false. |
||
203 | */ |
||
204 | 1 | public function verifyQuestion($name, $answer) |
|
209 | |||
210 | |||
211 | /** |
||
212 | * Check if a post belongs to user |
||
213 | * |
||
214 | * |
||
215 | * @return boolean |
||
216 | */ |
||
217 | public function controlAuthority($loggedUser, $name) |
||
226 | } |
||
227 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.