Complex classes like Give_HTML_Elements 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_HTML_Elements, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_HTML_Elements { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Donations Dropdown |
||
| 28 | * |
||
| 29 | * Renders an HTML Dropdown of all the donations. |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @param array $args Arguments for the dropdown. |
||
| 35 | * |
||
| 36 | * @return string Donations dropdown. |
||
| 37 | */ |
||
| 38 | public function donations_dropdown( $args = array() ) { |
||
| 39 | |||
| 40 | $defaults = array( |
||
| 41 | 'name' => 'donations', |
||
| 42 | 'id' => 'donations', |
||
| 43 | 'class' => '', |
||
| 44 | 'multiple' => false, |
||
| 45 | 'selected' => 0, |
||
| 46 | 'chosen' => false, |
||
| 47 | 'number' => 30, |
||
| 48 | 'placeholder' => __( 'Select a donation', 'give' ) |
||
| 49 | ); |
||
| 50 | |||
| 51 | $args = wp_parse_args( $args, $defaults ); |
||
| 52 | |||
| 53 | $payments = new Give_Payments_Query( array( |
||
| 54 | 'number' => $args['number'] |
||
| 55 | ) ); |
||
| 56 | |||
| 57 | $payments = $payments->get_payments(); |
||
| 58 | |||
| 59 | $options = array(); |
||
| 60 | |||
| 61 | //Provide nice human readable options. |
||
| 62 | if ( $payments ) { |
||
| 63 | $options[0] = $args['placeholder']; |
||
| 64 | foreach ( $payments as $payment ) { |
||
| 65 | |||
| 66 | $options[ absint( $payment->ID ) ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title ); |
||
| 67 | |||
| 68 | } |
||
| 69 | } else { |
||
| 70 | $options[0] = __( 'No donations found.', 'give' ); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | $output = $this->select( array( |
||
| 75 | 'name' => $args['name'], |
||
| 76 | 'selected' => $args['selected'], |
||
| 77 | 'id' => $args['id'], |
||
| 78 | 'class' => $args['class'], |
||
| 79 | 'options' => $options, |
||
| 80 | 'chosen' => $args['chosen'], |
||
| 81 | 'multiple' => $args['multiple'], |
||
| 82 | 'placeholder' => $args['placeholder'], |
||
| 83 | 'select_atts' => $args['select_atts'], |
||
| 84 | 'show_option_all' => false, |
||
| 85 | 'show_option_none' => false |
||
| 86 | ) ); |
||
| 87 | |||
| 88 | return $output; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Give Forms Dropdown |
||
| 93 | * |
||
| 94 | * Renders an HTML Dropdown of all the Give Forms. |
||
| 95 | * |
||
| 96 | * @since 1.0 |
||
| 97 | * @access public |
||
| 98 | * |
||
| 99 | * @param array $args Arguments for the dropdown. |
||
| 100 | * |
||
| 101 | * @return string Give forms dropdown. |
||
| 102 | */ |
||
| 103 | public function forms_dropdown( $args = array() ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Donors Dropdown |
||
| 163 | * |
||
| 164 | * Renders an HTML Dropdown of all customers. |
||
| 165 | * |
||
| 166 | * @since 1.0 |
||
| 167 | * @access public |
||
| 168 | * |
||
| 169 | * @param array $args Arguments for the dropdown. |
||
| 170 | * |
||
| 171 | * @return string Donors dropdown. |
||
| 172 | */ |
||
| 173 | public function donor_dropdown( $args = array() ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Categories Dropdown |
||
| 240 | * |
||
| 241 | * Renders an HTML Dropdown of all the Categories. |
||
| 242 | * |
||
| 243 | * @since 1.0 |
||
| 244 | * @access public |
||
| 245 | * |
||
| 246 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_categories'. |
||
| 247 | * @param int $selected Category to select automatically. Default is 0. |
||
| 248 | * @param array $args Select box options. |
||
| 249 | * |
||
| 250 | * @return string Categories dropdown. |
||
| 251 | */ |
||
| 252 | public function category_dropdown( $name = 'give_forms_categories', $selected = 0, $args = array() ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Tags Dropdown |
||
| 276 | * |
||
| 277 | * Renders an HTML Dropdown of all the Tags. |
||
| 278 | * |
||
| 279 | * @since 1.8 |
||
| 280 | * @access public |
||
| 281 | * |
||
| 282 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_tags'. |
||
| 283 | * @param int $selected Tag to select automatically. Default is 0. |
||
| 284 | * @param array $args Select box options. |
||
| 285 | * |
||
| 286 | * @return string Tags dropdown. |
||
| 287 | */ |
||
| 288 | public function tags_dropdown( $name = 'give_forms_tags', $selected = 0, $args = array() ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Years Dropdown |
||
| 312 | * |
||
| 313 | * Renders an HTML Dropdown of years. |
||
| 314 | * |
||
| 315 | * @since 1.0 |
||
| 316 | * @access public |
||
| 317 | * |
||
| 318 | * @param string $name Name attribute of the dropdown. Default is 'year'. |
||
| 319 | * @param int $selected Year to select automatically. Default is 0. |
||
| 320 | * @param int $years_before Number of years before the current year the dropdown should start with. Default is 5. |
||
| 321 | * @param int $years_after Number of years after the current year the dropdown should finish at. Default is 0. |
||
| 322 | * |
||
| 323 | * @return string Years dropdown. |
||
| 324 | */ |
||
| 325 | public function year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Months Dropdown |
||
| 350 | * |
||
| 351 | * Renders an HTML Dropdown of months. |
||
| 352 | * |
||
| 353 | * @since 1.0 |
||
| 354 | * @access public |
||
| 355 | * |
||
| 356 | * @param string $name Name attribute of the dropdown. Default is 'month'. |
||
| 357 | * @param int $selected Month to select automatically. Default is 0. |
||
| 358 | * |
||
| 359 | * @return string Months dropdown. |
||
| 360 | */ |
||
| 361 | public function month_dropdown( $name = 'month', $selected = 0 ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Dropdown |
||
| 384 | * |
||
| 385 | * Renders an HTML Dropdown. |
||
| 386 | * |
||
| 387 | * @since 1.0 |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @param array $args Arguments for the dropdown. |
||
| 391 | * |
||
| 392 | * @return string The dropdown. |
||
| 393 | */ |
||
| 394 | public function select( $args = array() ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Checkbox |
||
| 477 | * |
||
| 478 | * Renders an HTML Checkbox. |
||
| 479 | * |
||
| 480 | * @since 1.0 |
||
| 481 | * @access public |
||
| 482 | * |
||
| 483 | * @param array $args Arguments for the Checkbox. |
||
| 484 | * |
||
| 485 | * @return string The checkbox. |
||
| 486 | */ |
||
| 487 | public function checkbox( $args = array() ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Text Field |
||
| 514 | * |
||
| 515 | * Renders an HTML Text field. |
||
| 516 | * |
||
| 517 | * @since 1.0 |
||
| 518 | * @access public |
||
| 519 | * |
||
| 520 | * @param array $args Arguments for the text field. |
||
| 521 | * |
||
| 522 | * @return string The text field. |
||
| 523 | */ |
||
| 524 | public function text( $args = array() ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Date Picker |
||
| 578 | * |
||
| 579 | * Renders a date picker field. |
||
| 580 | * |
||
| 581 | * @since 1.5 |
||
| 582 | * @access public |
||
| 583 | * |
||
| 584 | * @param array $args Arguments for the date picker. |
||
| 585 | * |
||
| 586 | * @return string The date picker. |
||
| 587 | */ |
||
| 588 | public function date_field( $args = array() ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Textarea |
||
| 601 | * |
||
| 602 | * Renders an HTML textarea. |
||
| 603 | * |
||
| 604 | * @since 1.0 |
||
| 605 | * @access public |
||
| 606 | * |
||
| 607 | * @param array $args Arguments for the textarea. |
||
| 608 | * |
||
| 609 | * @return string The textarea. |
||
| 610 | */ |
||
| 611 | public function textarea( $args = array() ) { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * User Search Field |
||
| 645 | * |
||
| 646 | * Renders an ajax user search field. |
||
| 647 | * |
||
| 648 | * @since 1.0 |
||
| 649 | * @access public |
||
| 650 | * |
||
| 651 | * @param array $args Arguments for the search field. |
||
| 652 | * |
||
| 653 | * @return string The text field with ajax search. |
||
| 654 | */ |
||
| 655 | public function ajax_user_search( $args = array() ) { |
||
| 707 | |||
| 708 | } |
||
| 709 |
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.