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 |
||
18 | class Key_Toolset { |
||
19 | |||
20 | /** |
||
21 | * Prefix appended to all keys |
||
22 | */ |
||
23 | const KEY_PREFIX = '_'; |
||
24 | |||
25 | /** |
||
26 | * Glue characters between segments in keys |
||
27 | */ |
||
28 | const SEGMENT_GLUE = '|'; |
||
29 | |||
30 | /** |
||
31 | * Glue characters between segments values in keys |
||
32 | */ |
||
33 | const SEGMENT_VALUE_GLUE = ':'; |
||
34 | |||
35 | /** |
||
36 | * Number of segments in a key |
||
37 | */ |
||
38 | const TOTAL_SEGMENTS = 5; |
||
39 | |||
40 | /** |
||
41 | * "Equal" storage key pattern comparison type constant |
||
42 | */ |
||
43 | const PATTERN_COMPARISON_EQUAL = '='; |
||
44 | |||
45 | /** |
||
46 | * "Starts with" storage key pattern comparison type constant |
||
47 | */ |
||
48 | const PATTERN_COMPARISON_STARTS_WITH = '^'; |
||
49 | |||
50 | /** |
||
51 | * Get sanitized hierarchy index for hierarchy |
||
52 | * |
||
53 | * @param array<string> $full_hierarchy |
||
54 | * @param array<int> $full_hierarchy_index |
||
55 | * @return array<int> |
||
56 | */ |
||
57 | 4 | public function get_sanitized_hierarchy_index( $full_hierarchy, $full_hierarchy_index ) { |
|
65 | |||
66 | /** |
||
67 | * Get a storage key for a simple root field |
||
68 | * |
||
69 | * @param string $field_base_name |
||
70 | * @return string |
||
71 | */ |
||
72 | protected function get_storage_key_for_simple_root_field( $field_base_name ) { |
||
76 | |||
77 | /** |
||
78 | * Get a storage key for the root field |
||
79 | * Suitable for deleting entire trees of values (e.g. Complex_Field) |
||
80 | * |
||
81 | * @param array $full_hierarchy |
||
82 | * @return string |
||
83 | */ |
||
84 | protected function get_storage_key_root( $full_hierarchy ) { |
||
89 | |||
90 | /** |
||
91 | * Get a storage key up to the hierarchy index segment |
||
92 | * Suitable for getting and deleting multiple values for a single field |
||
93 | * |
||
94 | * @param array<string> $full_hierarchy |
||
95 | * @param array<int> $full_hierarchy_index |
||
96 | * @return string |
||
97 | */ |
||
98 | protected function get_storage_key_prefix( $full_hierarchy, $full_hierarchy_index ) { |
||
113 | |||
114 | /** |
||
115 | * Get a full storage key for a single field value |
||
116 | * |
||
117 | * @param bool $is_simple_root_field |
||
118 | * @param array<string> $full_hierarchy |
||
119 | * @param array<int> $full_hierarchy_index |
||
120 | * @param int $value_group_index |
||
121 | * @param string $value_key |
||
122 | * @return string |
||
123 | */ |
||
124 | 6 | public function get_storage_key( $is_simple_root_field, $full_hierarchy, $full_hierarchy_index, $value_group_index, $value_key ) { |
|
132 | |||
133 | /** |
||
134 | * Get a full storage key with optional wildcards for entry and value indexes |
||
135 | * |
||
136 | * @param bool $is_simple_root_field |
||
137 | * @param array<string> $full_hierarchy |
||
138 | * @param string $value_key |
||
139 | * @param string $wildcard |
||
140 | * @return string |
||
141 | */ |
||
142 | 3 | public function get_storage_key_with_index_wildcards( $is_simple_root_field, $full_hierarchy, $value_key = Value_Set::VALUE_PROPERTY, $wildcard = '%' ) { |
|
165 | |||
166 | /** |
||
167 | * Get an array of storage key patterns for use when getting values from storage |
||
168 | * |
||
169 | * @param bool $is_simple_root_field |
||
170 | * @param array<string> $full_hierarchy |
||
171 | * @return array |
||
172 | */ |
||
173 | 3 | public function get_storage_key_getter_patterns( $is_simple_root_field, $full_hierarchy ) { |
|
197 | |||
198 | /** |
||
199 | * Get an array of storage key patterns for use when deleting values from storage |
||
200 | * |
||
201 | * @param bool $is_complex_field |
||
202 | * @param bool $is_simple_root_field |
||
203 | * @param array<string> $full_hierarchy |
||
204 | * @param array<int> $full_hierarchy_index |
||
205 | * @return array |
||
206 | */ |
||
207 | 4 | public function get_storage_key_deleter_patterns( $is_complex_field, $is_simple_root_field, $full_hierarchy, $full_hierarchy_index ) { |
|
224 | |||
225 | /** |
||
226 | * Convert an array of storage key patterns to a parentheses-enclosed sql comparison |
||
227 | * |
||
228 | * @param string $table_column |
||
229 | * @param array $patterns |
||
230 | * @return string |
||
231 | */ |
||
232 | 2 | public function storage_key_patterns_to_sql( $table_column, $patterns ) { |
|
254 | |||
255 | /** |
||
256 | * Check if a storage key matches any pattern |
||
257 | * |
||
258 | * @param string $storage_key |
||
259 | * @param array $patterns |
||
260 | * @return bool |
||
261 | */ |
||
262 | 8 | public function storage_key_matches_any_pattern( $storage_key, $patterns ) { |
|
283 | |||
284 | /** |
||
285 | * Check if an array of segments has all of it's segments |
||
286 | * |
||
287 | * @param array<string> $segments_array |
||
288 | * @return bool |
||
289 | */ |
||
290 | 1 | public function is_segments_array_full( $segments_array ) { |
|
293 | |||
294 | /** |
||
295 | * Convert a storage key to an array of it's segments |
||
296 | * |
||
297 | * @param string $storage_key |
||
298 | * @return array<string> |
||
299 | */ |
||
300 | 1 | public function storage_key_to_segments_array( $storage_key ) { |
|
305 | |||
306 | /** |
||
307 | * Convert a segment to an array of it's values |
||
308 | * |
||
309 | * @param string $segment |
||
310 | * @return array<mixed> |
||
311 | */ |
||
312 | 1 | public function segment_to_segment_values_array( $segment ) { |
|
315 | |||
316 | /** |
||
317 | * Get a parsed array of storage key segments and values |
||
318 | * |
||
319 | * @param string $storage_key |
||
320 | * @return array |
||
321 | */ |
||
322 | 3 | public function parse_storage_key( $storage_key ) { |
|
349 | } |