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 | * Value property to use for fields which need to be kept "alive" when they have no values stored (e.g. Set field with 0 checkboxes checked) |
||
| 27 | * Required to determine whether a field should use it's default value or stay blank |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const KEEPALIVE_PROPERTY = '_empty'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Glue characters between segments in keys |
||
| 35 | */ |
||
| 36 | const SEGMENT_GLUE = '|'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Glue characters between segments values in keys |
||
| 40 | */ |
||
| 41 | const SEGMENT_VALUE_GLUE = ':'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Number of segments in a key |
||
| 45 | */ |
||
| 46 | const TOTAL_SEGMENTS = 5; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * "Equal" storage key pattern comparison type constant |
||
| 50 | */ |
||
| 51 | const PATTERN_COMPARISON_EQUAL = '='; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * "Starts with" storage key pattern comparison type constant |
||
| 55 | */ |
||
| 56 | const PATTERN_COMPARISON_STARTS_WITH = '^'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the KEEPALIVE_PROPERTY |
||
| 60 | * Needed since php 5.3 cannot parse $instance->property::constant ( but parses $instance::constant ) |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function get_keepalive_property() { |
||
| 65 | return static::KEEPALIVE_PROPERTY; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get sanitized hierarchy index for hierarchy |
||
| 70 | * |
||
| 71 | * @param array<string> $full_hierarchy |
||
| 72 | * @param array<int> $full_hierarchy_index |
||
| 73 | * @return array<int> |
||
| 74 | */ |
||
| 75 | 4 | public function get_sanitized_hierarchy_index( $full_hierarchy, $full_hierarchy_index ) { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get a storage key for a simple root field |
||
| 86 | * |
||
| 87 | * @param string $field_base_name |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | protected function get_storage_key_for_simple_root_field( $field_base_name ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a storage key for the root field |
||
| 97 | * Suitable for deleting entire trees of values (e.g. Complex_Field) |
||
| 98 | * |
||
| 99 | * @param array $full_hierarchy |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | protected function get_storage_key_root( $full_hierarchy ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get a storage key up to the hierarchy index segment |
||
| 110 | * Suitable for getting and deleting multiple values for a single field |
||
| 111 | * |
||
| 112 | * @param array<string> $full_hierarchy |
||
| 113 | * @param array<int> $full_hierarchy_index |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | protected function get_storage_key_prefix( $full_hierarchy, $full_hierarchy_index ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get a full storage key for a single field value |
||
| 134 | * |
||
| 135 | * @param bool $is_simple_root_field |
||
| 136 | * @param array<string> $full_hierarchy |
||
| 137 | * @param array<int> $full_hierarchy_index |
||
| 138 | * @param int $value_group_index |
||
| 139 | * @param string $property |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 6 | public function get_storage_key( $is_simple_root_field, $full_hierarchy, $full_hierarchy_index, $value_group_index, $property ) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get a full storage key with optional wildcards for entry and value indexes |
||
| 153 | * |
||
| 154 | * @param bool $is_simple_root_field |
||
| 155 | * @param array<string> $full_hierarchy |
||
| 156 | * @param string $property |
||
| 157 | * @param string $wildcard |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | 3 | public function get_storage_key_with_index_wildcards( $is_simple_root_field, $full_hierarchy, $property = Value_Set::VALUE_PROPERTY, $wildcard = '%' ) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get an array of storage key patterns for use when getting values from storage |
||
| 186 | * |
||
| 187 | * @param bool $is_simple_root_field |
||
| 188 | * @param array<string> $full_hierarchy |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 3 | public function get_storage_key_getter_patterns( $is_simple_root_field, $full_hierarchy ) { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Get an array of storage key patterns for use when deleting values from storage |
||
| 218 | * |
||
| 219 | * @param bool $is_complex_field |
||
| 220 | * @param bool $is_simple_root_field |
||
| 221 | * @param array<string> $full_hierarchy |
||
| 222 | * @param array<int> $full_hierarchy_index |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 4 | public function get_storage_key_deleter_patterns( $is_complex_field, $is_simple_root_field, $full_hierarchy, $full_hierarchy_index ) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Convert an array of storage key patterns to a parentheses-enclosed sql comparison |
||
| 245 | * |
||
| 246 | * @param string $table_column |
||
| 247 | * @param array $patterns |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | 2 | public function storage_key_patterns_to_sql( $table_column, $patterns ) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Check if a storage key matches any pattern |
||
| 275 | * |
||
| 276 | * @param string $storage_key |
||
| 277 | * @param array $patterns |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 8 | public function storage_key_matches_any_pattern( $storage_key, $patterns ) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Check if an array of segments has all of it's segments |
||
| 304 | * |
||
| 305 | * @param array<string> $segments_array |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | 1 | public function is_segments_array_full( $segments_array ) { |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Convert a storage key to an array of it's segments |
||
| 314 | * |
||
| 315 | * @param string $storage_key |
||
| 316 | * @return array<string> |
||
| 317 | */ |
||
| 318 | 1 | public function storage_key_to_segments_array( $storage_key ) { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Convert a segment to an array of it's values |
||
| 326 | * |
||
| 327 | * @param string $segment |
||
| 328 | * @return array<mixed> |
||
| 329 | */ |
||
| 330 | 1 | public function segment_to_segment_values_array( $segment ) { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Get a parsed array of storage key segments and values |
||
| 336 | * |
||
| 337 | * @param string $storage_key |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 3 | public function parse_storage_key( $storage_key ) { |
|
| 369 | } |