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 ) { |
||
41 | |||
42 | /** |
||
43 | * Save a single key-value pair to the database |
||
44 | * |
||
45 | * @param string $key |
||
46 | * @param string $value |
||
47 | */ |
||
48 | protected function save_key_value_pair( $key, $value ) { |
||
51 | |||
52 | /** |
||
53 | * Save a single key-value pair to the database with autoload |
||
54 | * |
||
55 | * @param string $key |
||
56 | * @param string $value |
||
57 | * @param string $autoload "yes"|"no" |
||
58 | */ |
||
59 | protected function save_key_value_pair_with_autoload( $key, $value, $autoload ) { |
||
68 | |||
69 | /** |
||
70 | * Save the field value(s) |
||
71 | * |
||
72 | * @param Field $field The field to save. |
||
73 | */ |
||
74 | public function save( Field $field ) { |
||
104 | |||
105 | /** |
||
106 | * Delete the field value(s) |
||
107 | * |
||
108 | * @param Field $field The field to delete. |
||
109 | */ |
||
110 | public function delete( Field $field ) { |
||
125 | } |
||
126 |