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 |
||
| 14 | class Helper { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Create a new helper. |
||
| 18 | * Hook the main Carbon Fields initialization functionality. |
||
| 19 | */ |
||
| 20 | public function __construct() { |
||
| 21 | add_action( 'wp_loaded', array( $this, 'trigger_fields_register' ) ); |
||
| 22 | add_action( 'carbon_after_register_fields', array( $this, 'init_containers' ) ); |
||
| 23 | add_action( 'admin_footer', array( $this, 'init_scripts' ), 0 ); |
||
| 24 | add_action( 'crb_field_activated', array( $this, 'add_templates' ) ); |
||
| 25 | add_action( 'crb_container_activated', array( $this, 'add_templates' ) ); |
||
| 26 | add_action( 'after_setup_theme', array( $this, 'load_textdomain' ), 9999 ); |
||
| 27 | |||
| 28 | # Initialize templater |
||
| 29 | new Templater(); |
||
| 30 | |||
| 31 | # Initialize sidebar manager |
||
| 32 | Sidebar_Manager::instance(); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Load the plugin textdomain. |
||
| 37 | */ |
||
| 38 | public function load_textdomain() { |
||
| 39 | $dir = dirname( dirname( dirname( __FILE__ ) ) ) . '/languages/'; |
||
| 40 | $domain = 'carbon_fields'; |
||
| 41 | $locale = get_locale(); |
||
| 42 | $path = $dir . $domain . '-' . $locale . '.mo'; |
||
| 43 | load_textdomain( $domain, $path ); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Register containers and fields. |
||
| 48 | */ |
||
| 49 | public function trigger_fields_register() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize containers. |
||
| 64 | */ |
||
| 65 | public function init_containers() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initialize main scripts |
||
| 71 | */ |
||
| 72 | public function init_scripts() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Retrieve containers and sidebars for use in the JS. |
||
| 94 | * |
||
| 95 | * @return array $carbon_data |
||
| 96 | */ |
||
| 97 | public function get_json_data() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Retrieve post meta field for a post. |
||
| 130 | * |
||
| 131 | * @param int $id Post ID. |
||
| 132 | * @param string $name Custom field name. |
||
| 133 | * @param string $type Custom field type (optional). |
||
| 134 | * @return mixed Meta value. |
||
| 135 | */ |
||
| 136 | public static function get_post_meta( $id, $name, $type = null ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Shorthand for get_post_meta(). |
||
| 144 | * Uses the ID of the current post in the loop. |
||
| 145 | * |
||
| 146 | * @param string $name Custom field name. |
||
| 147 | * @param string $type Custom field type (optional). |
||
| 148 | * @return mixed Meta value. |
||
| 149 | */ |
||
| 150 | public static function get_the_post_meta( $name, $type = null ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieve theme option field value. |
||
| 156 | * |
||
| 157 | * @param string $name Custom field name. |
||
| 158 | * @param string $type Custom field type (optional). |
||
| 159 | * @return mixed Option value. |
||
| 160 | */ |
||
| 161 | public static function get_theme_option( $name, $type = null ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Retrieve term meta field for a term. |
||
| 167 | * |
||
| 168 | * @param int $id Term ID. |
||
| 169 | * @param string $name Custom field name. |
||
| 170 | * @param string $type Custom field type (optional). |
||
| 171 | * @return mixed Meta value. |
||
| 172 | */ |
||
| 173 | public static function get_term_meta( $id, $name, $type = null ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve user meta field for a user. |
||
| 181 | * |
||
| 182 | * @param int $id User ID. |
||
| 183 | * @param string $name Custom field name. |
||
| 184 | * @param string $type Custom field type (optional). |
||
| 185 | * @return mixed Meta value. |
||
| 186 | */ |
||
| 187 | public static function get_user_meta( $id, $name, $type = null ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieve comment meta field for a comment. |
||
| 195 | * |
||
| 196 | * @param int $id Comment ID. |
||
| 197 | * @param string $name Custom field name. |
||
| 198 | * @param string $type Custom field type (optional). |
||
| 199 | * @return mixed Meta value. |
||
| 200 | */ |
||
| 201 | public static function get_comment_meta( $id, $name, $type = null ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Retrieve a certain field value from the database. |
||
| 209 | * Handles the logic for different field types. |
||
| 210 | * |
||
| 211 | * @param string $data_type Data type. |
||
| 212 | * @param string $name Custom field name. |
||
| 213 | * @param string $type Custom field type (optional). |
||
| 214 | * @param int $id ID (optional). |
||
| 215 | * @return mixed Meta value. |
||
| 216 | */ |
||
| 217 | public static function get_field_value( $data_type, $name, $type = null, $id = null ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Retrieve a certain field value from the database. |
||
| 256 | * Handles the logic for different data stores (containers). |
||
| 257 | * |
||
| 258 | * @param string $store_type Data store type. |
||
| 259 | * @param string $name Custom field name. |
||
| 260 | * @param int $id ID (optional). |
||
| 261 | * @return mixed Meta value. |
||
| 262 | */ |
||
| 263 | public static function get_field_value_by_store( $store_type, $name, $id = null ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Adds the field/container template(s) to the templates stack. |
||
| 300 | * |
||
| 301 | * @param object $object field or container object |
||
| 302 | **/ |
||
| 303 | public function add_templates( $object ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Build a string of concatenated pieces for an OR regex. |
||
| 324 | * |
||
| 325 | * @param array $pieces Pieces |
||
| 326 | * @param string $glue Glue between the pieces |
||
| 327 | * @return string Result string |
||
| 328 | */ |
||
| 329 | public static function preg_quote_array( $pieces, $glue = '|' ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Build the regex for parsing a certain complex field. |
||
| 337 | * |
||
| 338 | * @param string $field_name Name of the complex field. |
||
| 339 | * @param array $group_names Array of group names. |
||
| 340 | * @param array $field_names Array of subfield names. |
||
| 341 | * @return string Regex |
||
| 342 | */ |
||
| 343 | public static function get_complex_field_regex( $field_name, $group_names = array(), $field_names = array() ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Retrieve the complex field data for a certain field. |
||
| 361 | * |
||
| 362 | * @param string $type Datastore type. |
||
| 363 | * @param string $name Name of the field. |
||
| 364 | * @param int $id ID of the entry (optional). |
||
| 365 | * @return array Complex data entries. |
||
| 366 | */ |
||
| 367 | public static function get_complex_fields( $type, $name, $id = null ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Recursively expand the subfields of a complex field. |
||
| 405 | * |
||
| 406 | * @param array $input_groups Input groups. |
||
| 407 | * @param array $row Data row (key and value). |
||
| 408 | * @param array $field_name Field name pieces. |
||
| 409 | * @return array Expanded data. |
||
| 410 | */ |
||
| 411 | public static function expand_nested_field( $input_groups, $row, $field_name ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Parse the raw value of the relationship and association fields. |
||
| 432 | * |
||
| 433 | * @param string $raw_value Raw relationship value. |
||
| 434 | * @param string $type Field type. |
||
| 435 | * @return array Array of parsed data. |
||
| 436 | */ |
||
| 437 | public static function parse_relationship_field( $raw_value = '', $type = '' ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Detect if using the old way of storing the relationship field values. |
||
| 474 | * If so, parse them to the new way of storing the data. |
||
| 475 | * |
||
| 476 | * @param mixed $value Old field value. |
||
| 477 | * @return mixed New field value. |
||
| 478 | */ |
||
| 479 | public static function maybe_old_relationship_field( $value ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Recursive sorting function by array key. |
||
| 496 | * @param array &$array The input array. |
||
| 497 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 498 | * @return array Sorted array. |
||
| 499 | */ |
||
| 500 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 512 | |||
| 513 | } |