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 |
||
15 | abstract class Key_Value_Datastore extends Datastore { |
||
16 | |||
17 | /** |
||
18 | * Key Toolset for key generation and comparison utilities |
||
19 | * |
||
20 | * @var Key_Toolset |
||
21 | */ |
||
22 | protected $key_toolset; |
||
23 | |||
24 | /** |
||
25 | * Initialize the datastore. |
||
26 | */ |
||
27 | public function __construct() { |
||
31 | |||
32 | /** |
||
33 | * Get array of ancestors (ordered top-bottom) with the field name appended to the end |
||
34 | * |
||
35 | * @param Field $field |
||
36 | * @return array<string> |
||
37 | */ |
||
38 | protected function get_full_hierarchy_for_field( Field $field ) { |
||
42 | |||
43 | /** |
||
44 | * Get array of ancestor entry indexes (ordered top-bottom) |
||
45 | * Indexes show which entry/group this field belongs to in a Complex_Field |
||
46 | * |
||
47 | * @param Field $field |
||
48 | * @return array<int> |
||
49 | */ |
||
50 | protected function get_full_hierarchy_index_for_field( Field $field ) { |
||
55 | |||
56 | /** |
||
57 | * Raw Value Set Tree schema: |
||
58 | * array( |
||
59 | * [field_name] => array( |
||
60 | * 'value_set' => [raw_value_set], |
||
61 | * 'groups' => array( |
||
62 | * array( |
||
63 | * [recursion] |
||
64 | * ), |
||
65 | * ... |
||
66 | * ), |
||
67 | * ), |
||
68 | * ... |
||
69 | * ) |
||
70 | * |
||
71 | * @param array<stdClass> $storage_array |
||
72 | * @return array |
||
73 | */ |
||
74 | protected function cascading_storage_array_to_raw_value_set_tree( $storage_array ) { |
||
123 | |||
124 | /** |
||
125 | * Get a reduced raw value set tree only relevant to the specified field |
||
126 | * |
||
127 | * @param array $raw_value_set_tree |
||
128 | * @param Field $field |
||
129 | * @return array |
||
130 | */ |
||
131 | protected function reduce_raw_value_set_tree_to_field( $raw_value_set_tree, Field $field ) { |
||
148 | |||
149 | /** |
||
150 | * Get a raw database query results array for a field |
||
151 | * |
||
152 | * @param Field $field The field to retrieve value for. |
||
153 | * @param array $storage_key_patterns |
||
154 | * @return array<stdClass> Array of {key, value} objects |
||
155 | */ |
||
156 | abstract protected function get_storage_array( Field $field, $storage_key_patterns ); |
||
157 | |||
158 | /** |
||
159 | * Get the field value(s) |
||
160 | * |
||
161 | * @param Field $field The field to get value(s) for |
||
162 | * @return array<array> |
||
163 | */ |
||
164 | public function load( Field $field ) { |
||
171 | |||
172 | /** |
||
173 | * Save a single key-value pair to the database |
||
174 | * |
||
175 | * @param string $key |
||
176 | * @param string $value |
||
177 | */ |
||
178 | abstract protected function save_key_value_pair( $key, $value ); |
||
179 | |||
180 | /** |
||
181 | * Save the field value(s) |
||
182 | * |
||
183 | * @param Field $field The field to save. |
||
184 | */ |
||
185 | View Code Duplication | public function save( Field $field ) { |
|
211 | } |
||
212 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.