Conditions | 18 |
Paths | 561 |
Total Lines | 99 |
Code Lines | 63 |
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 |
||
62 | function jetpack_hulu_shortcode( $atts ) { |
||
63 | global $content_width; |
||
64 | |||
65 | // Set a default content width, if it's not specified. |
||
66 | $attr = shortcode_atts( |
||
67 | array( |
||
68 | 'id' => '', |
||
69 | 'width' => $content_width ? $content_width : 640, |
||
70 | 'start_time' => '', |
||
71 | 'end_time' => '', |
||
72 | 'thumbnail_frame' => '' |
||
73 | ), $atts |
||
74 | ); |
||
75 | |||
76 | $id = jetpack_shortcode_get_hulu_id( $atts ); |
||
77 | if ( ! $id ) { |
||
78 | return '<!-- Hulu Error: Hulu shortcode syntax invalid. -->'; |
||
79 | } |
||
80 | |||
81 | $start_time = 0; |
||
82 | if ( is_numeric( $attr['start_time'] ) ) { |
||
83 | $start_time = intval( $attr['start_time'] ); |
||
84 | } |
||
85 | if ( is_numeric( $attr['end_time'] ) && intval( $attr['end_time'] ) > $start_time ) { |
||
86 | $end_time = intval( $attr['end_time'] ); |
||
87 | } |
||
88 | if ( is_numeric( $attr['thumbnail_frame'] ) ) { |
||
89 | $thumbnail_frame = intval( $attr['thumbnail_frame'] ); |
||
90 | } |
||
91 | |||
92 | // check to see if $id is 76560 else we assume it's gQ6Z0I990IWv_VFQI2J7Eg |
||
93 | // If id is numeric, we'll send it off to the hulu oembed api to get the embed URL (and non-numeric id) |
||
94 | if ( is_numeric( $id ) ) { |
||
95 | $transient_key = "hulu-$id"; |
||
96 | if ( false === ( $transient_value = get_transient( $transient_key ) ) ) { |
||
97 | // let's make a cross-site http request out to the hulu oembed api |
||
98 | $response = wp_remote_get( 'http://www.hulu.com/api/oembed.json?url=' . urlencode( 'http://www.hulu.com/watch/' . esc_attr( $id ) ) ); |
||
99 | $response_code = wp_remote_retrieve_response_code( $response ); |
||
100 | $response_message = wp_remote_retrieve_response_message( $response ); |
||
101 | if ( 200 !== $response_code && ! empty( $response_message ) ) { |
||
102 | return "<!-- Hulu Error: Hulu shortcode http error $response_message -->"; |
||
103 | } elseif ( 200 !== $response_code ) { |
||
104 | return "<!-- Hulu Error: Hulu shortcode unknown error occurred, $response_code -->"; |
||
105 | } else { |
||
106 | $response_body = wp_remote_retrieve_body( $response ); |
||
107 | $json = json_decode( $response_body ); |
||
108 | |||
109 | // Pull out id from embed url (from oembed API) |
||
110 | $embed_url_params = array(); |
||
111 | parse_str( parse_url( $json->embed_url, PHP_URL_QUERY ), $embed_url_params ); |
||
112 | |||
113 | if ( isset( $embed_url_params['eid'] ) ) { |
||
114 | $id = $embed_url_params['eid']; |
||
115 | } |
||
116 | // let's cache this response indefinitely. |
||
117 | set_transient( $transient_key, $id ); |
||
118 | } |
||
119 | } else { |
||
120 | $id = $transient_value; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if ( ! $id ) { |
||
125 | return '<!-- Hulu Error: Not a Hulu video. -->'; |
||
126 | } |
||
127 | |||
128 | $width = intval( $attr['width'] ); |
||
129 | $height = round( ( $width / 640 ) * 360 ); |
||
130 | |||
131 | $iframe_url = 'http://www.hulu.com/embed.html'; |
||
132 | if ( is_ssl() ) { |
||
133 | $iframe_url = 'https://secure.hulu.com/embed.html'; |
||
134 | } |
||
135 | |||
136 | $query_args = array(); |
||
137 | $query_args['eid'] = esc_attr( $id ); |
||
138 | if ( isset( $start_time ) ) { |
||
139 | $query_args['st'] = intval( $start_time ); |
||
140 | } |
||
141 | if ( isset( $end_time ) ) { |
||
142 | $query_args['et'] = intval( $end_time ); |
||
143 | } |
||
144 | if ( isset( $thumbnail_frame ) ) { |
||
145 | $query_args['it'] = 'i' . intval( $thumbnail_frame ); |
||
146 | } |
||
147 | |||
148 | $iframe_url = add_query_arg( $query_args, $iframe_url ); |
||
149 | |||
150 | $html = sprintf( |
||
151 | '<div class="embed-hulu" style="text-align: center;"><iframe src="%s" width="%s" height="%s" style="border:0;" scrolling="no" webkitAllowFullScreen |
||
152 | mozallowfullscreen allowfullscreen></iframe></div>', |
||
153 | esc_url( $iframe_url ), |
||
154 | esc_attr( $width ), |
||
155 | esc_attr( $height ) |
||
156 | ); |
||
157 | $html = apply_filters( 'video_embed_html', $html ); |
||
158 | |||
159 | return $html; |
||
160 | } |
||
161 | |||
273 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.