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