Conditions | 36 |
Paths | 137 |
Total Lines | 175 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
172 | public function random_metabox_content( $post_id, $cmb ){ |
||
173 | $value = ''; |
||
174 | |||
175 | // First check that our post ID & cmb array aren't empty |
||
176 | if ( empty( $cmb ) || empty( $post_id ) ){ |
||
177 | return; |
||
178 | } |
||
179 | |||
180 | // Fetch the appropriate type of data and return |
||
181 | switch( $cmb['type'] ){ |
||
182 | |||
183 | case 'text': |
||
184 | case 'text_small': |
||
185 | case 'text_medium': |
||
186 | |||
187 | // If phone is in the id, fetch a phone # |
||
188 | if ( stripos( $cmb['id'], 'phone' ) ){ |
||
189 | $value = TestContent::phone(); |
||
190 | |||
191 | // If email is in the id, fetch an email address |
||
192 | } elseif ( stripos( $cmb['id'], 'email' ) ){ |
||
193 | $value = TestContent::email(); |
||
194 | |||
195 | // If time is in the id, fetch a time string |
||
196 | } elseif ( stripos( $cmb['id'], 'time' ) ){ |
||
197 | $value = TestContent::time(); |
||
198 | |||
199 | // Otherwise, just a random text string |
||
200 | } else { |
||
201 | $value = TestContent::title( rand( 10, 50 ) ); |
||
202 | } |
||
203 | |||
204 | break; |
||
205 | |||
206 | case 'text_url': |
||
207 | |||
208 | $value = TestContent::link(); |
||
209 | |||
210 | break; |
||
211 | |||
212 | case 'text_email': |
||
213 | |||
214 | $value = TestContent::email(); |
||
215 | |||
216 | break; |
||
217 | |||
218 | case 'text_time': |
||
219 | |||
220 | $value = TestContent::time(); |
||
221 | |||
222 | break; |
||
223 | |||
224 | case 'select_timezone': |
||
225 | |||
226 | $value = TestContent::timezone(); |
||
227 | |||
228 | break; |
||
229 | |||
230 | case 'text_date': |
||
231 | |||
232 | $value = TestContent::date( 'm/d/Y' ); |
||
233 | |||
234 | break; |
||
235 | |||
236 | case 'text_date_timestamp': |
||
237 | case 'text_datetime_timestamp': |
||
238 | |||
239 | $value = TestContent::date( 'U' ); |
||
240 | |||
241 | break; |
||
242 | |||
243 | // case 'text_datetime_timestamp_timezone': break; |
||
|
|||
244 | |||
245 | case 'text_money': |
||
246 | |||
247 | $value = rand( 0, 100000 ); |
||
248 | |||
249 | break; |
||
250 | |||
251 | case 'test_colorpicker': |
||
252 | |||
253 | $value = '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
||
254 | |||
255 | break; |
||
256 | |||
257 | case 'textarea': |
||
258 | case 'textarea_small': |
||
259 | case 'textarea_code': |
||
260 | |||
261 | $value = TestContent::plain_text(); |
||
262 | |||
263 | break; |
||
264 | |||
265 | case 'select': |
||
266 | case 'radio_inline': |
||
267 | case 'radio': |
||
268 | |||
269 | // Grab a random item out of the array and return the key |
||
270 | $new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
||
271 | $value = key( $new_val ); |
||
272 | |||
273 | break; |
||
274 | |||
275 | // case 'taxonomy_radio': break; |
||
276 | // case 'taxonomy_select': break; |
||
277 | // case 'taxonomy_multicheck': break; |
||
278 | |||
279 | case 'checkbox': |
||
280 | |||
281 | // 50/50 odds of being turned on |
||
282 | if ( rand( 0, 1 ) == 1 ){ |
||
283 | $value = 'on'; |
||
284 | } |
||
285 | |||
286 | break; |
||
287 | |||
288 | case 'multicheck': |
||
289 | |||
290 | $new_option = array(); |
||
291 | |||
292 | // Loop through each of our options |
||
293 | foreach ( $cmb['options'] as $key => $value ){ |
||
294 | |||
295 | // 50/50 chance of being included |
||
296 | if ( rand( 0, 1 ) ){ |
||
297 | $new_option[] = $key; |
||
298 | } |
||
299 | |||
300 | } |
||
301 | |||
302 | $value = $new_option; |
||
303 | |||
304 | break; |
||
305 | |||
306 | case 'wysiwyg': |
||
307 | |||
308 | $value = TestContent::paragraphs(); |
||
309 | |||
310 | break; |
||
311 | |||
312 | case 'file': |
||
313 | |||
314 | $value = TestContent::image( $post_id ); |
||
315 | |||
316 | break; |
||
317 | |||
318 | // case 'file_list': break; |
||
319 | |||
320 | case 'oembed': |
||
321 | |||
322 | $value = TestContent::oembed(); |
||
323 | |||
324 | break; |
||
325 | |||
326 | } |
||
327 | |||
328 | // Value must exist to attempt to insert |
||
329 | if ( !empty( $value ) && !is_wp_error( $value ) ){ |
||
330 | |||
331 | // Files must be treated separately - they use the attachment ID |
||
332 | // & url of media for separate cmb values |
||
333 | if ( $cmb['type'] != 'file' ){ |
||
334 | add_post_meta( $post_id, $cmb['id'], $value, true ); |
||
335 | } else { |
||
336 | add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
||
337 | add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
||
338 | } |
||
339 | |||
340 | // If we're dealing with a WP Error object, just return the message for debugging |
||
341 | } elseif ( is_wp_error( $value ) ){ |
||
342 | error_log( $value->get_error_message() ); |
||
343 | return $value->get_error_message(); |
||
344 | } |
||
345 | |||
346 | } // end random_metabox_content |
||
347 | |||
349 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.