Conditions | 7 |
Paths | 6 |
Total Lines | 94 |
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 |
||
118 | private function create_attachment( &$video ) { |
||
119 | //allow for really big imports that take a really long time! |
||
120 | @set_time_limit( 300 ); |
||
121 | |||
122 | $video["thumbnail"] = $this->get_thumbnail_url( $video ); |
||
123 | |||
124 | $thumbnail = file_get_contents( $video["thumbnail"] ); |
||
125 | $filetype = wp_check_filetype( $video["thumbnail"], null ); |
||
126 | |||
127 | $thumbnail_filename = $video["id"] . '.' . $filetype['ext']; |
||
128 | |||
129 | // $response = wp_remote_get( $video["thumbnail"] ); |
||
130 | // |
||
131 | // if ( is_wp_error( $response ) ) { |
||
132 | // return array( |
||
133 | // "type" => "error", |
||
134 | // "message" => $response->get_error_message() |
||
135 | // ); |
||
136 | // } |
||
137 | // |
||
138 | // $response_code = wp_remote_retrieve_response_code( $response ); |
||
139 | // if ( 200 !== $response_code ) { |
||
140 | // return array( |
||
141 | // "type" => "error", |
||
142 | // "message" => "Unable to retrieve thumbnail due to error " . $response_code |
||
143 | // ); |
||
144 | // } |
||
145 | // |
||
146 | // $thumbnail = wp_remote_retrieve_body( $response ); |
||
147 | // if ( empty($thumbnail) ) { |
||
148 | // return array( |
||
149 | // "type" => "error", |
||
150 | // "message" => "Unable to retrieve response body for thumbnail." |
||
151 | // ); |
||
152 | // } |
||
153 | // |
||
154 | // $thumbnail_filename = $this->get_thumbnail_filename( $video, $response ); |
||
155 | // if ($thumbnail_filename === false){ |
||
156 | // return array( |
||
157 | // "type" => "error", |
||
158 | // "message" => "Unable to generate thumbnail filename from response." |
||
159 | // ); |
||
160 | // } |
||
161 | |||
162 | $upload = wp_upload_bits( $thumbnail_filename, null, $thumbnail ); |
||
163 | if ($upload["error"] !== false){ |
||
164 | return array( |
||
165 | "type" => "error", |
||
166 | "message" => $upload["error"] === true ? "Unknown error uploading thumbnail image." : $upload["error"] |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | $guid = $upload["url"]; |
||
171 | $file = $upload["file"]; |
||
172 | |||
173 | // Create attachment |
||
174 | $attachment = array( |
||
175 | 'ID' => 0, |
||
176 | 'guid' => $guid, |
||
177 | 'post_title' => $video["title"], |
||
178 | 'post_excerpt' => $video["title"], |
||
179 | 'post_content' => $video["description"], |
||
180 | 'post_date' => '', |
||
181 | 'post_mime_type' => 'image/foogallery' |
||
182 | ); |
||
183 | |||
184 | // Include image.php so we can call wp_generate_attachment_metadata() |
||
185 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
||
186 | |||
187 | // Insert the attachment |
||
188 | $attachment_id = wp_insert_attachment( $attachment, $file, 0 ); |
||
189 | if ($attachment_id == 0 || is_wp_error($attachment_id)){ |
||
190 | return array( |
||
191 | "type" => "error", |
||
192 | "message" => is_wp_error($attachment_id) ? $attachment_id->get_error_message() : "Failed to insert the attachment." |
||
193 | ); |
||
194 | } |
||
195 | $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); |
||
196 | wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
||
197 | |||
198 | $thumbnail_details = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); // Yes we have the data, but get the thumbnail URL anyway, to be safe |
||
199 | if ($thumbnail_details === false){ |
||
200 | return array( |
||
201 | "type" => "error", |
||
202 | "message" => "Unable to retrieve thumbnail details." |
||
203 | ); |
||
204 | } |
||
205 | $video["thumbnail"] = $thumbnail_details[0]; |
||
206 | |||
207 | return array( |
||
208 | "type" => "success", |
||
209 | "attachment_id" => $attachment_id |
||
210 | ); |
||
211 | } |
||
212 | |||
277 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.