| Conditions | 23 |
| Paths | > 20000 |
| Total Lines | 123 |
| Code Lines | 64 |
| 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' => 'a', // a, button, badge |
||
| 24 | 'href' => '#', |
||
| 25 | 'new_window' => false, |
||
| 26 | 'class' => 'btn btn-primary', |
||
| 27 | 'id' => '', |
||
| 28 | 'title' => '', |
||
| 29 | 'value' => '', |
||
| 30 | 'content' => '', |
||
| 31 | 'icon' => '', |
||
| 32 | 'hover_content' => '', |
||
| 33 | 'hover_icon' => '', |
||
| 34 | 'new_line_after' => true, |
||
| 35 | 'no_wrap' => true, |
||
| 36 | 'onclick' => '', |
||
| 37 | 'style' => '', |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Parse incoming $args into an array and merge it with $defaults |
||
| 42 | */ |
||
| 43 | $args = wp_parse_args( $args, $defaults ); |
||
| 44 | $output = ''; |
||
| 45 | if ( ! empty( $args['type'] ) ) { |
||
| 46 | $type = $args['type'] != 'a' ? esc_attr($args['type']) : 'a'; |
||
| 47 | |||
| 48 | // open/type |
||
| 49 | if($type=='a'){ |
||
| 50 | $new_window = !empty($args['new_window']) ? ' target="_blank" ' : ''; |
||
| 51 | $output .= '<a href="' . $args['href'] . '"'.$new_window; |
||
| 52 | }elseif($type=='badge'){ |
||
| 53 | $output .= '<span '; |
||
| 54 | }else{ |
||
| 55 | $output .= '<button type="' . $type . '" '; |
||
| 56 | } |
||
| 57 | |||
| 58 | // name |
||
| 59 | if(!empty($args['name'])){ |
||
| 60 | $output .= AUI_Component_Helper::name($args['name']); |
||
| 61 | } |
||
| 62 | |||
| 63 | // id |
||
| 64 | if(!empty($args['id'])){ |
||
| 65 | $output .= AUI_Component_Helper::id($args['id']); |
||
| 66 | } |
||
| 67 | |||
| 68 | // title |
||
| 69 | if(!empty($args['title'])){ |
||
| 70 | $output .= AUI_Component_Helper::title($args['title']); |
||
| 71 | } |
||
| 72 | |||
| 73 | // value |
||
| 74 | if(!empty($args['value'])){ |
||
| 75 | $output .= AUI_Component_Helper::value($args['value']); |
||
| 76 | } |
||
| 77 | |||
| 78 | // class |
||
| 79 | $class = !empty($args['class']) ? $args['class'] : ''; |
||
| 80 | $output .= AUI_Component_Helper::class_attr($class); |
||
| 81 | |||
| 82 | // data-attributes |
||
| 83 | $output .= AUI_Component_Helper::data_attributes($args); |
||
| 84 | |||
| 85 | // aria-attributes |
||
| 86 | $output .= AUI_Component_Helper::aria_attributes($args); |
||
| 87 | |||
| 88 | // onclick, we don't escape this |
||
| 89 | if(!empty($args['onclick'])){ |
||
| 90 | $output .= ' onclick="'.$args['onclick'].'" '; |
||
| 91 | } |
||
| 92 | |||
| 93 | // style, we don't escape this |
||
| 94 | if(!empty($args['style'])){ |
||
| 95 | $output .= ' style="'.$args['style'].'" '; |
||
| 96 | } |
||
| 97 | |||
| 98 | // close opening tag |
||
| 99 | $output .= ' >'; |
||
| 100 | |||
| 101 | |||
| 102 | // hover content |
||
| 103 | $hover_content = false; |
||
| 104 | if(!empty($args['hover_content']) || !empty($args['hover_icon'])){ |
||
| 105 | $output .= "<span class='hover-content'>".AUI_Component_Helper::icon($args['hover_icon'],$args['hover_content']).$args['hover_content']."</span>"; |
||
| 106 | $hover_content = true; |
||
| 107 | } |
||
| 108 | |||
| 109 | // content |
||
| 110 | if($hover_content){$output .= "<span class='hover-content-original'>";} |
||
| 111 | if(!empty($args['content']) || !empty($args['icon'])){ |
||
| 112 | $output .= AUI_Component_Helper::icon($args['icon'],$args['content']).$args['content']; |
||
| 113 | } |
||
| 114 | if($hover_content){$output .= "</span>";} |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | // close |
||
| 119 | if($type=='a'){ |
||
| 120 | $output .= '</a>'; |
||
| 121 | }elseif($type=='badge'){ |
||
| 122 | $output .= '</span>'; |
||
| 123 | }else{ |
||
| 124 | $output .= '</button>'; |
||
| 125 | } |
||
| 126 | |||
| 127 | // maybe new line after? This adds better spacing between buttons. |
||
| 128 | if(!empty($args['new_line_after'])){ |
||
| 129 | $output .= PHP_EOL; |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | // wrap |
||
| 134 | if(!$args['no_wrap']){ |
||
| 135 | $output = AUI_Component_Input::wrap(array( |
||
| 136 | 'content' => $output, |
||
| 137 | )); |
||
| 138 | } |
||
| 139 | |||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 143 | return $output; |
||
| 144 | } |
||
| 146 | } |