LPH
Well-known member
The following code does not show an error in PhpStorm IDE but this leads to a switch fallthrough (which I didn't intentionally do in the block of code).
I'm trying to understand if an error I see with multiple authors is due to leaving off the break after the return. I know I could just put in the break, override the code, then test, but wasn't sure on best practices and wasn't sure if my explanation is even correct.
Should break be added after each return statement in the case block or should I leave the code and try to find the author error elsewhere? I've been looking for a few years and am down to this little patch of code.
Maybe the block of code should be rewritten into an if-elseif -else ? Or should I define a new variable and use something like this?
Thank you for your time.
PHP:
$author_option = $this->options['select_author'];
switch ( $author_option ) {
case ( $author_option === '1' ) :
$author_id = $post->post_author;
return XF::app()->finder( 'XF:User' )->where( 'user_id', $author_id )->fetchOne();
case ( $author_option === '2' ) :
$author_id = $post->post_author;
return get_the_author_meta( 'nickname', $author_id );
case ( $author_option === '3' ) :
$author_id = $post->post_author;
return get_the_author_meta( 'first_name', $author_id );
case ( $author_option === '4' ) :
$author_id = $post->post_author;
$author_first = get_the_author_meta( 'first_name', $author_id );
$author_last = get_the_author_meta( 'last_name', $author_id );
return $author_first . ' ' . $author_last;
case ( $author_option === '5' ) :
$author_id = $post->post_author;
return get_the_author_meta( 'user_nicename', $author_id );
}
return '';
I'm trying to understand if an error I see with multiple authors is due to leaving off the break after the return. I know I could just put in the break, override the code, then test, but wasn't sure on best practices and wasn't sure if my explanation is even correct.
Should break be added after each return statement in the case block or should I leave the code and try to find the author error elsewhere? I've been looking for a few years and am down to this little patch of code.
Maybe the block of code should be rewritten into an if-elseif -else ? Or should I define a new variable and use something like this?
PHP:
$author_info = '';
switch ( $author_option ) {
case ( $author_option === '1' ) :
$author_id = $post->post_author;
$author_info = XF::app()->finder( 'XF:User' )->where( 'user_id', $author_id )->fetchOne();
break;
case ( $author_option === '2' ) :
$author_id = $post->post_author;
$author_info = get_the_author_meta( 'nickname', $author_id );
break;
case ( $author_option === '3' ) :
$author_id = $post->post_author;
$author_info = get_the_author_meta( 'first_name', $author_id );
break;
case ( $author_option === '4' ) :
$author_id = $post->post_author;
$author_first = get_the_author_meta( 'first_name', $author_id );
$author_last = get_the_author_meta( 'last_name', $author_id );
$author_info = $author_first . ' ' . $author_last;
break;
case ( $author_option === '5' ) :
$author_id = $post->post_author;
$author_info = get_the_author_meta( 'user_nicename', $author_id );
break;
}
return $author_info;
Thank you for your time.
Last edited: