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') { |
||
260 | |||
261 | /** |
||
262 | * @param userID int Account ID |
||
263 | * Update a single row in a table |
||
264 | * @param field string Field to update |
||
265 | * @return bool |
||
266 | **/ |
||
267 | protected function updateSingle($id, $field, $table='') { |
||
276 | |||
277 | /** |
||
278 | * We may need to generate our bind_param list |
||
279 | **/ |
||
280 | public function addParam($type, &$value) { |
||
284 | public function getParam() { |
||
285 | $array = array_merge(array($this->types), $this->values); |
||
286 | // Clear the data |
||
287 | $this->values = NULL; |
||
288 | $this->types = NULL; |
||
289 | // See here why we need this: http://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given |
||
290 | if (strnatcmp(phpversion(),'5.3') >= 0) { |
||
291 | $refs = array(); |
||
292 | foreach($array as $key => $value) |
||
293 | $refs[$key] = &$array[$key]; |
||
294 | return $refs; |
||
295 | } |
||
296 | return $array; |
||
297 | } |
||
298 | } |
||
299 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.