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\BasicObject { |
||
| 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 | * Inserts DBObject to the database. |
||
| 254 | * |
||
| 255 | * @param bool $ignore Ignore unique indexes or not. |
||
| 256 | * @param bool Debug mode flag. |
||
| 257 | * |
||
| 258 | * @return mixed Primary key value. |
||
| 259 | * @throws DBCoreException If some database error occurred. |
||
| 260 | */ |
||
| 261 | public function insert($ignore = false, $debug = false) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Inits SQL query. |
||
| 267 | * |
||
| 268 | * @param string $queryType Type of the SQL query from types list from DBQuery. |
||
| 269 | * @param array $conditions List of conditions for WHERE instruction. |
||
| 270 | * @param array $fields List of fields for INSERT or UPDATE types of SQL queries. |
||
| 271 | * |
||
| 272 | * @return DBObject Oneself. |
||
| 273 | * @throws DBCoreException If some error occurred. |
||
| 274 | */ |
||
| 275 | public function initQuery($queryType, $conditions = [], $fields = []) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Prepare DBObject for the SELECT SQL query. |
||
| 311 | * |
||
| 312 | * @param array $conditions List of the conditions fields |
||
| 313 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 314 | * |
||
| 315 | * @return DBObject Current object. |
||
| 316 | */ |
||
| 317 | public function select($conditions = []) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Static way to prepare DBObject for the SELECT SQL query. |
||
| 323 | * |
||
| 324 | * @param array $conditions List of the conditions fields |
||
| 325 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 326 | * |
||
| 327 | * @return DBObject Current object. |
||
| 328 | */ |
||
| 329 | public static function _select($conditions = []) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Select and returns DB record for current DBObject table by record ID. |
||
| 338 | * |
||
| 339 | * @param mixed $recordId Record ID. |
||
| 340 | * @param bool $debug Debug mode flag. |
||
| 341 | * |
||
| 342 | * @return DBObject Record object or null. |
||
| 343 | */ |
||
| 344 | public static function _get($recordId, $debug = false) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns result of the COUNT() SQL query. |
||
| 352 | * |
||
| 353 | * @param array $conditions Conditions list. |
||
| 354 | * @param type $debug Debug mode flag. |
||
| 355 | * |
||
| 356 | * @return int |
||
| 357 | */ |
||
| 358 | View Code Duplication | public static function _count($conditions = [], $debug = false) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Returns result of the MAX($field) SQL query. |
||
| 372 | * |
||
| 373 | * @param string $field Name of the field. |
||
| 374 | * @param array $conditions Conditions list. |
||
| 375 | * @param type $debug Debug mode flag. |
||
| 376 | * |
||
| 377 | * @return int |
||
| 378 | */ |
||
| 379 | View Code Duplication | public static function _max($field, $conditions = [], $debug = false) { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Returns result of the MIN($field) SQL query. |
||
| 393 | * |
||
| 394 | * @param string $field Name of the field. |
||
| 395 | * @param array $conditions Conditions list. |
||
| 396 | * @param type $debug Debug mode flag. |
||
| 397 | * |
||
| 398 | * @return int |
||
| 399 | */ |
||
| 400 | View Code Duplication | public static function _min($field, $conditions = [], $debug = false) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Prepare DBObject for the UPDATE SQL query. |
||
| 414 | * |
||
| 415 | * @param type $fields List of fields to be updated |
||
| 416 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 417 | * @param array $conditions List of the conditions fields |
||
| 418 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 419 | * |
||
| 420 | * @return DBObject Current object. |
||
| 421 | */ |
||
| 422 | public function update($fields = [], $conditions = []) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Static way to prepare DBObject for the UPDATE SQL query. |
||
| 428 | * |
||
| 429 | * @param type $fields List of fields to be updated |
||
| 430 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 431 | * @param array $conditions List of the conditions fields |
||
| 432 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 433 | * |
||
| 434 | * @return DBObject Current object. |
||
| 435 | */ |
||
| 436 | public static function _update($fields = [], $conditions = []) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Prepare DBObject for the select query (for ORDER expression). |
||
| 445 | * |
||
| 446 | * @param array $order List of order conditions (fieldName => order), |
||
| 447 | * order may be 'ASC' OR 'DESC'. |
||
| 448 | * |
||
| 449 | * @param array $order |
||
| 450 | * @return DBObject Current object. |
||
| 451 | */ |
||
| 452 | public function order($order = null) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Prepare DBObject for the select query (for LIMIT expression). |
||
| 460 | * |
||
| 461 | * @param int $offset Limit offset value (or count if this is single |
||
| 462 | * parameter). |
||
| 463 | * @param int $count Number of records to select. |
||
| 464 | * |
||
| 465 | * @return DBObject Current object. |
||
| 466 | */ |
||
| 467 | public function limit($offset = 1, $count = null) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Selects DB record(s) for current DBObject table according to params. |
||
| 483 | * |
||
| 484 | * @param bool $debug Debug mode flag. |
||
| 485 | * |
||
| 486 | * @return mixed DBObject, array of DBObject or null. |
||
| 487 | * @throws DBCoreException If some DB or query syntax errors occurred. |
||
| 488 | */ |
||
| 489 | public function go($debug = false) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Deletes DB record for current DBObject. |
||
| 559 | * |
||
| 560 | * @return mixed Number of affected rows (1 if some record was deleted, |
||
| 561 | * 0 - if no) or FALSE if some error occurred. |
||
| 562 | */ |
||
| 563 | public function delete() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Deletes DB record by ID or condition. |
||
| 569 | * |
||
| 570 | * @param mixed $conditions List of the conditions fields |
||
| 571 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 572 | * or ID value of the record |
||
| 573 | * @return DBObject Current object. |
||
| 574 | */ |
||
| 575 | public static function _delete($conditions = []) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns DB table field name by it's camelcase variant. |
||
| 595 | * |
||
| 596 | * @param string $methodNameFragment |
||
| 597 | * |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | protected function getFieldName($methodNameFragment) { |
||
| 603 | |||
| 604 | } |
||
| 605 |
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.