AUI_Component_Button::get()   F
last analyzed

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 130
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 68
nc 393217
nop 1
dl 0
loc 130
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A component class for rendering a bootstrap button.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI_Component_Button {
13
14
	/**
15
	 * Build the component.
16
	 *
17
	 * @param array $args
18
	 *
19
	 * @return string The rendered component.
20
	 */
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
			'extra_attributes'  => array(), // an array of extra attributes
39
			'icon_extra_attributes'  => array() // an array of icon extra attributes
40
		);
41
42
		/**
43
		 * Parse incoming $args into an array and merge it with $defaults
44
		 */
45
		$args   = wp_parse_args( $args, $defaults );
46
		$output = '';
47
		if ( ! empty( $args['type'] ) ) {
48
			$type = $args['type'] != 'a' ? esc_attr($args['type']) : 'a';
49
50
			// open/type
51
			if($type=='a'){
52
				$new_window = !empty($args['new_window']) ? ' target="_blank" ' : '';
53
				$output .= '<a href="' . $args['href'] . '"'.$new_window;
54
			}elseif($type=='badge'){
55
				$output .= '<span ';
56
			}else{
57
				$output .= '<button type="' . $type . '" ';
58
			}
59
60
			// name
61
			if(!empty($args['name'])){
62
				$output .= AUI_Component_Helper::name($args['name']);
63
			}
64
65
			// id
66
			if(!empty($args['id'])){
67
				$output .= AUI_Component_Helper::id($args['id']);
68
			}
69
70
			// title
71
			if(!empty($args['title'])){
72
				$output .= AUI_Component_Helper::title($args['title']);
73
			}
74
75
			// value
76
			if(!empty($args['value'])){
77
				$output .= AUI_Component_Helper::value($args['value']);
78
			}
79
80
			// class
81
			$class = !empty($args['class']) ? $args['class'] : '';
82
			$output .= AUI_Component_Helper::class_attr($class);
83
			
84
			// data-attributes
85
			$output .= AUI_Component_Helper::data_attributes($args);
86
87
			// aria-attributes
88
			$output .= AUI_Component_Helper::aria_attributes($args);
89
90
			// extra attributes
91
			if(!empty($args['extra_attributes'])){
92
				$output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']);
93
			}
94
95
			// onclick, we don't escape this
96
			if(!empty($args['onclick'])){
97
				$output .= ' onclick="'.$args['onclick'].'" ';
98
			}
99
100
			// style, we don't escape this
101
			if(!empty($args['style'])){
102
				$output .= ' style="'.$args['style'].'" ';
103
			}
104
105
			// close opening tag
106
			$output .= ' >';
107
108
109
			// hover content
110
			$hover_content = false;
111
			if(!empty($args['hover_content']) || !empty($args['hover_icon'])){
112
				$output .= "<span class='hover-content'>".AUI_Component_Helper::icon($args['hover_icon'],$args['hover_content']).$args['hover_content']."</span>";
113
				$hover_content = true;
114
			}
115
			
116
			// content
117
			if($hover_content){$output .= "<span class='hover-content-original'>";}
118
			if(!empty($args['content']) || !empty($args['icon'])){
119
				$output .= AUI_Component_Helper::icon($args['icon'],$args['content'],$args['icon_extra_attributes']).$args['content'];
120
			}
121
			if($hover_content){$output .= "</span>";}
122
					
123
124
125
			// close
126
			if($type=='a'){
127
				$output .= '</a>';
128
			}elseif($type=='badge'){
129
				$output .= '</span>';
130
			}else{
131
				$output .= '</button>';
132
			}
133
134
			// maybe new line after?  This adds better spacing between buttons.
135
			if(!empty($args['new_line_after'])){
136
				$output .= PHP_EOL;
137
			}
138
139
140
			// wrap
141
			if(!$args['no_wrap']){
142
				$output = AUI_Component_Input::wrap(array(
143
					'content' => $output,
144
				));
145
			}
146
147
148
		}
149
150
		return $output;
151
	}
152
153
}