| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 224 |
| Lines | 23 |
| Ratio | 10.27 % |
| 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 |
||
| 18 | function shortcode_handler_bandcamp( $atts ) { |
||
| 19 | // there are no default values, but specify here anyway to explicitly list supported atts. |
||
| 20 | $attributes = shortcode_atts( |
||
| 21 | array( |
||
| 22 | 'album' => null, // integer album id. |
||
| 23 | 'track' => null, // integer track id. |
||
| 24 | 'video' => null, // integer track id for video player. |
||
| 25 | 'size' => 'venti', // one of the supported sizes. |
||
| 26 | 'bgcol' => 'FFFFFF', // hex, no '#' prefix. |
||
| 27 | 'linkcol' => null, // hex, no '#' prefix. |
||
| 28 | 'layout' => null, // encoded layout url. |
||
| 29 | 'width' => null, // integer with optional "%". |
||
| 30 | 'height' => null, // integer with optional "%". |
||
| 31 | 'notracklist' => null, // may be string "true" (defaults false). |
||
| 32 | 'tracklist' => null, // may be string "false" (defaults true). |
||
| 33 | 'artwork' => null, // may be string "false" (alternately: "none") or "small" (default is large). |
||
| 34 | 'minimal' => null, // may be string "true" (defaults false). |
||
| 35 | 'theme' => null, // may be theme identifier string ("light"|"dark" so far). |
||
| 36 | 'package' => null, // integer package id. |
||
| 37 | 't' => null, // integer track number. |
||
| 38 | 'tracks' => null, // comma separated list of allowed tracks. |
||
| 39 | 'esig' => null, // hex, no '#' prefix. |
||
| 40 | ), |
||
| 41 | $atts, |
||
| 42 | 'bandcamp' |
||
| 43 | ); |
||
| 44 | |||
| 45 | $sizes = array( |
||
| 46 | 'venti' => array( |
||
| 47 | 'width' => 400, |
||
| 48 | 'height' => 100, |
||
| 49 | ), |
||
| 50 | 'grande' => array( |
||
| 51 | 'width' => 300, |
||
| 52 | 'height' => 100, |
||
| 53 | ), |
||
| 54 | 'grande2' => array( |
||
| 55 | 'width' => 300, |
||
| 56 | 'height' => 355, |
||
| 57 | ), |
||
| 58 | 'grande3' => array( |
||
| 59 | 'width' => 300, |
||
| 60 | 'height' => 415, |
||
| 61 | ), |
||
| 62 | 'tall_album' => array( |
||
| 63 | 'width' => 150, |
||
| 64 | 'height' => 295, |
||
| 65 | ), |
||
| 66 | 'tall_track' => array( |
||
| 67 | 'width' => 150, |
||
| 68 | 'height' => 270, |
||
| 69 | ), |
||
| 70 | 'tall2' => array( |
||
| 71 | 'width' => 150, |
||
| 72 | 'height' => 450, |
||
| 73 | ), |
||
| 74 | 'short' => array( |
||
| 75 | 'width' => 46, |
||
| 76 | 'height' => 23, |
||
| 77 | ), |
||
| 78 | 'large' => array( |
||
| 79 | 'width' => 350, |
||
| 80 | 'height' => 470, |
||
| 81 | ), |
||
| 82 | 'medium' => array( |
||
| 83 | 'width' => 450, |
||
| 84 | 'height' => 120, |
||
| 85 | ), |
||
| 86 | 'small' => array( |
||
| 87 | 'width' => 350, |
||
| 88 | 'height' => 42, |
||
| 89 | ), |
||
| 90 | ); |
||
| 91 | |||
| 92 | $sizekey = $attributes['size']; |
||
| 93 | $height = null; |
||
| 94 | $width = null; |
||
| 95 | |||
| 96 | $is_video = false; |
||
| 97 | |||
| 98 | /* |
||
| 99 | * Build iframe url. For audio players, args are appended as |
||
| 100 | * extra path segments for historical reasons having to |
||
| 101 | * do with an IE-only flash bug which required this URL |
||
| 102 | * to contain no querystring. Delay the actual joining |
||
| 103 | * of args into a string until after we decide if it's |
||
| 104 | * a video player or an audio player |
||
| 105 | */ |
||
| 106 | $argparts = array(); |
||
| 107 | |||
| 108 | if ( ! isset( $attributes['album'] ) && ! isset( $attributes['track'] ) && ! isset( $attributes['video'] ) ) { |
||
| 109 | return "[bandcamp: shortcode must include 'track', 'album', or 'video' param]"; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( isset( $attributes['track'] ) && is_numeric( $attributes['track'] ) ) { |
||
| 113 | $track = esc_attr( $attributes['track'] ); |
||
| 114 | array_push( $argparts, "track={$track}" ); |
||
| 115 | } elseif ( isset( $attributes['video'] ) && is_numeric( $attributes['video'] ) ) { |
||
| 116 | $track = esc_attr( $attributes['video'] ); // videos are referenced by track id. |
||
| 117 | $url = '//bandcamp.com/EmbeddedPlayer/v=2'; |
||
|
|
|||
| 118 | $is_video = true; |
||
| 119 | array_push( $argparts, "track={$track}" ); |
||
| 120 | } |
||
| 121 | if ( isset( $attributes['album'] ) && is_numeric( $attributes['album'] ) ) { |
||
| 122 | $album = esc_attr( $attributes['album'] ); |
||
| 123 | array_push( $argparts, "album={$album}" ); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( 'tall' === $sizekey ) { |
||
| 127 | if ( isset( $attributes['album'] ) ) { |
||
| 128 | $sizekey .= '_album'; |
||
| 129 | } else { |
||
| 130 | $sizekey .= '_track'; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // if size specified that we don't recognize, fall back on venti. |
||
| 135 | if ( empty( $sizes[ $sizekey ] ) ) { |
||
| 136 | $sizekey = 'venti'; |
||
| 137 | $attributes['size'] = 'venti'; |
||
| 138 | } |
||
| 139 | |||
| 140 | /* |
||
| 141 | * use strict regex for digits + optional % instead of absint for height/width |
||
| 142 | * 'width' and 'height' params in the iframe url get the exact string from the shortcode |
||
| 143 | * args, whereas the inline style attribute must have "px" added to it if it has no "%" |
||
| 144 | */ |
||
| 145 | View Code Duplication | if ( isset( $attributes['width'] ) && preg_match( '|^([0-9]+)(%)?$|', $attributes['width'], $matches ) ) { |
|
| 146 | $width = $attributes['width']; |
||
| 147 | $csswidth = $attributes['width']; |
||
| 148 | if ( count( $matches ) < 3 ) { |
||
| 149 | $csswidth .= 'px'; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | View Code Duplication | if ( isset( $attributes['height'] ) && preg_match( '|^([0-9]+)(%)?$|', $attributes['height'], $matches ) ) { |
|
| 153 | $height = $attributes['height']; |
||
| 154 | $cssheight = $attributes['height']; |
||
| 155 | if ( count( $matches ) < 3 ) { |
||
| 156 | $cssheight .= 'px'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | if ( ! $height ) { |
||
| 161 | $height = $sizes[ $sizekey ]['height']; |
||
| 162 | $cssheight = $height . 'px'; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ( ! $width ) { |
||
| 166 | $width = $sizes[ $sizekey ]['width']; |
||
| 167 | $csswidth = $width . 'px'; |
||
| 168 | } |
||
| 169 | |||
| 170 | View Code Duplication | if ( isset( $attributes['layout'] ) ) { |
|
| 171 | array_push( $argparts, "layout={$attributes['layout']}" ); |
||
| 172 | } elseif ( isset( $attributes['size'] ) && preg_match( '|^[a-zA-Z0-9]+$|', $attributes['size'] ) ) { |
||
| 173 | array_push( $argparts, "size={$attributes['size']}" ); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ( isset( $attributes['bgcol'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['bgcol'] ) ) { |
||
| 177 | array_push( $argparts, "bgcol={$attributes['bgcol']}" ); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ( isset( $attributes['linkcol'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['linkcol'] ) ) { |
||
| 181 | array_push( $argparts, "linkcol={$attributes['linkcol']}" ); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ( isset( $attributes['package'] ) && preg_match( '|^[0-9]+$|', $attributes['package'] ) ) { |
||
| 185 | array_push( $argparts, "package={$attributes['package']}" ); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ( isset( $attributes['t'] ) && preg_match( '|^[0-9]+$|', $attributes['t'] ) ) { |
||
| 189 | array_push( $argparts, "t={$attributes['t']}" ); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ( 'true' === $attributes['notracklist'] ) { |
||
| 193 | array_push( $argparts, 'notracklist=true' ); |
||
| 194 | } |
||
| 195 | |||
| 196 | // 'tracklist' arg deprecates 'notracklist=true' to be less weird. note, behavior |
||
| 197 | // if both are specified is undefined |
||
| 198 | switch ( $attributes['tracklist'] ) { |
||
| 199 | case 'false': |
||
| 200 | case 'none': |
||
| 201 | array_push( $argparts, 'tracklist=false' ); |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | |||
| 205 | switch ( $attributes['artwork'] ) { |
||
| 206 | case 'false': |
||
| 207 | case 'none': |
||
| 208 | case 'small': |
||
| 209 | array_push( $argparts, 'artwork=' . $attributes['artwork'] ); |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | |||
| 213 | if ( 'true' === $attributes['minimal'] ) { |
||
| 214 | array_push( $argparts, 'minimal=true' ); |
||
| 215 | } |
||
| 216 | |||
| 217 | if ( isset( $attributes['theme'] ) && preg_match( '|^[a-zA-Z_]+$|', $attributes['theme'] ) ) { |
||
| 218 | array_push( $argparts, "theme={$attributes['theme']}" ); |
||
| 219 | } |
||
| 220 | |||
| 221 | // param 'tracks' is signed digest param 'esig'. |
||
| 222 | if ( isset( $attributes['tracks'] ) && preg_match( '|^[0-9\,]+$|', $attributes['tracks'] ) ) { |
||
| 223 | View Code Duplication | if ( isset( $attributes['esig'] ) && preg_match( '|^[0-9A-Fa-f]+$|', $attributes['esig'] ) ) { |
|
| 224 | array_push( $argparts, "tracks={$attributes['tracks']}" ); |
||
| 225 | array_push( $argparts, "esig={$attributes['esig']}" ); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | if ( $is_video ) { |
||
| 230 | $url = '//bandcamp.com/VideoEmbed?' . join( '&', $argparts ); |
||
| 231 | $extra_attrs = " mozallowfullscreen='1' webkitallowfullscreen='1' allowfullscreen='1'"; |
||
| 232 | } else { |
||
| 233 | $url = '//bandcamp.com/EmbeddedPlayer/v=2/' . join( '/', $argparts ) . '/'; |
||
| 234 | $extra_attrs = ''; |
||
| 235 | } |
||
| 236 | |||
| 237 | $iframe = '<iframe width="%s" height="%s" style="position: relative; display: block; width: %s; height: %s;" src="%s" allowtransparency="true" frameborder="0"%s></iframe>'; |
||
| 238 | $iframe = sprintf( $iframe, esc_attr( $width ), esc_attr( $height ), esc_attr( $csswidth ), esc_attr( $cssheight ), esc_url( $url ), $extra_attrs ); |
||
| 239 | |||
| 240 | return $iframe; |
||
| 241 | } |
||
| 242 | |||
| 244 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.