Conditions | 41 |
Paths | > 20000 |
Total Lines | 152 |
Lines | 15 |
Ratio | 9.87 % |
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 |
||
92 | function dailymotion_shortcode( $atts ) { |
||
93 | global $content_width; |
||
94 | |||
95 | if ( isset( $atts[0] ) ) { |
||
96 | $id = ltrim( $atts[0], '=' ); |
||
97 | $atts['id'] = $id; |
||
98 | |||
99 | } else { |
||
100 | $params = shortcode_new_to_old_params( $atts ); |
||
101 | parse_str( $params, $atts_new ); |
||
102 | |||
103 | foreach ( $atts_new as $k => $v ) { |
||
|
|||
104 | $atts[ $k ] = $v; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | $atts = shortcode_atts( |
||
109 | array( |
||
110 | 'id' => '', // string. |
||
111 | 'width' => '', // int. |
||
112 | 'height' => '', // int. |
||
113 | 'title' => '', // string. |
||
114 | 'user' => '', // string. |
||
115 | 'video' => '', // string. |
||
116 | 'autoplay' => 0, // int. |
||
117 | 'endscreen-enable' => 1, // int. |
||
118 | 'mute' => 0, // int. |
||
119 | 'sharing-enable' => 1, // int. |
||
120 | 'start' => '', // int. |
||
121 | 'subtitles-default' => '', // string. |
||
122 | 'ui-highlight' => '', // string. |
||
123 | 'ui-logo' => 1, // int. |
||
124 | 'ui-start-screen-info' => 0, // int. |
||
125 | 'ui-theme' => '', // string. |
||
126 | ), |
||
127 | $atts, |
||
128 | 'dailymotion' |
||
129 | ); |
||
130 | |||
131 | if ( isset( $atts['id'] ) && ! empty( $atts['id'] ) ) { |
||
132 | $id = rawurlencode( $atts['id'] ); |
||
133 | } else { |
||
134 | return '<!--Dailymotion error: bad or missing ID-->'; |
||
135 | } |
||
136 | |||
137 | /*set width and height using provided parameters if any */ |
||
138 | $width = isset( $atts['width'] ) ? (int) $atts['width'] : 0; |
||
139 | $height = isset( $atts['height'] ) ? (int) $atts['height'] : 0; |
||
140 | |||
141 | if ( ! $width && ! $height ) { |
||
142 | if ( ! empty( $content_width ) ) { |
||
143 | $width = absint( $content_width ); |
||
144 | } else { |
||
145 | $width = 425; |
||
146 | } |
||
147 | $height = $width / 425 * 334; |
||
148 | } elseif ( ! $height ) { |
||
149 | $height = $width / 425 * 334; |
||
150 | } elseif ( ! $width ) { |
||
151 | $width = $height / 334 * 425; |
||
152 | } |
||
153 | |||
154 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
||
155 | return sprintf( |
||
156 | '<amp-dailymotion data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-dailymotion>', |
||
157 | esc_attr( $id ), |
||
158 | absint( $width ), |
||
159 | absint( $height ) |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Let's add parameters if needed. |
||
165 | * |
||
166 | * @see https://developer.dailymotion.com/player |
||
167 | */ |
||
168 | $player_params = array(); |
||
169 | |||
170 | View Code Duplication | if ( isset( $atts['autoplay'] ) && '1' === $atts['autoplay'] ) { |
|
171 | $player_params['autoplay'] = '1'; |
||
172 | } |
||
173 | if ( isset( $atts['endscreen-enable'] ) && '0' === $atts['endscreen-enable'] ) { |
||
174 | $player_params['endscreen-enable'] = '0'; |
||
175 | } |
||
176 | View Code Duplication | if ( isset( $atts['mute'] ) && '1' === $atts['mute'] ) { |
|
177 | $player_params['mute'] = '1'; |
||
178 | } |
||
179 | if ( isset( $atts['sharing-enable'] ) && '0' === $atts['sharing-enable'] ) { |
||
180 | $player_params['sharing-enable'] = '0'; |
||
181 | } |
||
182 | if ( isset( $atts['start'] ) && ! empty( $atts['start'] ) ) { |
||
183 | $player_params['start'] = abs( (int) $atts['start'] ); |
||
184 | } |
||
185 | View Code Duplication | if ( isset( $atts['subtitles-default'] ) && ! empty( $atts['subtitles-default'] ) ) { |
|
186 | $player_params['subtitles-default'] = esc_attr( $atts['subtitles-default'] ); |
||
187 | } |
||
188 | View Code Duplication | if ( isset( $atts['ui-highlight'] ) && ! empty( $atts['ui-highlight'] ) ) { |
|
189 | $player_params['ui-highlight'] = esc_attr( $atts['ui-highlight'] ); |
||
190 | } |
||
191 | if ( isset( $atts['ui-logo'] ) && '0' === $atts['ui-logo'] ) { |
||
192 | $player_params['ui-logo'] = '0'; |
||
193 | } |
||
194 | if ( isset( $atts['ui-start-screen-info'] ) && '0' === $atts['ui-start-screen-info'] ) { |
||
195 | $player_params['ui-start-screen-info'] = '0'; |
||
196 | } |
||
197 | View Code Duplication | if ( isset( $atts['ui-theme'] ) && in_array( strtolower( $atts['ui-theme'] ), array( 'dark', 'light' ), true ) ) { |
|
198 | $player_params['ui-theme'] = esc_attr( $atts['ui-theme'] ); |
||
199 | } |
||
200 | |||
201 | // Add those parameters to the Video URL. |
||
202 | $video_url = add_query_arg( |
||
203 | $player_params, |
||
204 | 'https://www.dailymotion.com/embed/video/' . $id |
||
205 | ); |
||
206 | |||
207 | $output = ''; |
||
208 | |||
209 | if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) { |
||
210 | $output .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $video_url ) . '" style="border:0;" allowfullscreen></iframe>'; |
||
211 | |||
212 | $video = preg_replace( '/[^-a-z0-9_]/i', '', $atts['video'] ); |
||
213 | $title = wp_kses( $atts['title'], array() ); |
||
214 | if ( |
||
215 | array_key_exists( 'video', $atts ) |
||
216 | && $video |
||
217 | && array_key_exists( 'title', $atts ) |
||
218 | && $title |
||
219 | ) { |
||
220 | $output .= '<br /><strong><a href="' . esc_url( 'https://www.dailymotion.com/video/' . $video ) . '" target="_blank">' . esc_html( $title ) . '</a></strong>'; |
||
221 | } |
||
222 | |||
223 | $user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] ); |
||
224 | if ( array_key_exists( 'user', $atts ) && $user ) { |
||
225 | /* translators: %s is a Dailymotion user name */ |
||
226 | $output .= '<br /><em>' . wp_kses( |
||
227 | sprintf( |
||
228 | /* Translators: placeholder is a Dailymotion username, linking to a Dailymotion profile page. */ |
||
229 | __( 'Uploaded by %s', 'jetpack' ), |
||
230 | '<a href="' . esc_url( 'https://www.dailymotion.com/' . $user ) . '" target="_blank">' . esc_html( $user ) . '</a>' |
||
231 | ), |
||
232 | array( |
||
233 | 'a' => array( |
||
234 | 'href' => true, |
||
235 | 'target' => true, |
||
236 | ), |
||
237 | ) |
||
238 | ) . '</em>'; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return $output; |
||
243 | } |
||
244 | add_shortcode( 'dailymotion', 'dailymotion_shortcode' ); |
||
367 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.