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 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 | * Instance. |
||
27 | * |
||
28 | * @since 1.0 |
||
29 | * @access private |
||
30 | * @var |
||
31 | */ |
||
32 | static private $instance; |
||
33 | |||
34 | /** |
||
35 | * Singleton pattern. |
||
36 | * |
||
37 | * @since 1.0 |
||
38 | * @access private |
||
39 | */ |
||
40 | private function __construct() { |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Get instance. |
||
46 | * |
||
47 | * @since 1.0 |
||
48 | * @access public |
||
49 | * @return Give_HTML_Elements |
||
50 | */ |
||
51 | public static function get_instance() { |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Donations Dropdown |
||
62 | * |
||
63 | * Renders an HTML Dropdown of all the donations. |
||
64 | * |
||
65 | * @since 1.0 |
||
66 | * @access public |
||
67 | * |
||
68 | * @param array $args Arguments for the dropdown. |
||
69 | * |
||
70 | * @return string Donations dropdown. |
||
71 | */ |
||
72 | public function donations_dropdown( $args = array() ) { |
||
123 | |||
124 | /** |
||
125 | * Give Forms Dropdown |
||
126 | * |
||
127 | * Renders an HTML Dropdown of all the Give Forms. |
||
128 | * |
||
129 | * @since 1.0 |
||
130 | * @access public |
||
131 | * |
||
132 | * @param array $args Arguments for the dropdown. |
||
133 | * |
||
134 | * @return string Give forms dropdown. |
||
135 | */ |
||
136 | public function forms_dropdown( $args = array() ) { |
||
222 | |||
223 | /** |
||
224 | * Donors Dropdown |
||
225 | * |
||
226 | * Renders an HTML Dropdown of all donors. |
||
227 | * |
||
228 | * @since 1.0 |
||
229 | * @access public |
||
230 | * |
||
231 | * @param array $args Arguments for the dropdown. |
||
232 | * |
||
233 | * @return string Donors dropdown. |
||
234 | */ |
||
235 | public function donor_dropdown( $args = array() ) { |
||
299 | |||
300 | /** |
||
301 | * Categories Dropdown |
||
302 | * |
||
303 | * Renders an HTML Dropdown of all the Categories. |
||
304 | * |
||
305 | * @since 1.0 |
||
306 | * @access public |
||
307 | * |
||
308 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_categories'. |
||
309 | * @param int $selected Category to select automatically. Default is 0. |
||
310 | * @param array $args Select box options. |
||
311 | * |
||
312 | * @return string Categories dropdown. |
||
313 | */ |
||
314 | View Code Duplication | public function category_dropdown( $name = 'give_forms_categories', $selected = 0, $args = array() ) { |
|
315 | $categories = get_terms( 'give_forms_category', apply_filters( 'give_forms_category_dropdown', array() ) ); |
||
316 | |||
317 | $options = array(); |
||
318 | |||
319 | foreach ( $categories as $category ) { |
||
320 | $options[ absint( $category->term_id ) ] = esc_html( $category->name ); |
||
321 | } |
||
322 | |||
323 | $output = $this->select( wp_parse_args( $args, array( |
||
324 | 'name' => $name, |
||
325 | 'selected' => $selected, |
||
326 | 'options' => $options, |
||
327 | 'show_option_all' => esc_html__( 'All Categories', 'give' ), |
||
328 | 'show_option_none' => false, |
||
329 | ) ) ); |
||
330 | |||
331 | return $output; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Tags Dropdown |
||
336 | * |
||
337 | * Renders an HTML Dropdown of all the Tags. |
||
338 | * |
||
339 | * @since 1.8 |
||
340 | * @access public |
||
341 | * |
||
342 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_tags'. |
||
343 | * @param int $selected Tag to select automatically. Default is 0. |
||
344 | * @param array $args Select box options. |
||
345 | * |
||
346 | * @return string Tags dropdown. |
||
347 | */ |
||
348 | View Code Duplication | public function tags_dropdown( $name = 'give_forms_tags', $selected = 0, $args = array() ) { |
|
349 | $tags = get_terms( 'give_forms_tag', apply_filters( 'give_forms_tag_dropdown', array() ) ); |
||
350 | |||
351 | $options = array(); |
||
352 | |||
353 | foreach ( $tags as $tag ) { |
||
354 | $options[ absint( $tag->term_id ) ] = esc_html( $tag->name ); |
||
355 | } |
||
356 | |||
357 | $output = $this->select( wp_parse_args( $args, array( |
||
358 | 'name' => $name, |
||
359 | 'selected' => $selected, |
||
360 | 'options' => $options, |
||
361 | 'show_option_all' => esc_html__( 'All Tags', 'give' ), |
||
362 | 'show_option_none' => false, |
||
363 | ) ) ); |
||
364 | |||
365 | return $output; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Years Dropdown |
||
370 | * |
||
371 | * Renders an HTML Dropdown of years. |
||
372 | * |
||
373 | * @since 1.0 |
||
374 | * @access public |
||
375 | * |
||
376 | * @param string $name Name attribute of the dropdown. Default is 'year'. |
||
377 | * @param int $selected Year to select automatically. Default is 0. |
||
378 | * @param int $years_before Number of years before the current year the dropdown should start with. Default is 5. |
||
379 | * @param int $years_after Number of years after the current year the dropdown should finish at. Default is 0. |
||
380 | * |
||
381 | * @return string Years dropdown. |
||
382 | */ |
||
383 | public function year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) { |
||
405 | |||
406 | /** |
||
407 | * Months Dropdown |
||
408 | * |
||
409 | * Renders an HTML Dropdown of months. |
||
410 | * |
||
411 | * @since 1.0 |
||
412 | * @access public |
||
413 | * |
||
414 | * @param string $name Name attribute of the dropdown. Default is 'month'. |
||
415 | * @param int $selected Month to select automatically. Default is 0. |
||
416 | * |
||
417 | * @return string Months dropdown. |
||
418 | */ |
||
419 | public function month_dropdown( $name = 'month', $selected = 0 ) { |
||
439 | |||
440 | /** |
||
441 | * Dropdown |
||
442 | * |
||
443 | * Renders an HTML Dropdown. |
||
444 | * |
||
445 | * @since 1.0 |
||
446 | * @access public |
||
447 | * |
||
448 | * @param array $args Arguments for the dropdown. |
||
449 | * |
||
450 | * @return string The dropdown. |
||
451 | */ |
||
452 | public function select( $args = array() ) { |
||
538 | |||
539 | /** |
||
540 | * Checkbox |
||
541 | * |
||
542 | * Renders an HTML Checkbox. |
||
543 | * |
||
544 | * @since 1.0 |
||
545 | * @access public |
||
546 | * |
||
547 | * @param array $args Arguments for the Checkbox. |
||
548 | * |
||
549 | * @return string The checkbox. |
||
550 | */ |
||
551 | public function checkbox( $args = array() ) { |
||
575 | |||
576 | /** |
||
577 | * Text Field |
||
578 | * |
||
579 | * Renders an HTML Text field. |
||
580 | * |
||
581 | * @since 1.0 |
||
582 | * @access public |
||
583 | * |
||
584 | * @param array $args Arguments for the text field. |
||
585 | * |
||
586 | * @return string The text field. |
||
587 | */ |
||
588 | public function text( $args = array() ) { |
||
639 | |||
640 | /** |
||
641 | * Date Picker |
||
642 | * |
||
643 | * Renders a date picker field. |
||
644 | * |
||
645 | * @since 1.5 |
||
646 | * @access public |
||
647 | * |
||
648 | * @param array $args Arguments for the date picker. |
||
649 | * |
||
650 | * @return string The date picker. |
||
651 | */ |
||
652 | public function date_field( $args = array() ) { |
||
662 | |||
663 | /** |
||
664 | * Textarea |
||
665 | * |
||
666 | * Renders an HTML textarea. |
||
667 | * |
||
668 | * @since 1.0 |
||
669 | * @access public |
||
670 | * |
||
671 | * @param array $args Arguments for the textarea. |
||
672 | * |
||
673 | * @return string The textarea. |
||
674 | */ |
||
675 | public function textarea( $args = array() ) { |
||
706 | |||
707 | /** |
||
708 | * User Search Field |
||
709 | * |
||
710 | * Renders an ajax user search field. |
||
711 | * |
||
712 | * @since 1.0 |
||
713 | * @access public |
||
714 | * |
||
715 | * @param array $args Arguments for the search field. |
||
716 | * |
||
717 | * @return string The text field with ajax search. |
||
718 | */ |
||
719 | public function ajax_user_search( $args = array() ) { |
||
787 | |||
788 | } |
||
789 |