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 Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Helper { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Create a new helper. |
||
| 19 | * Hook the main Carbon Fields initialization functionality. |
||
| 20 | */ |
||
| 21 | public function __construct() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Load the plugin textdomain. |
||
| 44 | */ |
||
| 45 | public function load_textdomain() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Register containers and fields. |
||
| 55 | */ |
||
| 56 | public function trigger_fields_register() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initialize containers. |
||
| 71 | */ |
||
| 72 | public function init_containers() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialize main scripts |
||
| 78 | */ |
||
| 79 | public function init_scripts() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Print the carbon JSON data script. |
||
| 86 | */ |
||
| 87 | public function print_json_data_script() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieve containers and sidebars for use in the JS. |
||
| 99 | * |
||
| 100 | * @return array $carbon_data |
||
| 101 | */ |
||
| 102 | public function get_json_data() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Retrieve post meta field for a post. |
||
| 135 | * |
||
| 136 | * @param int $id Post ID. |
||
| 137 | * @param string $name Custom field name. |
||
| 138 | * @param string $type Custom field type (optional). |
||
| 139 | * @return mixed Meta value. |
||
| 140 | */ |
||
| 141 | public static function get_post_meta( $id, $name, $type = null ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Shorthand for get_post_meta(). |
||
| 149 | * Uses the ID of the current post in the loop. |
||
| 150 | * |
||
| 151 | * @param string $name Custom field name. |
||
| 152 | * @param string $type Custom field type (optional). |
||
| 153 | * @return mixed Meta value. |
||
| 154 | */ |
||
| 155 | public static function get_the_post_meta( $name, $type = null ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Retrieve theme option field value. |
||
| 161 | * |
||
| 162 | * @param string $name Custom field name. |
||
| 163 | * @param string $type Custom field type (optional). |
||
| 164 | * @return mixed Option value. |
||
| 165 | */ |
||
| 166 | public static function get_theme_option( $name, $type = null ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Retrieve term meta field for a term. |
||
| 172 | * |
||
| 173 | * @param int $id Term ID. |
||
| 174 | * @param string $name Custom field name. |
||
| 175 | * @param string $type Custom field type (optional). |
||
| 176 | * @return mixed Meta value. |
||
| 177 | */ |
||
| 178 | public static function get_term_meta( $id, $name, $type = null ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve user meta field for a user. |
||
| 186 | * |
||
| 187 | * @param int $id User ID. |
||
| 188 | * @param string $name Custom field name. |
||
| 189 | * @param string $type Custom field type (optional). |
||
| 190 | * @return mixed Meta value. |
||
| 191 | */ |
||
| 192 | public static function get_user_meta( $id, $name, $type = null ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Retrieve comment meta field for a comment. |
||
| 200 | * |
||
| 201 | * @param int $id Comment ID. |
||
| 202 | * @param string $name Custom field name. |
||
| 203 | * @param string $type Custom field type (optional). |
||
| 204 | * @return mixed Meta value. |
||
| 205 | */ |
||
| 206 | public static function get_comment_meta( $id, $name, $type = null ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieve a certain field value from the database. |
||
| 214 | * Handles the logic for different field types. |
||
| 215 | * |
||
| 216 | * @param string $data_type Data type. |
||
| 217 | * @param string $name Custom field name. |
||
| 218 | * @param string $type Custom field type (optional). |
||
| 219 | * @param int $id ID (optional). |
||
| 220 | * @return mixed Meta value. |
||
| 221 | */ |
||
| 222 | public static function get_field_value( $data_type, $name, $type = null, $id = null ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Retrieve a certain field value from the database. |
||
| 261 | * Handles the logic for different datastores (containers). |
||
| 262 | * |
||
| 263 | * @param string $datastore_type Datastore type. |
||
| 264 | * @param string $name Custom field name. |
||
| 265 | * @param int $id ID (optional). |
||
| 266 | * @return mixed Meta value. |
||
| 267 | */ |
||
| 268 | public static function get_field_value_by_datastore( $datastore_type, $name, $id = null ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds the field/container template(s) to the templates stack. |
||
| 305 | * |
||
| 306 | * @param object $object field or container object |
||
| 307 | **/ |
||
| 308 | public function add_templates( $object ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Build a string of concatenated pieces for an OR regex. |
||
| 329 | * |
||
| 330 | * @param array $pieces Pieces |
||
| 331 | * @param string $glue Glue between the pieces |
||
| 332 | * @return string Result string |
||
| 333 | */ |
||
| 334 | public static function preg_quote_array( $pieces, $glue = '|' ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Build the regex for parsing a certain complex field. |
||
| 342 | * |
||
| 343 | * @param string $field_name Name of the complex field. |
||
| 344 | * @param array $group_names Array of group names. |
||
| 345 | * @param array $field_names Array of subfield names. |
||
| 346 | * @return string Regex |
||
| 347 | */ |
||
| 348 | public static function get_complex_field_regex( $field_name, $group_names = array(), $field_names = array() ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Retrieve the complex field data for a certain field. |
||
| 366 | * |
||
| 367 | * @param string $type Datastore type. |
||
| 368 | * @param string $name Name of the field. |
||
| 369 | * @param int $id ID of the entry (optional). |
||
| 370 | * @return array Complex data entries. |
||
| 371 | */ |
||
| 372 | public static function get_complex_fields( $type, $name, $id = null ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Recursively expand the subfields of a complex field. |
||
| 410 | * |
||
| 411 | * @param array $input_groups Input groups. |
||
| 412 | * @param array $row Data row (key and value). |
||
| 413 | * @param array $field_name Field name pieces. |
||
| 414 | * @return array Expanded data. |
||
| 415 | */ |
||
| 416 | public static function expand_nested_field( $input_groups, $row, $field_name ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Parse the raw value of the relationship and association fields. |
||
| 437 | * |
||
| 438 | * @param string $raw_value Raw relationship value. |
||
| 439 | * @param string $type Field type. |
||
| 440 | * @return array Array of parsed data. |
||
| 441 | */ |
||
| 442 | public static function parse_relationship_field( $raw_value = '', $type = '' ) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Detect if using the old way of storing the relationship field values. |
||
| 479 | * If so, parse them to the new way of storing the data. |
||
| 480 | * |
||
| 481 | * @param mixed $value Old field value. |
||
| 482 | * @return mixed New field value. |
||
| 483 | */ |
||
| 484 | public static function maybe_old_relationship_field( $value ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Recursive sorting function by array key. |
||
| 501 | * @param array &$array The input array. |
||
| 502 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 503 | * @return array Sorted array. |
||
| 504 | */ |
||
| 505 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 517 | } |
||
| 518 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.