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:
| 1 | <?php |
||
| 16 | class DBTimedObject extends DBObject { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Changes record creation time and user Id. |
||
| 20 | * |
||
| 21 | * @global DBObject $_USER User object. |
||
| 22 | */ |
||
| 23 | private function changeCreateTime() { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Changes record updating time and user Id. |
||
| 32 | * |
||
| 33 | * @global DBObject $_USER User object. |
||
| 34 | */ |
||
| 35 | private function changeUpdateTime() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Save record to the database. |
||
| 44 | * |
||
| 45 | * @param bool $debug Debug mode flag. |
||
| 46 | * |
||
| 47 | * @return int Id of the record. |
||
| 48 | */ |
||
| 49 | public function save($debug = false) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Saves activation flag to the database. |
||
| 61 | * |
||
| 62 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 63 | * the last query failed. |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function saveActivationFlag() { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Saves removement flag to the database. |
||
| 82 | * |
||
| 83 | * @return int Returns the number of affected rows on success, and -1 if |
||
| 84 | * the last query failed. |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function saveRemovementFlag() { |
|
| 100 | |||
| 101 | } |
||
| 102 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state