Complex classes like Give_Fields_API 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 Give_Fields_API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Give_Fields_API { |
||
|
|
|||
| 13 | /** |
||
| 14 | * Instance. |
||
| 15 | * |
||
| 16 | * @since 1.9 |
||
| 17 | * @access private |
||
| 18 | * @var Give_Fields_API |
||
| 19 | */ |
||
| 20 | static private $instance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The defaults for all elements |
||
| 24 | * |
||
| 25 | * @since 1.9 |
||
| 26 | * @access static |
||
| 27 | */ |
||
| 28 | static $field_defaults = array( |
||
| 29 | 'type' => '', |
||
| 30 | 'name' => '', |
||
| 31 | 'data_type' => '', |
||
| 32 | 'value' => '', |
||
| 33 | 'required' => false, |
||
| 34 | |||
| 35 | // Set default value to field. |
||
| 36 | 'default' => '', |
||
| 37 | |||
| 38 | // Add label, before and after field. |
||
| 39 | 'label' => '', |
||
| 40 | 'label_position' => 'before', |
||
| 41 | |||
| 42 | // Add description to field as tooltip. |
||
| 43 | 'tooltip' => '', |
||
| 44 | |||
| 45 | // Show multiple fields in same row with in sub section. |
||
| 46 | 'sub_section_start' => false, |
||
| 47 | 'sub_section_end' => false, |
||
| 48 | |||
| 49 | // Add custom attributes. |
||
| 50 | 'attributes' => array(), |
||
| 51 | 'row_attributes' => array(), |
||
| 52 | |||
| 53 | // Params to edit field html. |
||
| 54 | // @todo: Implement these params. |
||
| 55 | 'before_field' => '', |
||
| 56 | 'after_field' => '', |
||
| 57 | 'before_field_wrapper' => '', |
||
| 58 | 'after_field_wrapper' => '', |
||
| 59 | 'before_label' => '', |
||
| 60 | 'after_label' => '', |
||
| 61 | |||
| 62 | // Manually render field. |
||
| 63 | 'callback' => '', |
||
| 64 | |||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The defaults for all elements |
||
| 69 | * |
||
| 70 | * @since 1.9 |
||
| 71 | * @access static |
||
| 72 | */ |
||
| 73 | static $section_defaults = array( |
||
| 74 | 'label' => '', |
||
| 75 | 'name' => '', |
||
| 76 | 'attributes' => array(), |
||
| 77 | |||
| 78 | // Manually render section. |
||
| 79 | 'callback' => '', |
||
| 80 | ); |
||
| 81 | |||
| 82 | |||
| 83 | private function __construct() { |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Get instance. |
||
| 89 | * |
||
| 90 | * @return static |
||
| 91 | */ |
||
| 92 | public static function get_instance() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Initialize this module |
||
| 102 | * |
||
| 103 | * @since 1.9 |
||
| 104 | * @access static |
||
| 105 | */ |
||
| 106 | public function init() { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Render custom field. |
||
| 113 | * |
||
| 114 | * @since 1.0 |
||
| 115 | * @access private |
||
| 116 | * |
||
| 117 | * @param array $field |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | private function render_custom_field( $field ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Render tag |
||
| 140 | * |
||
| 141 | * @since 1.9 |
||
| 142 | * @access public |
||
| 143 | * |
||
| 144 | * @param $field |
||
| 145 | * @param $form |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public static function render_tag( $field, $form = null ) { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Render `{{form_fields}}` tag. |
||
| 165 | * |
||
| 166 | * @since 1.9 |
||
| 167 | * @access private |
||
| 168 | * |
||
| 169 | * @param string $form_html |
||
| 170 | * @param array $form |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function render_tags( $form_html, $form ) { |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Render section. |
||
| 229 | * |
||
| 230 | * @since 1.9 |
||
| 231 | * @access public |
||
| 232 | * |
||
| 233 | * @param array $section |
||
| 234 | * @param array $form |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public static function render_section( $section, $form = null ) { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Render text field. |
||
| 262 | * |
||
| 263 | * @since 1.9 |
||
| 264 | * @access private |
||
| 265 | * |
||
| 266 | * @param array $field |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public static function render_text_field( $field ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Render text field. |
||
| 305 | * |
||
| 306 | * @since 1.9 |
||
| 307 | * @access private |
||
| 308 | * |
||
| 309 | * @param array $field |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public static function render_submit_field( $field ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Render text field. |
||
| 319 | * |
||
| 320 | * @since 1.9 |
||
| 321 | * @access private |
||
| 322 | * |
||
| 323 | * @param array $field |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public static function render_checkbox_field( $field ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Render label |
||
| 333 | * |
||
| 334 | * @since 1.9 |
||
| 335 | * @access public |
||
| 336 | * |
||
| 337 | * @param $field |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public static function render_label( $field ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Get field attribute string from field arguments. |
||
| 364 | * |
||
| 365 | * @since 1.9 |
||
| 366 | * @access private |
||
| 367 | * |
||
| 368 | * @param array $attributes |
||
| 369 | * |
||
| 370 | * @return array|string |
||
| 371 | */ |
||
| 372 | private function get_attributes( $attributes ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get field attribute string from field arguments. |
||
| 390 | * |
||
| 391 | * @since 1.9 |
||
| 392 | * @access private |
||
| 393 | * |
||
| 394 | * @param $field |
||
| 395 | * |
||
| 396 | * @return array|string |
||
| 397 | */ |
||
| 398 | private function get_field_attributes( $field ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get row attribute string from field arguments. |
||
| 404 | * |
||
| 405 | * @since 1.9 |
||
| 406 | * @access private |
||
| 407 | * |
||
| 408 | * @param $field |
||
| 409 | * |
||
| 410 | * @return array|string |
||
| 411 | */ |
||
| 412 | private function get_row_attributes( $field ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set default values for fields |
||
| 418 | * |
||
| 419 | * @since 1.0 |
||
| 420 | * @access private |
||
| 421 | * |
||
| 422 | * @param array $field |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | private function set_default_values( $field ) { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Set responsive fields. |
||
| 461 | * |
||
| 462 | * @since 1.9 |
||
| 463 | * @access private |
||
| 464 | * |
||
| 465 | * @param $form |
||
| 466 | * |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | private function set_responsive_field( &$form ) { |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Check if current feld is part of sub section or not. |
||
| 519 | * |
||
| 520 | * @since 1.9 |
||
| 521 | * @access private |
||
| 522 | * |
||
| 523 | * @param $field |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | private function is_sub_section( $field ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Is the element a button? |
||
| 541 | * |
||
| 542 | * @since 1.9 |
||
| 543 | * @access static |
||
| 544 | * |
||
| 545 | * @param array $element |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | static function is_button( $element ) { |
||
| 552 | } |
||
| 553 |