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 |
||
| 10 | class Theme_Options_Datastore extends Key_Value_Datastore { |
||
| 11 | /** |
||
| 12 | * Initialization tasks. |
||
| 13 | **/ |
||
| 14 | public function init() {} |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Get a raw database query results array for a field |
||
| 18 | * |
||
| 19 | * @param Field $field The field to retrieve value for. |
||
| 20 | * @param array $storage_key_patterns |
||
| 21 | * @return array<stdClass> Array of {key, value} objects |
||
| 22 | */ |
||
| 23 | protected function get_storage_array( Field $field, $storage_key_patterns ) { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Save a single key-value pair to the database |
||
| 42 | * |
||
| 43 | * @param string $key |
||
| 44 | * @param string $value |
||
| 45 | */ |
||
| 46 | protected function save_key_value_pair( $key, $value ) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Save a single key-value pair to the database with autoload |
||
| 52 | * |
||
| 53 | * @param string $key |
||
| 54 | * @param string $value |
||
| 55 | * @param bool $autoload |
||
| 56 | */ |
||
| 57 | protected function save_key_value_pair_with_autoload( $key, $value, $autoload = true ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Save the field value(s) |
||
| 70 | * |
||
| 71 | * @param Field $field The field to save. |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function save( Field $field ) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Delete the field value(s) |
||
| 102 | * |
||
| 103 | * @param Field $field The field to delete. |
||
| 104 | */ |
||
| 105 | public function delete( Field $field ) { |
||
| 121 | } |
||
| 122 |