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 | |||
27 | /** |
||
28 | * Set the database object to use for accessing storage. |
||
29 | * |
||
30 | * @param DatabaseQueryBuilder $db as database access object. |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | 3 | public function setDb(DatabaseQueryBuilder $db) |
|
38 | |||
39 | |||
40 | |||
41 | /** |
||
42 | * Check if database is injected or throw an exception. |
||
43 | * |
||
44 | * @throws ActiveRecordException when database is not set. |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | 4 | protected function checkDb() |
|
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * Get essential object properties. |
||
59 | * |
||
60 | * @return array with object properties. |
||
61 | */ |
||
62 | 3 | protected function getProperties() |
|
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * Find and return first object found by search criteria and use |
||
75 | * its data to populate this instance. |
||
76 | * |
||
77 | * @param string $column to use in where statement. |
||
78 | * @param mixed $value to use in where statement. |
||
79 | * |
||
80 | * @return this |
||
81 | */ |
||
82 | 1 | public function find($column, $value) |
|
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Find and return all. |
||
97 | * |
||
98 | * @return array of object of this class |
||
99 | */ |
||
100 | 1 | public function findAll() |
|
109 | |||
110 | |||
111 | |||
112 | /** |
||
113 | * Find and return all matching a search criteria of |
||
114 | * for example `id = ?` or `id IN [?, ?]`. |
||
115 | * |
||
116 | * @param string $where to use in where statement. |
||
117 | * @param mixed $value to use in where statement. |
||
118 | * |
||
119 | * @return array of object of this class |
||
120 | */ |
||
121 | 1 | View Code Duplication | public function findAllWhere($where, $value) |
122 | { |
||
123 | 1 | $this->checkDb(); |
|
124 | 1 | $params = is_array($value) ? $value : [$value]; |
|
125 | 1 | return $this->db->connect() |
|
126 | 1 | ->select() |
|
127 | 1 | ->from($this->tableName) |
|
128 | 1 | ->where($where) |
|
129 | 1 | ->execute($params) |
|
130 | 1 | ->fetchAllClass(get_class($this)); |
|
131 | } |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * Save current object/row, insert if id is missing and do an |
||
137 | * update if the id exists. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 4 | public function save() |
|
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * Save current object/row, update with where. |
||
154 | * |
||
155 | * @param string $where to use in where statement. |
||
156 | * @param mixed $params to use in where statement. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function saveWhere($where, $param) |
||
164 | |||
165 | |||
166 | |||
167 | /** |
||
168 | * Create new row. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | 4 | View Code Duplication | protected function create() |
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * Update row. |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | View Code Duplication | protected function update() |
|
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * Update row where. |
||
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 | protected function updateWhere($where, $param) |
||
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * Delete row. |
||
240 | * |
||
241 | * @param integer $id to delete or use $this->id as default. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | public function delete($id = null) |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * Delete row where. |
||
262 | * |
||
263 | * @param string $where to use in where statement. |
||
264 | * @param mixed $value to use in where statement. |
||
265 | * |
||
266 | * @return void |
||
267 | */ |
||
268 | View Code Duplication | public function deleteWhere($where, $value) |
|
280 | } |
||
281 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: