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 | protected $properties = []; |
||
| 26 | protected $output; |
||
| 27 | |||
| 28 | public function __set($property, $value) |
||
| 32 | |||
| 33 | public function __get($property) |
||
| 37 | |||
| 38 | public function getProperties() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * connect Setup database connection |
||
| 45 | */ |
||
| 46 | protected static function connect() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * stripclassName() |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public static function stripclassName() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the table name if defined in the model |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function tableName() |
||
| 73 | /** |
||
| 74 | * getClassName() |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getClassName() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * getTableName() |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | protected function getTableName($connection) |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * sanitize(argument) Removes unwanted characters |
||
| 96 | * |
||
| 97 | * @param $value |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | protected static function sanitize($value) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * checkConnection |
||
| 110 | * |
||
| 111 | * @param $con |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | protected static function checkConnection($con) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * checkTableExist Check if table already in the database |
||
| 122 | * |
||
| 123 | * @param $tablename |
||
| 124 | * @param $con |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function checkTableExist($table, $con=NULL) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * checkTableName Return the table name |
||
| 140 | * |
||
| 141 | * @param $tablename |
||
| 142 | * @param $con |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | View Code Duplication | protected static function checkTableName($tableName, $con=NULL) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * checkColumn Check if column exist in table |
||
| 160 | * |
||
| 161 | * @param $tableName |
||
| 162 | * @param $columnName |
||
| 163 | * @param $con |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | protected static function checkColumn($tableName, $columnName, $con=NULL) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * buildColumn Build the column name |
||
| 182 | * |
||
| 183 | * @param $data |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | View Code Duplication | protected static function buildColumn($data) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * buildValues Build the column values |
||
| 205 | * |
||
| 206 | * @param $data |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | View Code Duplication | protected static function buildValues($data) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * buildClause Build the clause value |
||
| 228 | * |
||
| 229 | * @param $data |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | View Code Duplication | protected static function buildClause($tableName, $data) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get the fields to be fillables defined in the model |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function fields() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * selectAllQuery |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function selectAllQuery($tableName) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * whereAndClause |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public static function whereAndClause($tableName, $data, $condition) |
||
| 306 | |||
| 307 | View Code Duplication | public static function whereLikeClause($tableName, $data, $condition) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * selectQuery |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public static function selectQuery($tableName, $fields, $data, $condition, $connection) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * insertQuery |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function insertQuery($tableName) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * updateQuery |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function updateQuery($tableName, $id) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * query($query, $dbCOnnection) |
||
| 388 | * Raw sql query |
||
| 389 | * |
||
| 390 | * @return object |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function query($query, $con = NULL) |
|
| 404 | } |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.