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:
Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Base { |
||
11 | private $sError = ''; |
||
12 | private $sCronError = ''; |
||
13 | protected $table = ''; |
||
14 | private $values = array(), $types = ''; |
||
15 | |||
16 | public function getTableName() { |
||
19 | |||
20 | protected $debug; |
||
21 | public function setDebug($debug) { |
||
30 | |||
31 | public $log; |
||
32 | public function setLog($log) { |
||
35 | |||
36 | protected $mysqli; |
||
37 | public function setMysql($mysqli) { |
||
49 | /** |
||
50 | * @var Smarty |
||
51 | */ |
||
52 | var $smarty; |
||
53 | public function setSmarty($smarty) { |
||
65 | |||
66 | protected $aErrorCodes; |
||
67 | public function setErrorCodes(&$aErrorCodes) { |
||
121 | /** |
||
122 | * Additional information in error string for cronjobs logging |
||
123 | **/ |
||
124 | public function getCronError() { |
||
127 | |||
128 | /** |
||
129 | * Get error message from error code array |
||
130 | * @param errCode string Error code string |
||
131 | * @param optional string Optional addtitional error strings to append |
||
132 | * @retrun string Error Message |
||
133 | **/ |
||
134 | public function getErrorMsg($errCode='') { |
||
150 | |||
151 | /** |
||
152 | * Fetch count of all entries in table |
||
153 | * @param none |
||
154 | * @param data mixed Count or false |
||
155 | **/ |
||
156 | public function getCount() { |
||
163 | |||
164 | /** |
||
165 | * Fetch count of all entries in table filtered by a column/value |
||
166 | * @param none |
||
167 | * @param data mixed Count or false |
||
168 | **/ |
||
169 | public function getCountFiltered($column='id', $value=NULL, $type='i', $operator = '=') { |
||
176 | |||
177 | /** |
||
178 | * Fetch all entries as an assoc array from a table |
||
179 | * This should, in general, not be used but sometimes it's just easier |
||
180 | * @param none |
||
181 | * @return array Assoc array of all rows found in table |
||
182 | **/ |
||
183 | View Code Duplication | public function getAllAssoc() { |
|
190 | |||
191 | /** |
||
192 | * Get a single row as an assoc array |
||
193 | * @param value string Value to search for |
||
194 | * @param field string Column to search for |
||
195 | * @param type string Type of value |
||
196 | * @return array Resulting row |
||
197 | **/ |
||
198 | protected function getSingleAssoc($value, $field='id', $type='i') { |
||
205 | |||
206 | /** |
||
207 | * Get a single value from a row matching the query specified |
||
208 | * @param value string Value to search for |
||
209 | * @param search Return column to search for |
||
210 | * @param field string Search column |
||
211 | * @param type string Type of value |
||
212 | * @param lower bool try with LOWER comparision |
||
213 | * @return array Return result |
||
214 | **/ |
||
215 | protected function getSingle($value, $search='id', $field='id', $type="i", $lower=false) { |
||
231 | |||
232 | /** |
||
233 | * Check if the prepared statement is valid |
||
234 | * @param $bState Statement return value |
||
235 | * @return bool true or false |
||
236 | **/ |
||
237 | function checkStmt($bState) { |
||
243 | |||
244 | /** |
||
245 | * Catch SQL errors with this method |
||
246 | * @param error_code string Error code to read |
||
247 | **/ |
||
248 | protected function sqlError($error_code='E0020') { |
||
261 | |||
262 | /** |
||
263 | * @param userID int Account ID |
||
264 | * Update a single row in a table |
||
265 | * @param field string Field to update |
||
266 | * @return bool |
||
267 | **/ |
||
268 | protected function updateSingle($id, $field, $table='') { |
||
277 | |||
278 | /** |
||
279 | * We may need to generate our bind_param list |
||
280 | **/ |
||
281 | public function addParam($type, &$value) { |
||
285 | public function getParam() { |
||
286 | $array = array_merge(array($this->types), $this->values); |
||
287 | // Clear the data |
||
288 | $this->values = NULL; |
||
289 | $this->types = NULL; |
||
290 | // See here why we need this: http://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given |
||
291 | if (strnatcmp(phpversion(),'5.3') >= 0) { |
||
292 | $refs = array(); |
||
293 | foreach($array as $key => $value) |
||
294 | $refs[$key] = &$array[$key]; |
||
295 | return $refs; |
||
296 | } |
||
297 | return $array; |
||
298 | } |
||
299 | } |
||
300 |
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: