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 |
||
10 | class Table implements ArrayAccess |
||
11 | { |
||
12 | private $db; |
||
13 | private $row; |
||
14 | private $collection; |
||
15 | private $cache = []; |
||
16 | |||
17 | public $name; |
||
18 | public $fields = []; |
||
19 | |||
20 | /** |
||
21 | * Constructor. |
||
22 | * |
||
23 | * @param SimpleCrud $db |
||
24 | * @param string $name |
||
25 | */ |
||
26 | final public function __construct(SimpleCrud $db, $name) |
||
42 | |||
43 | /** |
||
44 | * Callback used to init the table. |
||
45 | */ |
||
46 | protected function init() |
||
49 | |||
50 | /** |
||
51 | * Store a row in the cache. |
||
52 | * |
||
53 | * @param int $id |
||
|
|||
54 | * @param Row $Row |
||
55 | */ |
||
56 | public function cache(Row $row) |
||
60 | |||
61 | /** |
||
62 | * Clear the current cache. |
||
63 | */ |
||
64 | public function clearCache() |
||
68 | |||
69 | /** |
||
70 | * Magic method to create queries related with this table. |
||
71 | * |
||
72 | * @param string $name |
||
73 | * @param array $arguments |
||
74 | * |
||
75 | * @throws SimpleCrudException |
||
76 | * |
||
77 | * @return Queries\Query|null |
||
78 | */ |
||
79 | public function __call($name, $arguments) |
||
83 | |||
84 | /** |
||
85 | * Check if a row with a specific id exists. |
||
86 | * |
||
87 | * @see ArrayAccess |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function offsetExists($offset) |
||
102 | |||
103 | /** |
||
104 | * Returns a row with a specific id. |
||
105 | * |
||
106 | * @see ArrayAccess |
||
107 | * |
||
108 | * @return Row|null |
||
109 | */ |
||
110 | public function offsetGet($offset) |
||
121 | |||
122 | /** |
||
123 | * Store a row with a specific id. |
||
124 | * |
||
125 | * @see ArrayAccess |
||
126 | */ |
||
127 | public function offsetSet($offset, $value) |
||
162 | |||
163 | /** |
||
164 | * Remove a row with a specific id. |
||
165 | * |
||
166 | * @see ArrayAccess |
||
167 | */ |
||
168 | public function offsetUnset($offset) |
||
177 | |||
178 | /** |
||
179 | * Returns the SimpleCrud instance associated with this table. |
||
180 | * |
||
181 | * @return SimpleCrud |
||
182 | */ |
||
183 | public function getDatabase() |
||
187 | |||
188 | /** |
||
189 | * Returns the table scheme. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function getScheme() |
||
197 | |||
198 | /** |
||
199 | * Returns an attribute. |
||
200 | * |
||
201 | * @param string $name |
||
202 | * |
||
203 | * @return null|mixed |
||
204 | */ |
||
205 | public function getAttribute($name) |
||
209 | |||
210 | /** |
||
211 | * Defines the Row class used by this table. |
||
212 | * |
||
213 | * @param Row $row |
||
214 | */ |
||
215 | public function setRow(Row $row) |
||
219 | |||
220 | /** |
||
221 | * Defines the RowCollection class used by this table. |
||
222 | * |
||
223 | * @param RowCollection $collection |
||
224 | */ |
||
225 | public function setCollection(RowCollection $collection) |
||
229 | |||
230 | /** |
||
231 | * Creates a new row instance. |
||
232 | * |
||
233 | * @param array $data The values of the row |
||
234 | * |
||
235 | * @return Row |
||
236 | */ |
||
237 | public function create(array $data = []) |
||
251 | |||
252 | /** |
||
253 | * Creates a new rowCollection instance. |
||
254 | * |
||
255 | * @param array $data Rows added to this collection |
||
256 | * |
||
257 | * @return RowCollection |
||
258 | */ |
||
259 | public function createCollection(array $data = []) |
||
269 | |||
270 | /** |
||
271 | * Default data converter/validator from database. |
||
272 | * |
||
273 | * @param array $data The values before insert to database |
||
274 | * @param bool $new True for inserts, false for updates |
||
275 | */ |
||
276 | public function dataToDatabase(array $data, $new) |
||
280 | |||
281 | /** |
||
282 | * Default data converter from database. |
||
283 | * |
||
284 | * @param array $data The database format values |
||
285 | */ |
||
286 | public function dataFromDatabase(array $data) |
||
290 | |||
291 | /** |
||
292 | * Prepares the data from the result of a database selection. |
||
293 | * |
||
294 | * @param array $data |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | public function createFromDatabase(array $data) |
||
319 | |||
320 | /** |
||
321 | * Prepares the data before save into database (used by update and insert). |
||
322 | * |
||
323 | * @param array $data |
||
324 | * @param bool $new |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | public function prepareDataToDatabase(array $data, $new) |
||
345 | } |
||
346 |
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.