Complex classes like Give_Form_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_Form_API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Give_Form_API { |
||
| 13 | /** |
||
| 14 | * Instance. |
||
| 15 | * |
||
| 16 | * @since 2.0 |
||
| 17 | * @access private |
||
| 18 | * @var Give_Form_API |
||
| 19 | */ |
||
| 20 | static private $instance; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Array of forms. |
||
| 24 | * |
||
| 25 | * @since 2.0 |
||
| 26 | * @access private |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $forms = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of forms render count. |
||
| 33 | * |
||
| 34 | * @since 2.0 |
||
| 35 | * @access private |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private static $forms_render_counter = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The defaults for all elements |
||
| 42 | * |
||
| 43 | * @since 2.0 |
||
| 44 | * @access static |
||
| 45 | */ |
||
| 46 | static $field_defaults = array( |
||
| 47 | 'id' => '', |
||
| 48 | 'method' => 'post', |
||
| 49 | 'action' => '', |
||
| 50 | 'fields' => array(), |
||
| 51 | |||
| 52 | // Sort field by priority. |
||
| 53 | // If this param set to true then define priority for each field. |
||
| 54 | 'sort_by_priority' => false, |
||
| 55 | |||
| 56 | // This param will help to generate w3c validated form otherwise |
||
| 57 | // Some conflict can occur if you use same form twice or multiple times on same page for example id conflict. |
||
| 58 | 'set_unique_id' => true, |
||
| 59 | |||
| 60 | |||
| 61 | // Add custom attributes. |
||
| 62 | 'form_attributes' => array(), |
||
| 63 | |||
| 64 | // Supported form layout: simple, stepper, reveal, modal, button. |
||
| 65 | 'display_style' => 'simple', |
||
| 66 | 'continue_button_html' => '', |
||
| 67 | 'continue_button_title' => '', |
||
| 68 | |||
| 69 | // Manually render form. |
||
| 70 | 'callback' => '', |
||
| 71 | ); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Display styles. |
||
| 75 | * |
||
| 76 | * @since 2.0 |
||
| 77 | * @access private |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $display_styles = array( |
||
| 81 | 'simple' => 'includes/forms/api/view/simple-form-template.php', |
||
| 82 | 'stepper' => 'includes/forms/api/view/stepper-form-template.php', |
||
| 83 | 'reveal' => 'includes/forms/api/view/reveal-form-template.php', |
||
| 84 | 'modal' => 'includes/forms/api/view/modal-form-template.php', |
||
| 85 | 'button' => 'includes/forms/api/view/button-form-template.php', |
||
| 86 | ); |
||
| 87 | |||
| 88 | |||
| 89 | private function __construct() { |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Get instance. |
||
| 95 | * |
||
| 96 | * @return static |
||
| 97 | */ |
||
| 98 | public static function get_instance() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Initialize this module |
||
| 108 | * |
||
| 109 | * @since 2.0 |
||
| 110 | * @access static |
||
| 111 | */ |
||
| 112 | public function init() { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Register form. |
||
| 133 | * |
||
| 134 | * @since 2.0 |
||
| 135 | * @access public |
||
| 136 | * |
||
| 137 | * @param $form |
||
| 138 | * @param $form_id |
||
| 139 | * @param bool $force |
||
| 140 | */ |
||
| 141 | public static function register_form( $form, $form_id, $force = false ) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Render form by shortcode. |
||
| 150 | * |
||
| 151 | * @since 2.0 |
||
| 152 | * @access public |
||
| 153 | * |
||
| 154 | * @param array $attrs |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function render_shortcode( $attrs ) { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Render custom form. |
||
| 167 | * |
||
| 168 | * @since 1.0 |
||
| 169 | * @access private |
||
| 170 | * |
||
| 171 | * @param array $form |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | private function render_custom_form( $form ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Render forms. |
||
| 194 | * |
||
| 195 | * @since 2.0 |
||
| 196 | * @access static |
||
| 197 | * |
||
| 198 | * @param string $form_slug Form name. |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | static function render_form( $form_slug ) { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Set default values form form. |
||
| 255 | * |
||
| 256 | * @since 2.0 |
||
| 257 | * @access private |
||
| 258 | * |
||
| 259 | * @param $form |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | private static function set_default_values( $form ) { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Process a form, filling in $values with what's been posted |
||
| 298 | * |
||
| 299 | * @since 2.0 |
||
| 300 | * @access static |
||
| 301 | */ |
||
| 302 | static function process_form() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Recursively process a meta form element, filling in $values accordingly |
||
| 307 | * |
||
| 308 | * @since 2.0 |
||
| 309 | * @access static |
||
| 310 | * |
||
| 311 | * @param string $form_slug |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | static function get_form( $form_slug ) { |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Get forms. |
||
| 339 | * |
||
| 340 | * @since 1.0 |
||
| 341 | * @access public |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public static function get_forms() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get forms. |
||
| 351 | * |
||
| 352 | * @since 1.0 |
||
| 353 | * @access public |
||
| 354 | * |
||
| 355 | * @param array $form_tags |
||
| 356 | * @param array $form |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | private static function render_form_tags( $form_tags, $form ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Enqueue form api scripts. |
||
| 419 | * |
||
| 420 | * @since 2.0 |
||
| 421 | * @access public |
||
| 422 | */ |
||
| 423 | public function register_form_api_scripts() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Load Form API js var. |
||
| 458 | * |
||
| 459 | * @since 2.0 |
||
| 460 | * @access public |
||
| 461 | */ |
||
| 462 | public static function enqueue_scripts() { |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Get unique form/field id |
||
| 472 | * |
||
| 473 | * @since 2.0 |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @param array $form |
||
| 477 | * @param array $field |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public static function get_unique_id( $form, $field = array() ) { |
||
| 508 | } |
||
| 509 | |||
| 526 | add_action( 'init', 'give_init_forms_api', 99 ); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.