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 | 6 | 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 | 7 | protected function checkDb() |
|
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * Get essential object properties. |
||
64 | * |
||
65 | * @return array with object properties. |
||
66 | */ |
||
67 | 6 | 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 integer $id to find or use $this->{$this->tableIdColumn} |
||
102 | * as default. |
||
103 | * |
||
104 | * @return this |
||
105 | */ |
||
106 | 4 | public function findById($id = null) |
|
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * Find and return first object found by search criteria and use |
||
116 | * its data to populate this instance. |
||
117 | * |
||
118 | * The search criteria `$where` of can be set up like this: |
||
119 | * `id = ?` |
||
120 | * `id1 = ? and id2 = ?` |
||
121 | * |
||
122 | * The `$value` can be a single value or an array of values. |
||
123 | * |
||
124 | * @param string $where to use in where statement. |
||
125 | * @param mixed $value to use in where statement. |
||
126 | * |
||
127 | * @return this |
||
128 | */ |
||
129 | 4 | View Code Duplication | public function findWhere($where, $value) |
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * Find and return all. |
||
145 | * |
||
146 | * @return array of object of this class |
||
147 | */ |
||
148 | 1 | public function findAll() |
|
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * Find and return all matching the search criteria. |
||
162 | * |
||
163 | * The search criteria `$where` of can be set up like this: |
||
164 | * `id = ?` |
||
165 | * `id IN [?, ?]` |
||
166 | * |
||
167 | * The `$value` can be a single value or an array of values. |
||
168 | * |
||
169 | * @param string $where to use in where statement. |
||
170 | * @param mixed $value to use in where statement. |
||
171 | * |
||
172 | * @return array of object of this class |
||
173 | */ |
||
174 | 1 | View Code Duplication | public function findAllWhere($where, $value) |
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * Save current object/row, insert if id is missing and do an |
||
190 | * update if the id exists. |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 7 | public function save() |
|
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * Save/update current object/row using a custom where-statement. |
||
207 | * |
||
208 | * The criteria `$where` of can be set up like this: |
||
209 | * `id = ?` |
||
210 | * `id1 = ? AND id2 = ?` |
||
211 | * |
||
212 | * The `$value` can be a single value or an array of values. |
||
213 | * |
||
214 | * @param string $where to use in where statement. |
||
215 | * @param mixed $value to use in where statement. |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | 1 | public function saveWhere($where, $value) |
|
223 | |||
224 | |||
225 | |||
226 | /** |
||
227 | * Create new row. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | 7 | View Code Duplication | protected function create() |
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * Update row using $tableIdColumn as where. |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | 2 | View Code Duplication | protected function update() |
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * Update row using a custom where-statement. |
||
272 | * |
||
273 | * The criteria `$where` of can be set up like this: |
||
274 | * `id = ?` |
||
275 | * `id1 = ? AND id2 = ?` |
||
276 | * `id IN (?, ?)` |
||
277 | * |
||
278 | * The `$value` can be a single value or an array of values. |
||
279 | * |
||
280 | * @param string $where to use in where statement. |
||
281 | * @param mixed $value to use in where statement. |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | 1 | protected function updateWhere($where, $value) |
|
301 | |||
302 | |||
303 | |||
304 | /** |
||
305 | * Update row using $tableIdColumn as where and clear value of |
||
306 | * `$tableIdColumn`. |
||
307 | * |
||
308 | * @param integer $id to delete or use $this->{$this->tableIdColumn} |
||
309 | * as default. |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | 2 | public function delete($id = null) |
|
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * Delete row using a custom where-statement and leave value of |
||
330 | * `$tableIdColumn` as it is. |
||
331 | * |
||
332 | * The criteria `$where` of can be set up like this: |
||
333 | * `id = ?` |
||
334 | * `id1 = ? AND id2 = ?` |
||
335 | * `id IN (?, ?)` |
||
336 | * |
||
337 | * The `$value` can be a single value or an array of values. |
||
338 | * |
||
339 | * @param string $where to use in where statement. |
||
340 | * @param mixed $value to use in where statement. |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | 1 | View Code Duplication | public function deleteWhere($where, $value) |
354 | } |
||
355 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.