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 DBObject 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 DBObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class DBObject extends \Asymptix\core\Object { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Status constants. |
||
| 21 | */ |
||
| 22 | const STATUS_ACTIVATED = 1; |
||
| 23 | const STATUS_DEACTIVATED = 0; |
||
| 24 | |||
| 25 | const STATUS_REMOVED = 1; |
||
| 26 | const STATUS_RESTORED = 0; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * DB Query object for Prepared Statement. |
||
| 30 | * |
||
| 31 | * @var DBPreparedQuery |
||
| 32 | */ |
||
| 33 | private $dbQuery = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Creates new default object. |
||
| 37 | */ |
||
| 38 | public function __construct() {} |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns primary key value. |
||
| 42 | * |
||
| 43 | * @return mixed. |
||
|
|
|||
| 44 | */ |
||
| 45 | public function getId() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Sets primary key value. |
||
| 55 | * |
||
| 56 | * @param mixed $recordId Key vaue. |
||
| 57 | * |
||
| 58 | * @return bool Success flag. |
||
| 59 | * @throws DBCoreException If object has no field with such name. |
||
| 60 | */ |
||
| 61 | public function setId($recordId) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns name of the primary key field. |
||
| 67 | * |
||
| 68 | * @return mixed |
||
| 69 | */ |
||
| 70 | public static function getIdFieldName() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns DBObject table name. |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public static function getTableName() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Saves activation flag to the database. |
||
| 85 | * |
||
| 86 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 87 | * the last query failed. |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function saveActivationFlag() { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Detects if current record is activated. |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | * |
||
| 105 | * @throws DBCoreException If record hos no 'activation' field. |
||
| 106 | */ |
||
| 107 | public function isActivated() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Activates record and save changes into the database. |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | public function activate() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Deactivates record and save changes into the database. |
||
| 129 | * |
||
| 130 | * @return int |
||
| 131 | */ |
||
| 132 | public function deactivate() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Changes record activation flag and save changes into the database. |
||
| 140 | */ |
||
| 141 | public function changeActivation() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Saves removement flag to the database. |
||
| 151 | * |
||
| 152 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 153 | * the last query failed. |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function saveRemovementFlag() { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Detects if current record is removed. |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | * |
||
| 171 | * @throws DBCoreException If record hos no 'removed' field. |
||
| 172 | */ |
||
| 173 | public function isRemoved() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Enable removed flag of the record and save changes into the database. |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public function remove() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Disable removed flag of the record and save changes into the database. |
||
| 195 | * |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | public function restore() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Changes record removement flag and save changes into the database. |
||
| 206 | */ |
||
| 207 | public function changeRemovement() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Detects if current DBObject represents not existed DB record. |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isNewRecord() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Saves DBObject to the database. If this is a new object - INSERT SQL |
||
| 230 | * instruction executes, if existed one - UPDATE. |
||
| 231 | * |
||
| 232 | * @param bool $debug Debug mode flag. |
||
| 233 | * |
||
| 234 | * @return mixed Primary key value. |
||
| 235 | * @throws DBCoreException If some database error occurred. |
||
| 236 | */ |
||
| 237 | public function save($debug = false) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Inits SQL query. |
||
| 254 | * |
||
| 255 | * @param string $queryType Type of the SQL query from types list from DBQuery. |
||
| 256 | * @param array $conditions List of conditions for WHERE instruction. |
||
| 257 | * @param array $fields List of fields for INSERT or UPDATE types of SQL queries. |
||
| 258 | * |
||
| 259 | * @return DBObject Itself. |
||
| 260 | */ |
||
| 261 | public function initQuery($queryType, $conditions = [], $fields = []) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Prepare DBObject for the SELECT SQL query. |
||
| 289 | * |
||
| 290 | * @param array $conditions List of the conditions fields |
||
| 291 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 292 | * |
||
| 293 | * @return DBObject Current object. |
||
| 294 | */ |
||
| 295 | public function select($conditions = []) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Static way to prepare DBObject for the SELECT SQL query. |
||
| 301 | * |
||
| 302 | * @param array $conditions List of the conditions fields |
||
| 303 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 304 | * |
||
| 305 | * @return DBObject Current object. |
||
| 306 | */ |
||
| 307 | public static function _select($conditions = []) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Select and returns DB record for current DBObject table by record ID. |
||
| 316 | * |
||
| 317 | * @param mixed $recordId Record ID. |
||
| 318 | * @param bool $debug Debug mode flag. |
||
| 319 | * |
||
| 320 | * @return DBObject Record object or null. |
||
| 321 | */ |
||
| 322 | public static function _get($recordId, $debug = false) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Prepare DBObject for the UPDATE SQL query. |
||
| 330 | * |
||
| 331 | * @param type $fields List of fields to be updated |
||
| 332 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 333 | * @param array $conditions List of the conditions fields |
||
| 334 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 335 | * |
||
| 336 | * @return DBObject Current object. |
||
| 337 | */ |
||
| 338 | public function update($fields = [], $conditions = []) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Static way to prepare DBObject for the UPDATE SQL query. |
||
| 344 | * |
||
| 345 | * @param type $fields List of fields to be updated |
||
| 346 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 347 | * @param array $conditions List of the conditions fields |
||
| 348 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 349 | * |
||
| 350 | * @return DBObject Current object. |
||
| 351 | */ |
||
| 352 | public static function _update($fields = [], $conditions = []) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Prepare DBObject for the select query (for ORDER expression). |
||
| 361 | * |
||
| 362 | * @param array $order List of order conditions (fieldName => order), |
||
| 363 | * order may be 'ASC' OR 'DESC'. |
||
| 364 | * |
||
| 365 | * @param array $order |
||
| 366 | * @return DBObject Current object. |
||
| 367 | */ |
||
| 368 | public function order($order = null) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Prepare DBObject for the select query (for LIMIT expression). |
||
| 376 | * |
||
| 377 | * @param int $offset Limit offset value (or count if this is single |
||
| 378 | * parameter). |
||
| 379 | * @param int $count Number of records to select. |
||
| 380 | * |
||
| 381 | * @return DBObject Current object. |
||
| 382 | */ |
||
| 383 | public function limit($offset = 1, $count = null) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Selects DB record(s) for current DBObject table according to params. |
||
| 395 | * |
||
| 396 | * @param bool $debug Debug mode flag. |
||
| 397 | * |
||
| 398 | * @return mixed DBObject, array of DBObject or null. |
||
| 399 | * @throws DBCoreException If some DB or query syntax errors occurred. |
||
| 400 | */ |
||
| 401 | public function go($debug = false) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Deletes DB record for current DBObject. |
||
| 503 | * |
||
| 504 | * @return mixed Number of affected rows (1 if some record was deleted, |
||
| 505 | * 0 - if no) or FALSE if some error occurred. |
||
| 506 | */ |
||
| 507 | public function delete() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Deletes DB record by ID or condition. |
||
| 513 | * |
||
| 514 | * @param mixed $conditions List of the conditions fields |
||
| 515 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 516 | * or ID value of the record |
||
| 517 | * @return DBObject Current object. |
||
| 518 | */ |
||
| 519 | public static function _delete($conditions = []) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns DB table field name by it's camelcase variant. |
||
| 539 | * |
||
| 540 | * @param string $methodNameFragment |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | protected function getFieldName($methodNameFragment) { |
||
| 547 | |||
| 548 | } |
||
| 549 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.