| Conditions | 19 |
| Paths | 242 |
| Total Lines | 109 |
| Lines | 7 |
| Ratio | 6.42 % |
| 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 |
||
| 31 | function soundcloud_shortcode( $atts, $content = null ) { |
||
| 32 | |||
| 33 | // Custom shortcode options. |
||
| 34 | $shortcode_options = array_merge( |
||
| 35 | array( 'url' => trim( $content ) ), |
||
| 36 | is_array( $atts ) ? $atts : array() |
||
| 37 | ); |
||
| 38 | |||
| 39 | // The "url" option is required. |
||
| 40 | View Code Duplication | if ( empty( $shortcode_options['url'] ) ) { |
|
| 41 | if ( current_user_can( 'edit_posts' ) ) { |
||
| 42 | return esc_html__( 'Please specify a Soundcloud URL.', 'jetpack' ); |
||
| 43 | } else { |
||
| 44 | return '<!-- Missing Soundcloud URL -->'; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | // Turn shortcode option "param" (param=value¶m2=value) into array of params. |
||
| 49 | $shortcode_params = array(); |
||
| 50 | if ( isset( $shortcode_options['params'] ) ) { |
||
| 51 | parse_str( html_entity_decode( $shortcode_options['params'] ), $shortcode_params ); |
||
| 52 | $shortcode_options = array_merge( |
||
| 53 | $shortcode_options, |
||
| 54 | $shortcode_params |
||
| 55 | ); |
||
| 56 | unset( $shortcode_options['params'] ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $options = shortcode_atts( |
||
| 60 | // This list used to include an 'iframe' option. We don't include it anymore as we don't support the Flash player anymore. |
||
| 61 | array( |
||
| 62 | 'url' => '', |
||
| 63 | 'width' => soundcloud_get_option( 'player_width' ), |
||
| 64 | 'height' => soundcloud_url_has_tracklist( $shortcode_options['url'] ) ? soundcloud_get_option( 'player_height_multi' ) : soundcloud_get_option( 'player_height' ), |
||
| 65 | 'auto_play' => soundcloud_get_option( 'auto_play' ), |
||
| 66 | 'hide_related' => false, |
||
| 67 | 'visual' => false, |
||
| 68 | 'show_comments' => soundcloud_get_option( 'show_comments' ), |
||
| 69 | 'color' => soundcloud_get_option( 'color' ), |
||
| 70 | 'show_user' => false, |
||
| 71 | 'show_reposts' => false, |
||
| 72 | ), |
||
| 73 | $shortcode_options, |
||
| 74 | 'soundcloud' |
||
| 75 | ); |
||
| 76 | |||
| 77 | // "width" needs to be an integer. |
||
| 78 | if ( ! empty( $options['width'] ) && ! preg_match( '/^\d+$/', $options['width'] ) ) { |
||
| 79 | // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone. |
||
| 80 | $options['width'] = 0; |
||
| 81 | } |
||
| 82 | // Set default width if not defined. |
||
| 83 | $width = ! empty( $options['width'] ) ? absint( $options['width'] ) : '100%'; |
||
| 84 | |||
| 85 | // Set default height if not defined. |
||
| 86 | if ( |
||
| 87 | empty( $options['height'] ) |
||
| 88 | || ( |
||
| 89 | // "height" needs to be an integer. |
||
| 90 | ! empty( $options['height'] ) |
||
| 91 | && ! preg_match( '/^\d+$/', $options['height'] ) |
||
| 92 | ) |
||
| 93 | ) { |
||
| 94 | if ( |
||
| 95 | soundcloud_url_has_tracklist( $options['url'] ) |
||
| 96 | || 'true' === $options['visual'] |
||
| 97 | ) { |
||
| 98 | $height = 450; |
||
| 99 | } else { |
||
| 100 | $height = 166; |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | $height = absint( $options['height'] ); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Set visual to false when displaying the smallest player. |
||
| 107 | if ( '20' === $options['height'] ) { |
||
| 108 | $options['visual'] = false; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Build our list of Souncloud parameters. |
||
| 112 | $query_args = array( |
||
| 113 | 'url' => rawurlencode( $options['url'] ), |
||
| 114 | ); |
||
| 115 | |||
| 116 | // Add our options, if they are set to true or false. |
||
| 117 | foreach ( $options as $name => $value ) { |
||
| 118 | if ( 'true' === $value ) { |
||
| 119 | $query_args[ $name ] = 'true'; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ( 'false' === $value || false === $value ) { |
||
| 123 | $query_args[ $name ] = 'false'; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Build final embed URL. |
||
| 128 | $url = add_query_arg( |
||
| 129 | $query_args, |
||
| 130 | 'https://w.soundcloud.com/player/' |
||
| 131 | ); |
||
| 132 | |||
| 133 | return sprintf( |
||
| 134 | '<iframe width="%1$s" height="%2$d" scrolling="no" frameborder="no" src="%3$s"></iframe>', |
||
| 135 | esc_attr( $width ), |
||
| 136 | esc_attr( $height ), |
||
| 137 | $url |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | add_shortcode( 'soundcloud', 'soundcloud_shortcode' ); |
||
| 233 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.