| Conditions | 3 |
| Paths | 4 |
| Total Lines | 57 |
| Lines | 12 |
| Ratio | 21.05 % |
| 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 |
||
| 28 | public function render( $atts ) { |
||
| 29 | |||
| 30 | // Extract attributes and set default values. |
||
| 31 | $shortcode_atts = shortcode_atts( array( |
||
| 32 | 'title' => __( 'Related articles', 'wordlift' ), |
||
| 33 | 'with_carousel' => true, |
||
| 34 | 'squared_thumbs' => false, |
||
| 35 | ), $atts ); |
||
| 36 | |||
| 37 | View Code Duplication | foreach ( |
|
|
|
|||
| 38 | array( |
||
| 39 | 'with_carousel', |
||
| 40 | 'squared_thumbs', |
||
| 41 | ) as $att |
||
| 42 | ) { |
||
| 43 | |||
| 44 | // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
||
| 45 | $shortcode_atts[ $att ] = filter_var( |
||
| 46 | $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | /* |
||
| 51 | * Display the navigator only on the single post/page. |
||
| 52 | * |
||
| 53 | * @see https://github.com/insideout10/wordlift-plugin/issues/831 |
||
| 54 | * |
||
| 55 | * @since 3.19.3 we're using `is_singular` instead of `is_single` to allow the navigator also on pages. |
||
| 56 | */ |
||
| 57 | // avoid building the widget when there is a list of posts. |
||
| 58 | if ( ! is_singular() ) { |
||
| 59 | return ''; |
||
| 60 | } |
||
| 61 | |||
| 62 | $current_post = get_post(); |
||
| 63 | |||
| 64 | // Enqueue common shortcode scripts. |
||
| 65 | $this->enqueue_scripts(); |
||
| 66 | |||
| 67 | // Use the registered style which define an optional dependency to font-awesome. |
||
| 68 | // |
||
| 69 | // @see https://github.com/insideout10/wordlift-plugin/issues/699 |
||
| 70 | // wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' ); |
||
| 71 | wp_enqueue_style( 'wordlift-ui' ); |
||
| 72 | |||
| 73 | $navigator_id = uniqid( 'wl-navigator-widget-' ); |
||
| 74 | |||
| 75 | wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array( |
||
| 76 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 77 | 'action' => 'wl_navigator', |
||
| 78 | 'post_id' => $current_post->ID, |
||
| 79 | 'attrs' => $shortcode_atts, |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | return "<div id='$navigator_id' class='wl-navigator-widget'></div>"; |
||
| 84 | } |
||
| 85 | |||
| 87 |
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.