| Conditions | 12 |
| Paths | 96 |
| Total Lines | 71 |
| 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 |
||
| 32 | public static function select( $args = array() ) { |
||
| 33 | |||
| 34 | $defaults = array( |
||
| 35 | 'options' => array(), |
||
| 36 | 'name' => null, |
||
| 37 | 'class' => '', |
||
| 38 | 'id' => '', |
||
| 39 | 'selected' => 0, |
||
| 40 | 'chosen' => false, |
||
| 41 | 'placeholder' => null, |
||
| 42 | 'multiple' => false, |
||
| 43 | 'show_option_all' => _x( 'All', 'all dropdown items', 'google-maps-builder' ), |
||
| 44 | 'show_option_none' => _x( 'None', 'no dropdown items', 'google-maps-builder' ) |
||
| 45 | ); |
||
| 46 | |||
| 47 | $args = wp_parse_args( $args, $defaults ); |
||
| 48 | |||
| 49 | if ( $args['multiple'] ) { |
||
| 50 | $multiple = ' MULTIPLE'; |
||
| 51 | } else { |
||
| 52 | $multiple = ''; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( $args['chosen'] ) { |
||
| 56 | $args['class'] .= 'gmb-select-chosen'; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ( $args['placeholder'] ) { |
||
| 60 | $placeholder = $args['placeholder']; |
||
| 61 | } else { |
||
| 62 | $placeholder = ''; |
||
| 63 | } |
||
| 64 | |||
| 65 | $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( sanitize_key( str_replace( '-', '_', $args['id'] ) ) ) . '" class="gmb-select ' . esc_attr( $args['class'] ) . '"' . $multiple . ' data-placeholder="' . $placeholder . '">'; |
||
| 66 | |||
| 67 | if ( $args['show_option_all'] ) { |
||
| 68 | if ( $args['multiple'] ) { |
||
| 69 | $selected = selected( true, in_array( 0, $args['selected'] ), false ); |
||
| 70 | } else { |
||
| 71 | $selected = selected( $args['selected'], 0, false ); |
||
| 72 | } |
||
| 73 | $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>'; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ( ! empty( $args['options'] ) ) { |
||
| 77 | |||
| 78 | if ( $args['show_option_none'] ) { |
||
| 79 | if ( $args['multiple'] ) { |
||
| 80 | $selected = selected( true, in_array( - 1, $args['selected'] ), false ); |
||
| 81 | } else { |
||
| 82 | $selected = selected( $args['selected'], - 1, false ); |
||
| 83 | } |
||
| 84 | $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>'; |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ( $args['options'] as $key => $option ) { |
||
| 88 | |||
| 89 | if ( $args['multiple'] && is_array( $args['selected'] ) ) { |
||
| 90 | $selected = selected( true, in_array( $key, $args['selected'] ), false ); |
||
| 91 | } else { |
||
| 92 | $selected = selected( $args['selected'], $key, false ); |
||
| 93 | } |
||
| 94 | |||
| 95 | $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>'; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $output .= '</select>'; |
||
| 100 | |||
| 101 | return $output; |
||
| 102 | } |
||
| 103 | |||
| 149 |