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:
Complex classes like Legacy_Storage_Service often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Legacy_Storage_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Legacy_Storage_Service extends Service { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Contaier repository to fetch fields from |
||
| 20 | * |
||
| 21 | * @var ContainerRepository |
||
| 22 | */ |
||
| 23 | protected $container_repository; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Key Toolset for key generation and comparison utilities |
||
| 27 | * |
||
| 28 | * @var Key_Toolset |
||
| 29 | */ |
||
| 30 | protected $key_toolset; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * List of special key suffixes that the Map field uses so save extra data |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $map_keys = array( 'lat', 'lng', 'zoom', 'address' ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Cache of converted storage arrays |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $storage_array_cache = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Service constructor |
||
| 48 | * |
||
| 49 | * @param ContainerRepository $container_repository |
||
| 50 | */ |
||
| 51 | public function __construct( ContainerRepository $container_repository, Key_Toolset $key_toolset ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Enable the service |
||
| 58 | */ |
||
| 59 | protected function enabled() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Disable the service |
||
| 65 | */ |
||
| 66 | protected function disabled() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if a key is a legacy map property key |
||
| 72 | * |
||
| 73 | * @param string $key |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | protected function is_legacy_map_key( $key ) { |
||
| 77 | $map_regex = array_map( function( $map_key ) { |
||
| 78 | return preg_quote( $map_key, '/' ); |
||
| 79 | }, $this->map_keys ); |
||
| 80 | $map_regex = '/-(' . implode( '|', $map_regex ) . ')$/'; |
||
| 81 | return (bool) preg_match( $map_regex, $key ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Return container instance which uses the passed datastore |
||
| 86 | * |
||
| 87 | * @param Datastore_Interface $datastore |
||
| 88 | * @return Container |
||
| 89 | */ |
||
| 90 | protected function get_container_for_datastore( Datastore_Interface $datastore ) { |
||
| 91 | $containers = $this->container_repository->get_containers(); |
||
| 92 | foreach ( $containers as $container ) { |
||
| 93 | if ( $container->get_datastore() === $datastore ) { |
||
| 94 | return $container; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a nested array of field_group permutations suitable for old key parsing |
||
| 102 | * |
||
| 103 | * @param array $fields |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | protected function get_field_group_permutations( $fields ) { |
||
| 107 | $permutations = array(); |
||
| 108 | |||
| 109 | foreach ( $fields as $field ) { |
||
| 110 | if ( is_a( $field, 'Carbon_Fields\\Field\\Complex_Field' ) ) { |
||
| 111 | $group_names = $field->get_group_names(); |
||
| 112 | foreach ( $group_names as $group_name ) { |
||
| 113 | $group = $field->get_group_by_name( $group_name ); |
||
| 114 | if ( ! $group ) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | $permutations[] = array( |
||
| 118 | 'field' => $field->get_base_name(), |
||
| 119 | 'group' => $group_name, |
||
| 120 | 'children' => $this->get_field_group_permutations( $group->get_fields() ), |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | $permutations[] = array( |
||
| 125 | 'field' => $field->get_base_name(), |
||
| 126 | 'group' => '', |
||
| 127 | 'children' => array(), |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return $permutations; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get array of database table details for datastore |
||
| 137 | * |
||
| 138 | * @param Datastore_Interface $datastore |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | protected function get_table_details_for_datastore( Datastore_Interface $datastore ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get array of sql comparisons for field |
||
| 169 | * |
||
| 170 | * @param Field $field |
||
| 171 | * @param string $key_prefix |
||
| 172 | * @param string $key_column |
||
| 173 | * @return array<string> |
||
| 174 | */ |
||
| 175 | protected function get_legacy_sql_comparisons_for_field( Field $field, $key_prefix, $key_column ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get a key-value array of legacy values for fields in the container of the passed datastore |
||
| 200 | * |
||
| 201 | * @param Container $container |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | protected function get_legacy_storage_array_from_database( Container $container ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get a key-value array of CF 1.5 values for fields in the container of the passed datastore |
||
| 246 | * |
||
| 247 | * @param Datastore_Interface $datastore |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function get_legacy_storage_array( Datastore_Interface $datastore ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get expanded value for key from legacy storage array |
||
| 265 | * |
||
| 266 | * @param string $key Legacy key to fetch additional values for |
||
| 267 | * @param array $legacy_storage_array key=>value array of legacy data |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | protected function get_value_for_legacy_key( $key, $legacy_storage_array ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Convert legacy storage rows to array of row descriptors |
||
| 289 | * |
||
| 290 | * @param array $legacy_storage_array |
||
| 291 | * @param array $field_group_permutations |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | protected function legacy_storage_rows_to_row_descriptors( $legacy_storage_array, $field_group_permutations ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get key segmentation regex for a field name |
||
| 311 | * |
||
| 312 | * @param string $field_name |
||
| 313 | * @param string $group_name |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | protected function get_key_segmentation_regex_for_field_name( $field_name, $group_name = '' ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Convert legacy storage row to row descriptor |
||
| 328 | * |
||
| 329 | * @param string $key |
||
| 330 | * @param string $value |
||
| 331 | * @param array $field_group_permutations |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | protected function legacy_storage_row_to_row_descriptor( $key, $value, $field_group_permutations ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Convert row descriptor to array of new storage key-values |
||
| 372 | * |
||
| 373 | * @param array $row_descriptor |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | protected function row_descriptor_to_storage_array( $row_descriptor ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get all data saved for a datastore in the new key-value format |
||
| 434 | * |
||
| 435 | * @param Datastore_Interface $datastore |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | 1 | public function get_storage_array_for_datastore( Datastore_Interface $datastore ) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get array of new storage key-values matching key patterns |
||
| 458 | * |
||
| 459 | * @param Datastore_Interface $datastore |
||
| 460 | * @param array $storage_key_patterns |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | 1 | public function get_storage_array( Datastore_Interface $datastore, $storage_key_patterns ) { |
|
| 476 | |||
| 477 | public function filter_storage_array( $storage_array, $datastore, $storage_key_patterns ) { |
||
| 483 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: