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 | * 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 Itself. |
||
| 273 | */ |
||
| 274 | public function initQuery($queryType, $conditions = [], $fields = []) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Prepare DBObject for the SELECT SQL query. |
||
| 302 | * |
||
| 303 | * @param array $conditions List of the conditions fields |
||
| 304 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 305 | * |
||
| 306 | * @return DBObject Current object. |
||
| 307 | */ |
||
| 308 | public function select($conditions = []) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Static way to prepare DBObject for the SELECT SQL query. |
||
| 314 | * |
||
| 315 | * @param array $conditions List of the conditions fields |
||
| 316 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 317 | * |
||
| 318 | * @return DBObject Current object. |
||
| 319 | */ |
||
| 320 | public static function _select($conditions = []) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Select and returns DB record for current DBObject table by record ID. |
||
| 329 | * |
||
| 330 | * @param mixed $recordId Record ID. |
||
| 331 | * @param bool $debug Debug mode flag. |
||
| 332 | * |
||
| 333 | * @return DBObject Record object or null. |
||
| 334 | */ |
||
| 335 | public static function _get($recordId, $debug = false) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns result of the COUNT() SQL query. |
||
| 343 | * |
||
| 344 | * @param array $conditions Conditions list. |
||
| 345 | * @param type $debug Debug mode flag. |
||
| 346 | * |
||
| 347 | * @return int |
||
| 348 | */ |
||
| 349 | View Code Duplication | public static function _count($conditions = [], $debug = false) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Returns result of the MAX($field) SQL query. |
||
| 363 | * |
||
| 364 | * @param string $field Name of the field. |
||
| 365 | * @param array $conditions Conditions list. |
||
| 366 | * @param type $debug Debug mode flag. |
||
| 367 | * |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | View Code Duplication | public static function _max($field, $conditions = [], $debug = false) { |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Returns result of the MIN($field) SQL query. |
||
| 384 | * |
||
| 385 | * @param string $field Name of the field. |
||
| 386 | * @param array $conditions Conditions list. |
||
| 387 | * @param type $debug Debug mode flag. |
||
| 388 | * |
||
| 389 | * @return int |
||
| 390 | */ |
||
| 391 | View Code Duplication | public static function _min($field, $conditions = [], $debug = false) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Prepare DBObject for the UPDATE SQL query. |
||
| 405 | * |
||
| 406 | * @param type $fields List of fields to be updated |
||
| 407 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 408 | * @param array $conditions List of the conditions fields |
||
| 409 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 410 | * |
||
| 411 | * @return DBObject Current object. |
||
| 412 | */ |
||
| 413 | public function update($fields = [], $conditions = []) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Static way to prepare DBObject for the UPDATE SQL query. |
||
| 419 | * |
||
| 420 | * @param type $fields List of fields to be updated |
||
| 421 | * (fieldName => fieldValue or sqlAssignment => params). |
||
| 422 | * @param array $conditions List of the conditions fields |
||
| 423 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 424 | * |
||
| 425 | * @return DBObject Current object. |
||
| 426 | */ |
||
| 427 | public static function _update($fields = [], $conditions = []) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Prepare DBObject for the select query (for ORDER expression). |
||
| 436 | * |
||
| 437 | * @param array $order List of order conditions (fieldName => order), |
||
| 438 | * order may be 'ASC' OR 'DESC'. |
||
| 439 | * |
||
| 440 | * @param array $order |
||
| 441 | * @return DBObject Current object. |
||
| 442 | */ |
||
| 443 | public function order($order = null) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Prepare DBObject for the select query (for LIMIT expression). |
||
| 451 | * |
||
| 452 | * @param int $offset Limit offset value (or count if this is single |
||
| 453 | * parameter). |
||
| 454 | * @param int $count Number of records to select. |
||
| 455 | * |
||
| 456 | * @return DBObject Current object. |
||
| 457 | */ |
||
| 458 | public function limit($offset = 1, $count = null) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Selects DB record(s) for current DBObject table according to params. |
||
| 470 | * |
||
| 471 | * @param bool $debug Debug mode flag. |
||
| 472 | * |
||
| 473 | * @return mixed DBObject, array of DBObject or null. |
||
| 474 | * @throws DBCoreException If some DB or query syntax errors occurred. |
||
| 475 | */ |
||
| 476 | public function go($debug = false) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Deletes DB record for current DBObject. |
||
| 574 | * |
||
| 575 | * @return mixed Number of affected rows (1 if some record was deleted, |
||
| 576 | * 0 - if no) or FALSE if some error occurred. |
||
| 577 | */ |
||
| 578 | public function delete() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Deletes DB record by ID or condition. |
||
| 584 | * |
||
| 585 | * @param mixed $conditions List of the conditions fields |
||
| 586 | * (fieldName => fieldValue or sqlCondition => params). |
||
| 587 | * or ID value of the record |
||
| 588 | * @return DBObject Current object. |
||
| 589 | */ |
||
| 590 | public static function _delete($conditions = []) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Returns DB table field name by it's camelcase variant. |
||
| 610 | * |
||
| 611 | * @param string $methodNameFragment |
||
| 612 | * |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | protected function getFieldName($methodNameFragment) { |
||
| 618 | |||
| 619 | } |
||
| 620 |
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.