Switch case fallthrough

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).

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:
There is no "default" case.

I guess my knowledge is too rough on using switch. Isn't a default not necessary when the options are all provided in the switch?

Is it worth putting in the default: // do nothing ?

As an update, the change in this block of code didn't fix the error. It did lead me to question the WordPress hook in the class.

Thank you for your time.
 
I guess my knowledge is too rough on using switch. Isn't a default not necessary when the options are all provided in the switch?
phpstorm doesn't know that every case has been described. If it was a php 8.1 enum sure, it could do that.
 
Also I'm not sure your case blocks are defined correctly...

The switch() is for what you're comparing, the cases for what you're comparing against, so more like this:

Code:
            $author_info = '';

            switch ( $author_option ) {

                case 1 :
                    $author_id = $post->post_author;

                    return XF::app()->finder( 'XF:User' )->where( 'user_id', $author_id )->fetchOne();

(rest of cases snipped for brevity)
 
The switch() is for what you're comparing, the cases for what you're comparing against, so more like this:

OMG. I've looked at this code for months. Thank you for pointing out a very huge error. I can now see that it was originally an if-elseif and I didn't properly change it to a switch.
 
Last edited:
Back
Top Bottom