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 |
||
12 | class ActiveRecordModel |
||
13 | { |
||
14 | /** |
||
15 | * @var DatabaseQueryBuilder $db the object for persistent |
||
16 | * storage. |
||
17 | */ |
||
18 | protected $db = null; |
||
19 | |||
20 | /** |
||
21 | * @var string $tableName name of the database table. |
||
22 | */ |
||
23 | protected $tableName = null; |
||
24 | |||
25 | /** |
||
26 | * @var string $tableIdColumn name of the id column in the database table. |
||
27 | */ |
||
28 | protected $tableIdColumn = "id"; |
||
29 | |||
30 | |||
31 | |||
32 | /** |
||
33 | * Set the database object to use for accessing storage. |
||
34 | * |
||
35 | * @param DatabaseQueryBuilder $db as database access object. |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | 5 | public function setDb(DatabaseQueryBuilder $db) |
|
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * Check if database is injected or throw an exception. |
||
48 | * |
||
49 | * @throws ActiveRecordException when database is not set. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | 6 | protected function checkDb() |
|
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * Get essential object properties. |
||
64 | * |
||
65 | * @return array with object properties. |
||
66 | */ |
||
67 | 5 | protected function getProperties() |
|
78 | |||
79 | |||
80 | |||
81 | /** |
||
82 | * Find and return first object found by search criteria and use |
||
83 | * its data to populate this instance. |
||
84 | * |
||
85 | * @param string $column to use in where statement. |
||
86 | * @param mixed $value to use in where statement. |
||
87 | * |
||
88 | * @return this |
||
89 | */ |
||
90 | 1 | public function find($column, $value) |
|
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * Find and return first object by its tableIdColumn and use |
||
99 | * its data to populate this instance. |
||
100 | * |
||
101 | * @param string $column to use in where statement. |
||
|
|||
102 | * @param mixed $value to use in where statement. |
||
103 | * |
||
104 | * @return this |
||
105 | */ |
||
106 | 3 | public function findById($value) |
|
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * Find and return first object found by search criteria and use |
||
115 | * its data to populate this instance. |
||
116 | * |
||
117 | * The search criteria `$where` of can be set up like this: |
||
118 | * `id = ?` |
||
119 | * `id1 = ? and id2 = ?` |
||
120 | * |
||
121 | * The `$value` can be a single value or an array of values. |
||
122 | * |
||
123 | * @param string $where to use in where statement. |
||
124 | * @param mixed $value to use in where statement. |
||
125 | * |
||
126 | * @return this |
||
127 | */ |
||
128 | 3 | View Code Duplication | public function findWhere($where, $value) |
139 | |||
140 | |||
141 | |||
142 | /** |
||
143 | * Find and return all. |
||
144 | * |
||
145 | * @return array of object of this class |
||
146 | */ |
||
147 | 1 | public function findAll() |
|
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * Find and return all matching the search criteria. |
||
161 | * |
||
162 | * The search criteria `$where` of can be set up like this: |
||
163 | * `id = ?` |
||
164 | * `id IN [?, ?]` |
||
165 | * |
||
166 | * The `$value` can be a single value or an array of values. |
||
167 | * |
||
168 | * @param string $where to use in where statement. |
||
169 | * @param mixed $value to use in where statement. |
||
170 | * |
||
171 | * @return array of object of this class |
||
172 | */ |
||
173 | 1 | View Code Duplication | public function findAllWhere($where, $value) |
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Save current object/row, insert if id is missing and do an |
||
189 | * update if the id exists. |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | 6 | public function save() |
|
201 | |||
202 | |||
203 | |||
204 | /** |
||
205 | * Create new row. |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | 6 | View Code Duplication | protected function create() |
223 | |||
224 | |||
225 | |||
226 | /** |
||
227 | * Update row. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | 2 | View Code Duplication | protected function update() |
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * Delete row. |
||
250 | * |
||
251 | * @param integer $id to delete or use $this->{$this->tableIdColumn} as default. |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | 1 | public function delete($id = null) |
|
267 | } |
||
268 |
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.