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 |
||
14 | class ActiveRecordModel |
||
15 | { |
||
16 | /** |
||
17 | * @var DatabaseQueryBuilder $db the object for persistent |
||
18 | * storage. |
||
19 | */ |
||
20 | protected $db = null; |
||
21 | |||
22 | /** |
||
23 | * @var string $tableName name of the database table. |
||
24 | */ |
||
25 | protected $tableName = null; |
||
26 | |||
27 | |||
28 | |||
29 | /** |
||
30 | * Set the database object to use for accessing storage. |
||
31 | * |
||
32 | * @param DatabaseQueryBuilder $db as database access object. |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function setDb(DatabaseQueryBuilder $db) |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * Check if database is injected or throw an exception. |
||
45 | * |
||
46 | * @throws ActiveRecordException when database is not set. |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | protected function checkDb() |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Get essential object properties. |
||
61 | * |
||
62 | * @return array with object properties. |
||
63 | */ |
||
64 | protected function getProperties() |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * Find and return first object found by search criteria and use |
||
77 | * its data to populate this instance. |
||
78 | * |
||
79 | * @param string $column to use in where statement. |
||
80 | * @param mixed $value to use in where statement. |
||
81 | * |
||
82 | * @return this |
||
83 | */ |
||
84 | public function find($column, $value) |
||
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * Find and return first object by its tableIdColumn and use |
||
93 | * its data to populate this instance. |
||
94 | * |
||
95 | * @param integer $id to find or use $this->{$this->tableIdColumn} |
||
96 | * as default. |
||
97 | * |
||
98 | * @return this |
||
99 | */ |
||
100 | public function findById($id = null) |
||
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * Find and return first object found by search criteria and use |
||
110 | * its data to populate this instance. |
||
111 | * |
||
112 | * The search criteria `$where` of can be set up like this: |
||
113 | * `id = ?` |
||
114 | * `id1 = ? and id2 = ?` |
||
115 | * |
||
116 | * The `$value` can be a single value or an array of values. |
||
117 | * |
||
118 | * @param string $where to use in where statement. |
||
119 | * @param mixed $value to use in where statement. |
||
120 | * |
||
121 | * @return this |
||
122 | */ |
||
123 | View Code Duplication | public function findWhere($where, $value) |
|
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * Find and return all. |
||
139 | * |
||
140 | * @return array of object of this class |
||
141 | */ |
||
142 | public function findAll() |
||
151 | |||
152 | |||
153 | |||
154 | View Code Duplication | public function findAllLimitOrderBy($order, $number) |
|
165 | |||
166 | View Code Duplication | public function findAllLimit($number) |
|
176 | |||
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * Find and return all matching the search criteria. |
||
182 | * |
||
183 | * The search criteria `$where` of can be set up like this: |
||
184 | * `id = ?` |
||
185 | * `id IN [?, ?]` |
||
186 | * |
||
187 | * The `$value` can be a single value or an array of values. |
||
188 | * |
||
189 | * @param string $where to use in where statement. |
||
190 | * @param mixed $value to use in where statement. |
||
191 | * |
||
192 | * @return array of object of this class |
||
193 | */ |
||
194 | View Code Duplication | public function findAllWhere($where, $value) |
|
205 | |||
206 | |||
207 | /** |
||
208 | * Execute rawsql |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public function findAllSql($sql, $params = []) |
||
219 | |||
220 | |||
221 | public function next() |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Execute rawsql |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function findAllSqlTest($sql, $params) |
||
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * Save current object/row, insert if id is missing and do an |
||
243 | * update if the id exists. |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | public function save($idName = null, $id = null) |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * Create new row. |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | protected function create() |
||
279 | |||
280 | |||
281 | |||
282 | /** |
||
283 | * Update row. |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | protected function update($idName = null, $id = null) |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * Delete row. |
||
307 | * |
||
308 | * @param integer $id to delete or use $this->id as default. |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | public function delete($idName = null, $id = null) |
||
325 | |||
326 | |||
327 | public function lastInsertId() |
||
331 | } |
||
332 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: