| Conditions | 17 |
| Paths | 305 |
| Total Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 64 | function jetpack_hulu_shortcode( $atts ) { |
||
| 65 | global $content_width; |
||
| 66 | |||
| 67 | // Set a default content width, if it's not specified. |
||
| 68 | $attr = shortcode_atts( |
||
| 69 | array( |
||
| 70 | 'id' => '', |
||
| 71 | 'width' => $content_width ? $content_width : 640, |
||
| 72 | 'start_time' => '', |
||
| 73 | 'end_time' => '', |
||
| 74 | 'thumbnail_frame' => '', |
||
| 75 | ), |
||
| 76 | $atts |
||
| 77 | ); |
||
| 78 | |||
| 79 | $id = jetpack_shortcode_get_hulu_id( $atts ); |
||
| 80 | if ( ! $id ) { |
||
| 81 | return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->'; |
||
| 82 | } |
||
| 83 | |||
| 84 | $start_time = 0; |
||
| 85 | if ( is_numeric( $attr['start_time'] ) ) { |
||
| 86 | $start_time = intval( $attr['start_time'] ); |
||
| 87 | } |
||
| 88 | if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) { |
||
| 89 | $end_time = intval( $attr['end_time'] ); |
||
| 90 | } |
||
| 91 | if ( is_numeric( $attr['thumbnail_frame'] ) ) { |
||
| 92 | $thumbnail_frame = intval( $attr['thumbnail_frame'] ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg |
||
| 96 | // If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id). |
||
| 97 | if ( is_numeric( $id ) ) { |
||
| 98 | $transient_key = "hulu-$id"; |
||
| 99 | $transient_value = get_transient( $transient_key ); |
||
| 100 | |||
| 101 | if ( false === $transient_value ) { |
||
| 102 | // let's make a cross-site http request out to the hulu oembed api. |
||
| 103 | $oembed_url = sprintf( |
||
| 104 | 'https://www.hulu.com/api/oembed.json?url=%s', |
||
| 105 | rawurlencode( 'https://www.hulu.com/watch/' . esc_attr( $id ) ) |
||
| 106 | ); |
||
| 107 | $response = wp_remote_get( $oembed_url ); |
||
| 108 | $response_code = wp_remote_retrieve_response_code( $response ); |
||
| 109 | $response_message = wp_remote_retrieve_response_message( $response ); |
||
| 110 | if ( 200 !== $response_code && ! empty( $response_message ) ) { |
||
| 111 | return "<!-- Hulu Error: Hulu shortcode http error $response_message -->"; |
||
| 112 | } elseif ( 200 !== $response_code ) { |
||
| 113 | return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->"; |
||
| 114 | } else { |
||
| 115 | $response_body = wp_remote_retrieve_body( $response ); |
||
| 116 | $json = json_decode( $response_body ); |
||
| 117 | |||
| 118 | // Pull out id from embed url (from oembed API). |
||
| 119 | $embed_url_params = array(); |
||
| 120 | parse_str( wp_parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params ); |
||
|
|
|||
| 121 | |||
| 122 | if ( isset( $embed_url_params['eid'] ) ) { |
||
| 123 | $id = $embed_url_params['eid']; |
||
| 124 | } |
||
| 125 | // let's cache this response indefinitely. |
||
| 126 | set_transient( $transient_key, $id ); |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | $id = $transient_value; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | if ( ! $id ) { |
||
| 134 | return '<!-- Hulu Error: Not a Hulu video. -->'; |
||
| 135 | } |
||
| 136 | |||
| 137 | $query_args = array(); |
||
| 138 | $query_args['eid'] = esc_attr( $id ); |
||
| 139 | if ( isset( $start_time ) ) { |
||
| 140 | $query_args['st'] = intval( $start_time ); |
||
| 141 | } |
||
| 142 | if ( isset( $end_time ) ) { |
||
| 143 | $query_args['et'] = intval( $end_time ); |
||
| 144 | } |
||
| 145 | if ( isset( $thumbnail_frame ) ) { |
||
| 146 | $query_args['it'] = 'i' . intval( $thumbnail_frame ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $iframe_url = add_query_arg( $query_args, 'https://www.hulu.com/embed.html' ); |
||
| 150 | $width = intval( $attr['width'] ); |
||
| 151 | $height = round( ( $width / 640 ) * 360 ); |
||
| 152 | |||
| 153 | $html = sprintf( |
||
| 154 | '<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen |
||
| 155 | mozallowfullscreen allowfullscreen></iframe></div>', |
||
| 156 | esc_url( $iframe_url ), |
||
| 157 | esc_attr( $width ), |
||
| 158 | esc_attr( $height ) |
||
| 159 | ); |
||
| 160 | $html = apply_filters( 'video_embed_html', $html ); |
||
| 161 | |||
| 162 | return $html; |
||
| 163 | } |
||
| 164 | |||
| 276 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.