| Conditions | 12 |
| Paths | 648 |
| Total Lines | 77 |
| Code Lines | 60 |
| Lines | 10 |
| Ratio | 12.99 % |
| 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 |
||
| 82 | function form( $instance ) { |
||
| 83 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
| 84 | |||
| 85 | $title = stripslashes( $instance['title'] ); |
||
| 86 | $display = $instance['display']; |
||
| 87 | $format = $instance['format']; |
||
| 88 | $image_size = isset( $instance['imagesize'] ) ? $instance['imagesize'] : 0 ; |
||
| 89 | $image_color = isset( $instance['imagecolor'] ) ? $instance['imagecolor'] : 'red'; |
||
| 90 | |||
| 91 | echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title:', 'jetpack' ) . ' |
||
| 92 | <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" /> |
||
| 93 | </label></p>'; |
||
| 94 | |||
| 95 | $displays = array( |
||
| 96 | 'posts' => __( 'Posts', 'jetpack' ), |
||
| 97 | 'comments' => __( 'Comments', 'jetpack' ), |
||
| 98 | 'posts-comments' => __( 'Posts & Comments', 'jetpack' ) |
||
| 99 | ); |
||
| 100 | echo '<p><label for="' . $this->get_field_id( 'display' ) . '">' . esc_html__( 'Feed(s) to Display:', 'jetpack' ) . ' |
||
| 101 | <select class="widefat" id="' . $this->get_field_id( 'display' ) . '" name="' . $this->get_field_name( 'display' ) . '">'; |
||
| 102 | foreach ( $displays as $display_option => $label ) { |
||
| 103 | echo '<option value="' . esc_attr( $display_option ) . '"'; |
||
| 104 | if ( $display_option == $display ) echo ' selected="selected"'; |
||
| 105 | echo '>' . esc_html( $label ) . '</option>' . "\n"; |
||
| 106 | } |
||
| 107 | echo '</select></label></p>'; |
||
| 108 | |||
| 109 | $formats = array( |
||
| 110 | 'text' => __( 'Text Link', 'jetpack' ), |
||
| 111 | 'image' => __( 'Image Link', 'jetpack' ), |
||
| 112 | 'text-image' => __( 'Text & Image Links', 'jetpack' ) |
||
| 113 | ); |
||
| 114 | echo '<p><label for="' . $this->get_field_id( 'format' ) . '">' . _x( 'Format:', 'Noun', 'jetpack' ) . ' |
||
| 115 | <select class="widefat" id="' . $this->get_field_id( 'format' ) . '" name="' . $this->get_field_name( 'format' ) . '" onchange="if ( this.value == \'text\' ) jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeOut(); else jQuery( \'#' . $this->get_field_id( 'image-settings' ) . '\' ).fadeIn();">'; |
||
| 116 | View Code Duplication | foreach ( $formats as $format_option => $label ) { |
|
| 117 | echo '<option value="' . esc_attr( $format_option ) . '"'; |
||
| 118 | if ( $format_option == $format ) echo ' selected="selected"'; |
||
| 119 | echo '>' . esc_html( $label ) . '</option>' . "\n"; |
||
| 120 | } |
||
| 121 | echo '</select></label></p>'; |
||
| 122 | |||
| 123 | echo '<div id="' . $this->get_field_id( 'image-settings' ) . '"'; |
||
| 124 | if ( 'text' == $format ) echo ' style="display: none;"'; |
||
| 125 | echo '><h3>' . esc_html__( 'Image Settings:', 'jetpack' ) . '</h3>'; |
||
| 126 | |||
| 127 | $sizes = array( |
||
| 128 | 'small' => __( 'Small', 'jetpack' ), |
||
| 129 | 'medium' => __( 'Medium', 'jetpack' ), |
||
| 130 | 'large' => __( 'Large', 'jetpack' ) |
||
| 131 | ); |
||
| 132 | echo '<p><label for="' . $this->get_field_id( 'imagesize' ) . '">' . esc_html__( 'Image Size:', 'jetpack' ) . ' |
||
| 133 | <select class="widefat" id="' . $this->get_field_id( 'imagesize' ) . '" name="' . $this->get_field_name( 'imagesize' ) . '">'; |
||
| 134 | View Code Duplication | foreach ( $sizes as $size => $label ) { |
|
| 135 | echo '<option value="' . esc_attr( $size) . '"'; |
||
| 136 | if ( $size == $image_size ) echo ' selected="selected"'; |
||
| 137 | echo '>' . esc_html( $label ) . '</option>' . "\n"; |
||
| 138 | } |
||
| 139 | echo '</select></label></p>'; |
||
| 140 | |||
| 141 | $colors = array( |
||
| 142 | 'red' => __( 'Red', 'jetpack' ), |
||
| 143 | 'orange' => __( 'Orange', 'jetpack' ), |
||
| 144 | 'green' => __( 'Green', 'jetpack' ), |
||
| 145 | 'blue' => __( 'Blue', 'jetpack' ), |
||
| 146 | 'purple' => __( 'Purple', 'jetpack' ), |
||
| 147 | 'pink' => __( 'Pink', 'jetpack' ), |
||
| 148 | 'silver' => __( 'Silver', 'jetpack' ), |
||
| 149 | ); |
||
| 150 | echo '<p><label for="' . $this->get_field_id( 'imagecolor' ) . '">' . esc_html__( 'Image Color:', 'jetpack' ) . ' |
||
| 151 | <select class="widefat" id="' . $this->get_field_id( 'imagecolor' ) . '" name="' . $this->get_field_name( 'imagecolor' ) . '">'; |
||
| 152 | foreach ( $colors as $color => $label ) { |
||
| 153 | echo '<option value="' . esc_attr( $color) . '"'; |
||
| 154 | if ( $color == $image_color ) echo ' selected="selected"'; |
||
| 155 | echo '>' . esc_html( $label ) . '</option>' . "\n"; |
||
| 156 | } |
||
| 157 | echo '</select></label></p></div>'; |
||
| 158 | } |
||
| 159 | |||
| 226 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: