| Conditions | 17 |
| Paths | 1537 |
| Total Lines | 68 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public static function get($args = array()){ |
||
| 22 | $defaults = array( |
||
| 23 | 'type' => 'info', |
||
| 24 | 'class' => '', |
||
| 25 | 'icon' => '', |
||
| 26 | 'heading' => '', |
||
| 27 | 'content' => '', |
||
| 28 | 'footer' => '', |
||
| 29 | 'dismissible'=> false, |
||
| 30 | 'data' => '', |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Parse incoming $args into an array and merge it with $defaults |
||
| 35 | */ |
||
| 36 | $args = wp_parse_args( $args, $defaults ); |
||
| 37 | $output = ''; |
||
| 38 | if ( ! empty( $args['content'] ) ) { |
||
| 39 | $type = sanitize_html_class( $args['type'] ); |
||
| 40 | if($type=='error'){$type='danger';} |
||
| 41 | $icon = !empty($args['icon']) ? "<i class='".esc_attr($args['icon'])."'></i>" : ''; |
||
| 42 | |||
| 43 | // set default icon |
||
| 44 | if(!$icon && $args['icon']!==false && $type){ |
||
| 45 | if($type=='danger'){$icon = '<i class="fas fa-exclamation-circle"></i>';} |
||
| 46 | elseif($type=='warning'){$icon = '<i class="fas fa-exclamation-triangle"></i>';} |
||
| 47 | elseif($type=='success'){$icon = '<i class="fas fa-check-circle"></i>';} |
||
| 48 | elseif($type=='info'){$icon = '<i class="fas fa-info-circle"></i>';} |
||
| 49 | } |
||
| 50 | |||
| 51 | $data = ''; |
||
| 52 | $class = !empty($args['class']) ? esc_attr($args['class']) : ''; |
||
| 53 | if($args['dismissible']){$class .= " alert-dismissible fade show";} |
||
| 54 | |||
| 55 | // open |
||
| 56 | $output .= '<div class="alert alert-' . $type . ' '.$class.'" role="alert" '.$data.'>'; |
||
| 57 | |||
| 58 | // heading |
||
| 59 | if ( ! empty( $args['heading'] ) ) { |
||
| 60 | $output .= '<h4 class="alert-heading">' . $args['heading'] . '</h4>'; |
||
| 61 | } |
||
| 62 | |||
| 63 | // icon |
||
| 64 | if ( ! empty( $icon) ) { |
||
| 65 | $output .= $icon." "; |
||
| 66 | } |
||
| 67 | |||
| 68 | // content |
||
| 69 | $output .= $args['content']; |
||
| 70 | |||
| 71 | // dismissible |
||
| 72 | if($args['dismissible']){ |
||
| 73 | $output .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close">'; |
||
| 74 | $output .= '<span aria-hidden="true">×</span>'; |
||
| 75 | $output .= '</button>'; |
||
| 76 | } |
||
| 77 | |||
| 78 | // footer |
||
| 79 | if ( ! empty( $args['footer'] ) ) { |
||
| 80 | $output .= '<hr>'; |
||
| 81 | $output .= '<p class="mb-0">' . $args['footer'] . '</p>'; |
||
| 82 | } |
||
| 83 | |||
| 84 | // close |
||
| 85 | $output .= '</div>'; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $output; |
||
| 89 | } |
||
| 91 | } |