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 DatabaseQuery 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 DatabaseQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class DatabaseQuery implements DatabaseQueryInterface |
||
| 21 | { |
||
| 22 | //Inject the inflector trait, file upload trait |
||
| 23 | use Inflector, Upload; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * connect Setup database connection |
||
| 27 | */ |
||
| 28 | protected static function connect() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * stripclassName() |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public static function stripclassName() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the table name if defined in the model |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public function tableName() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get the fields to be fillables defined in the model |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function fields() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * getClassName() |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getClassName() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * getTableName() |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | protected function getTableName($connection) |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * sanitize(argument) Removes unwanted characters |
||
| 95 | * |
||
| 96 | * @param $value |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | protected static function sanitize($value) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * checkConnection |
||
| 109 | * |
||
| 110 | * @param $con |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | protected static function checkConnection($con) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * checkTableExist Check if table already in the database |
||
| 121 | * |
||
| 122 | * @param $tablename |
||
| 123 | * @param $con |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function checkTableExist($table, $con=NULL) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * checkTableName Return the table name |
||
| 139 | * |
||
| 140 | * @param $tablename |
||
| 141 | * @param $con |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | View Code Duplication | protected static function checkTableName($tableName, $con=NULL) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * checkColumn Check if column exist in table |
||
| 159 | * |
||
| 160 | * @param $tableName |
||
| 161 | * @param $columnName |
||
| 162 | * @param $con |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected static function checkColumn($tableName, $columnName, $con=NULL) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the variables declared in the Model |
||
| 181 | * |
||
| 182 | * @return Array |
||
| 183 | */ |
||
| 184 | protected static function getParentClassVar() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the difference in variables between model and column definition |
||
| 191 | * |
||
| 192 | * @param $getClassVars |
||
| 193 | * |
||
| 194 | * @return Array |
||
| 195 | */ |
||
| 196 | protected static function getColumns($getClassVars) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * buildColumn Build the column name |
||
| 203 | * |
||
| 204 | * @param $data |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected static function buildColumn($getClassVars) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * buildValues Build the column values |
||
| 228 | * |
||
| 229 | * @param $data |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | View Code Duplication | protected static function buildValues($getClassVars) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * buildClause Build the clause value |
||
| 252 | * |
||
| 253 | * @param $data |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | View Code Duplication | protected static function buildClause($tableName, $data) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * selectAllQuery |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function selectAllQuery($tableName, $field) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * whereAndClause |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public static function whereAndClause($tableName, $data, $condition) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * selectQuery |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public static function selectQuery($tableName, $fields, $data, $condition, $connection) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * insertQuery |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function insertQuery($tableName) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * updateQuery |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function updateQuery($tableName) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * query($query, $dbCOnnection) |
||
| 376 | * Raw sql query |
||
| 377 | * |
||
| 378 | * @return object |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function query($query, $con = NULL) |
|
| 392 | } |
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: