Conditions | 38 |
Paths | 211 |
Total Lines | 186 |
Code Lines | 89 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 2 |
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 |
||
269 | public function random_metabox_content( $post_id, $cmb, $connected ){ |
||
270 | $value = ''; |
||
271 | |||
272 | // First check that our post ID & cmb array aren't empty |
||
273 | if ( empty( $cmb ) || empty( $post_id ) ){ |
||
274 | return; |
||
275 | } |
||
276 | |||
277 | // Fetch the appropriate type of data and return |
||
278 | switch( $cmb['type'] ){ |
||
279 | |||
280 | case 'text': |
||
281 | case 'text_small': |
||
282 | case 'text_medium': |
||
283 | |||
284 | // If phone is in the id, fetch a phone # |
||
285 | if ( stripos( $cmb['id'], 'phone' ) ){ |
||
286 | $value = TestContent::phone(); |
||
287 | |||
288 | // If email is in the id, fetch an email address |
||
289 | } elseif ( stripos( $cmb['id'], 'email' ) ){ |
||
290 | $value = TestContent::email(); |
||
291 | |||
292 | // If time is in the id, fetch a time string |
||
293 | } elseif ( stripos( $cmb['id'], 'time' ) ){ |
||
294 | $value = TestContent::time(); |
||
295 | |||
296 | // Otherwise, just a random text string |
||
297 | } else { |
||
298 | $value = TestContent::title( rand( 10, 50 ) ); |
||
299 | } |
||
300 | |||
301 | break; |
||
302 | |||
303 | case 'text_url': |
||
304 | |||
305 | $value = TestContent::link(); |
||
306 | |||
307 | break; |
||
308 | |||
309 | case 'text_email': |
||
310 | |||
311 | $value = TestContent::email(); |
||
312 | |||
313 | break; |
||
314 | |||
315 | case 'text_time': |
||
316 | |||
317 | $value = TestContent::time(); |
||
318 | |||
319 | break; |
||
320 | |||
321 | case 'select_timezone': |
||
322 | |||
323 | $value = TestContent::timezone(); |
||
324 | |||
325 | break; |
||
326 | |||
327 | case 'text_date': |
||
328 | |||
329 | $value = TestContent::date( 'm/d/Y' ); |
||
330 | |||
331 | break; |
||
332 | |||
333 | case 'text_date_timestamp': |
||
334 | case 'text_datetime_timestamp': |
||
335 | |||
336 | $value = TestContent::date( 'U' ); |
||
337 | |||
338 | break; |
||
339 | |||
340 | // case 'text_datetime_timestamp_timezone': break; |
||
341 | |||
342 | case 'text_money': |
||
343 | |||
344 | $value = rand( 0, 100000 ); |
||
345 | |||
346 | break; |
||
347 | |||
348 | case 'test_colorpicker': |
||
349 | |||
350 | $value = '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
||
351 | |||
352 | break; |
||
353 | |||
354 | case 'textarea': |
||
355 | case 'textarea_small': |
||
356 | case 'textarea_code': |
||
357 | |||
358 | $value = TestContent::plain_text(); |
||
359 | |||
360 | break; |
||
361 | |||
362 | case 'select': |
||
363 | case 'radio_inline': |
||
364 | case 'radio': |
||
365 | |||
366 | // Grab a random item out of the array and return the key |
||
367 | $new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
||
368 | $value = key( $new_val ); |
||
369 | |||
370 | break; |
||
371 | |||
372 | // case 'taxonomy_radio': break; |
||
373 | // case 'taxonomy_select': break; |
||
374 | // case 'taxonomy_multicheck': break; |
||
375 | |||
376 | case 'checkbox': |
||
377 | |||
378 | // 50/50 odds of being turned on |
||
379 | if ( rand( 0, 1 ) == 1 ){ |
||
380 | $value = 'on'; |
||
381 | } |
||
382 | |||
383 | break; |
||
384 | |||
385 | case 'multicheck': |
||
386 | |||
387 | $new_option = array(); |
||
388 | |||
389 | // Loop through each of our options |
||
390 | foreach ( $cmb['options'] as $key => $value ){ |
||
391 | |||
392 | // 50/50 chance of being included |
||
393 | if ( rand( 0, 1 ) ){ |
||
394 | $new_option[] = $key; |
||
395 | } |
||
396 | |||
397 | } |
||
398 | |||
399 | $value = $new_option; |
||
400 | |||
401 | break; |
||
402 | |||
403 | case 'wysiwyg': |
||
404 | |||
405 | $value = TestContent::paragraphs(); |
||
406 | |||
407 | break; |
||
408 | |||
409 | case 'file': |
||
410 | |||
411 | if ( true == $connected ){ |
||
412 | $value = TestContent::image( $post_id ); |
||
413 | } |
||
414 | |||
415 | break; |
||
416 | |||
417 | // case 'file_list': break; |
||
418 | |||
419 | case 'oembed': |
||
420 | |||
421 | $value = TestContent::oembed(); |
||
422 | |||
423 | break; |
||
424 | |||
425 | } |
||
426 | |||
427 | // Value must exist to attempt to insert |
||
428 | if ( !empty( $value ) && !is_wp_error( $value ) ){ |
||
429 | |||
430 | $type = $cmb['type']; |
||
431 | $id = $cmb['id']; |
||
432 | $value = apply_filters( "tc_{$type}_metabox", $value ); // Filter by metabox type |
||
433 | $value = apply_filters( "tc_{$id}_metabox", $value ); // Filter by metabox ID |
||
434 | |||
435 | // Files must be treated separately - they use the attachment ID |
||
436 | // & url of media for separate cmb values |
||
437 | if ( $cmb['type'] != 'file' ){ |
||
438 | add_post_meta( $post_id, $cmb['id'], $value, true ); |
||
439 | } else { |
||
440 | add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
||
441 | add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
||
442 | } |
||
443 | |||
444 | if ( $cmb['source'] === 'acf' ){ |
||
445 | add_post_meta( $post_id, '_' . $cmb['id'], $cmb['key'], true ); |
||
446 | } |
||
447 | |||
448 | // If we're dealing with a WP Error object, just return the message for debugging |
||
449 | } elseif ( is_wp_error( $value ) ){ |
||
450 | error_log( $value->get_error_message() ); |
||
451 | return $value->get_error_message(); |
||
452 | } |
||
453 | |||
454 | } // end random_metabox_content |
||
455 | |||
457 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: