Conditions | 12 |
Paths | 34 |
Total Lines | 72 |
Code Lines | 53 |
Lines | 16 |
Ratio | 22.22 % |
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 |
||
194 | public function form( $instance ) { |
||
195 | |||
196 | if ( empty( $this->settings ) ) { |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | foreach ( $this->settings as $key => $setting ) { |
||
201 | |||
202 | $class = isset( $setting['class'] ) ? $setting['class'] : ''; |
||
203 | $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; |
||
204 | |||
205 | switch ( $setting['type'] ) { |
||
206 | |||
207 | View Code Duplication | case 'text' : |
|
208 | ?> |
||
209 | <p> |
||
210 | <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> |
||
211 | <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" /> |
||
212 | </p> |
||
213 | <?php |
||
214 | break; |
||
215 | |||
216 | case 'number' : |
||
217 | ?> |
||
218 | <p> |
||
219 | <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> |
||
220 | <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" /> |
||
221 | </p> |
||
222 | <?php |
||
223 | break; |
||
224 | |||
225 | case 'select' : |
||
226 | ?> |
||
227 | <p> |
||
228 | <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> |
||
229 | <select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>"> |
||
230 | <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?> |
||
231 | <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option> |
||
232 | <?php endforeach; ?> |
||
233 | </select> |
||
234 | </p> |
||
235 | <?php |
||
236 | break; |
||
237 | |||
238 | case 'textarea' : |
||
239 | ?> |
||
240 | <p> |
||
241 | <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> |
||
242 | <textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea> |
||
243 | <?php if ( isset( $setting['desc'] ) ) : ?> |
||
244 | <small><?php echo esc_html( $setting['desc'] ); ?></small> |
||
245 | <?php endif; ?> |
||
246 | </p> |
||
247 | <?php |
||
248 | break; |
||
249 | |||
250 | View Code Duplication | case 'checkbox' : |
|
251 | ?> |
||
252 | <p> |
||
253 | <input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> /> |
||
254 | <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> |
||
255 | </p> |
||
256 | <?php |
||
257 | break; |
||
258 | |||
259 | // Default: run an action |
||
260 | default : |
||
261 | do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance ); |
||
262 | break; |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.