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 SmartpartnerTable |
||
25 | { |
||
26 | /** |
||
27 | * @var string $_name name of the table |
||
28 | */ |
||
29 | public $_name; |
||
30 | |||
31 | /** |
||
32 | * @var string $_structure structure of the table |
||
33 | */ |
||
34 | public $_structure; |
||
35 | |||
36 | /** |
||
37 | * @var array $_data containing valued of each records to be added |
||
38 | */ |
||
39 | public $_data; |
||
40 | |||
41 | /** |
||
42 | * @var array $_alteredFields containing fields to be altered |
||
43 | */ |
||
44 | public $_alteredFields; |
||
45 | |||
46 | /** |
||
47 | * @var array $_newFields containing new fields to be added |
||
48 | */ |
||
49 | public $_newFields; |
||
50 | |||
51 | /** |
||
52 | * @var array $_droppedFields containing fields to be dropped |
||
53 | */ |
||
54 | public $_droppedFields; |
||
55 | |||
56 | /** |
||
57 | * @var array $_flagForDrop flag table to drop it |
||
58 | */ |
||
59 | public $_flagForDrop = false; |
||
60 | |||
61 | /** |
||
62 | * @var array $_updatedFields containing fields which values will be updated |
||
63 | */ |
||
64 | public $_updatedFields; |
||
65 | |||
66 | /** |
||
67 | * Constructor |
||
68 | * |
||
69 | * @param string $name name of the table |
||
70 | * |
||
71 | */ |
||
72 | public function __construct($name) |
||
77 | |||
78 | /** |
||
79 | * Return the table name, prefixed with site table prefix |
||
80 | * |
||
81 | * @return string table name |
||
82 | * |
||
83 | */ |
||
84 | public function name() |
||
90 | |||
91 | /** |
||
92 | * Set the table structure |
||
93 | * |
||
94 | * @param string $structure table structure |
||
95 | * |
||
96 | */ |
||
97 | public function setStructure($structure) |
||
101 | |||
102 | /** |
||
103 | * Return the table structure |
||
104 | * |
||
105 | * @return string table structure |
||
106 | * |
||
107 | */ |
||
108 | public function getStructure() |
||
112 | |||
113 | /** |
||
114 | * Add values of a record to be added |
||
115 | * |
||
116 | * @param string $data values of a record |
||
117 | * |
||
118 | */ |
||
119 | public function setData($data) |
||
123 | |||
124 | /** |
||
125 | * Get the data array |
||
126 | * |
||
127 | * @return array containing the records values to be added |
||
128 | * |
||
129 | */ |
||
130 | public function getData() |
||
134 | |||
135 | /** |
||
136 | * Use to insert data in a table |
||
137 | * |
||
138 | * @return bool true if success, false if an error occured |
||
139 | * |
||
140 | */ |
||
141 | View Code Duplication | public function addData() |
|
157 | |||
158 | /** |
||
159 | * Add a field to be added |
||
160 | * |
||
161 | * @param string $name name of the field |
||
162 | * @param string $properties properties of the field |
||
163 | * |
||
164 | */ |
||
165 | public function addAlteredField($name, $properties) |
||
171 | |||
172 | /** |
||
173 | * Add new field of a record to be added |
||
174 | * |
||
175 | * @param string $name name of the field |
||
176 | * @param string $properties properties of the field |
||
177 | * |
||
178 | */ |
||
179 | public function addNewField($name, $properties) |
||
185 | |||
186 | /** |
||
187 | * Get fields that need to be altered |
||
188 | * |
||
189 | * @return array fields that need to be altered |
||
190 | * |
||
191 | */ |
||
192 | public function getAlteredFields() |
||
196 | |||
197 | /** |
||
198 | * Add field for which the value will be updated |
||
199 | * |
||
200 | * @param string $name name of the field |
||
201 | * @param string $value value to be set |
||
202 | * |
||
203 | */ |
||
204 | public function addUpdatedField($name, $value) |
||
210 | |||
211 | /** |
||
212 | * Get new fields to be added |
||
213 | * |
||
214 | * @return array fields to be added |
||
215 | * |
||
216 | */ |
||
217 | public function getNewFields() |
||
221 | |||
222 | /** |
||
223 | * Get fields which values need to be updated |
||
224 | * |
||
225 | * @return array fields which values need to be updated |
||
226 | * |
||
227 | */ |
||
228 | public function getUpdatedFields() |
||
232 | |||
233 | /** |
||
234 | * Add values of a record to be added |
||
235 | * |
||
236 | * @param string $name name of the field |
||
237 | * |
||
238 | */ |
||
239 | public function addDroppedField($name) |
||
243 | |||
244 | /** |
||
245 | * Get fields that need to be dropped |
||
246 | * |
||
247 | * @return array fields that need to be dropped |
||
248 | * |
||
249 | */ |
||
250 | public function getDroppedFields() |
||
254 | |||
255 | /** |
||
256 | * Set the flag to drop the table |
||
257 | * |
||
258 | */ |
||
259 | public function setFlagForDrop() |
||
263 | |||
264 | /** |
||
265 | * Use to create a table |
||
266 | * |
||
267 | * @return bool true if success, false if an error occured |
||
268 | * |
||
269 | */ |
||
270 | View Code Duplication | public function createTable() |
|
285 | |||
286 | /** |
||
287 | * Use to drop a table |
||
288 | * |
||
289 | * @return bool true if success, false if an error occured |
||
290 | * |
||
291 | */ |
||
292 | View Code Duplication | public function dropTable() |
|
308 | |||
309 | /** |
||
310 | * Use to alter a table |
||
311 | * |
||
312 | * @return bool true if success, false if an error occured |
||
313 | * |
||
314 | */ |
||
315 | View Code Duplication | public function alterTable() |
|
334 | |||
335 | /** |
||
336 | * Use to add new fileds in the table |
||
337 | * |
||
338 | * @return bool true if success, false if an error occured |
||
339 | * |
||
340 | */ |
||
341 | View Code Duplication | public function addNewFields() |
|
358 | |||
359 | /** |
||
360 | * Use to update fields values |
||
361 | * |
||
362 | * @return bool true if success, false if an error occured |
||
363 | * |
||
364 | */ |
||
365 | View Code Duplication | public function updateFieldsValues() |
|
383 | |||
384 | /** |
||
385 | * Use to drop fields |
||
386 | * |
||
387 | * @return bool true if success, false if an error occured |
||
388 | * |
||
389 | */ |
||
390 | View Code Duplication | public function dropFields() |
|
409 | } |
||
410 | |||
535 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.