| Conditions | 11 |
| Paths | 64 |
| Total Lines | 65 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 112 | public function select( $args = array() ) { |
||
| 113 | $defaults = array( |
||
| 114 | 'options' => array(), |
||
| 115 | 'name' => null, |
||
| 116 | 'class' => '', |
||
| 117 | 'id' => '', |
||
| 118 | 'selected' => 0, |
||
| 119 | 'placeholder' => null, |
||
| 120 | 'multiple' => false, |
||
| 121 | 'disabled' => false, |
||
| 122 | 'show_option_all' => _x( 'All', 'all dropdown items', 'gravityview' ), |
||
| 123 | 'show_option_none' => _x( 'None', 'no dropdown items', 'gravityview' ), |
||
| 124 | 'data' => array(), |
||
| 125 | ); |
||
| 126 | |||
| 127 | $args = wp_parse_args( $args, $defaults ); |
||
| 128 | |||
| 129 | $data_elements = ''; |
||
| 130 | foreach ( $args['data'] as $key => $value ) { |
||
| 131 | $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"'; |
||
| 132 | } |
||
| 133 | |||
| 134 | if( $args['multiple'] ) { |
||
| 135 | $multiple = ' MULTIPLE'; |
||
| 136 | } else { |
||
| 137 | $multiple = ''; |
||
| 138 | } |
||
| 139 | |||
| 140 | if( $args['placeholder'] ) { |
||
| 141 | $placeholder = $args['placeholder']; |
||
| 142 | } else { |
||
| 143 | $placeholder = ''; |
||
| 144 | } |
||
| 145 | |||
| 146 | $disabled = $args['disabled'] ? ' disabled="disabled"' : ''; |
||
| 147 | $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) ); |
||
| 148 | $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( str_replace( '-', '_', $args['id'] ) ) . '" class="gravityview-select ' . $class . '"' . $multiple . $disabled . ' data-placeholder="' . $placeholder . '"'. $data_elements . '>'; |
||
| 149 | |||
| 150 | if ( ! empty( $args['options'] ) ) { |
||
| 151 | |||
| 152 | if ( $args['show_option_none'] ) { |
||
| 153 | if( $args['multiple'] ) { |
||
| 154 | $selected = selected( true, in_array( -1, $args['selected'] ), false ); |
||
| 155 | } else { |
||
| 156 | $selected = selected( $args['selected'], -1, false ); |
||
| 157 | } |
||
| 158 | $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>'; |
||
| 159 | } |
||
| 160 | |||
| 161 | foreach( $args['options'] as $key => $option ) { |
||
| 162 | |||
| 163 | if( $args['multiple'] && is_array( $args['selected'] ) ) { |
||
| 164 | $selected = selected( true, in_array( $key, $args['selected'], true ), false ); |
||
| 165 | } else { |
||
| 166 | $selected = selected( $args['selected'], $key, false ); |
||
| 167 | } |
||
| 168 | |||
| 169 | $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>'; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | $output .= '</select>'; |
||
| 174 | |||
| 175 | return $output; |
||
| 176 | } |
||
| 177 | } |
||
| 178 |