What is a more elegant solution to these nested if/elseif statements?

后端 未结 9 1833
野趣味
野趣味 2021-01-12 17:09

I\'m building a website that contains users with user profiles. Many of the fields in the profile are optional.

There is an opportunity for a lot of user-generate

相关标签:
9条回答
  • 2021-01-12 17:47

    It is not much, but because $name it is at least NULL:

    public function nameify($names = NULL) {
        $name = 'NULL';
        if (!empty($names)) {
            if (!empty($names['display_name'])) {
                $name = $names['display_name'];
            } elseif (!empty($names['first_name'])) {
                $name = $names['first_name'];
                if (!empty($names['last_name'])) {
                    $name .= ' ' . $names['last_name'];
                }
            } elseif (!empty($names['last_name'])) {
                $name = $names['last_name'];
            }
    
            if ($name=='NULL' && !empty($names['id'])) {
                $name = 'user' . $names['id'];
            } 
        } 
        return $name;
    }
    
    0 讨论(0)
  • 2021-01-12 17:47

    Somewhat less readable, but effective):

    list($idx,$name) = array_shift(array_filter(array(
        $names['display_name'],
        implode(' ',array_filter(array($names['first_name'],$names['last_name']))),
        'user'.$names['id'];
        )));
    
    0 讨论(0)
  • 2021-01-12 17:49

    A State machine works very nicely for involved logic like that. It's very simple to implement as well (using a switch statement).

    0 讨论(0)
  • 2021-01-12 17:52

    I would propose this:

    public function nameify($names = null) {
        if(empty($names))
            return null;
    
        if(!empty($names['display_name']))
            return $names['display_name'];
    
        if(!empty($names['first_name'])) {
            $name = $names['first_name'];
            if (!empty($names['last_name'])) {
                $name .= ' ' . $names['last_name'];
            }
            return $name;
        }
    
        if(!empty($names['id]))
            return 'user' . $names['id'];
    
        return null;
    }
    
    0 讨论(0)
  • 2021-01-12 17:56

    I'm not sure that my version would be simplier, but here it is:

    public function nameify($names = null) {
        $result = array();
    
        if( !empty($names['display_name']) ) {
            array_push($result,$names['display_name']);
        } else {
            if( !empty($names['first_name']) ) {
                array_push($result, $names['first_name']);
            }
            if( !empty($names['last_name']) ) {
                array_push($result, $names['last_name']);
            }
        }
    
        if( empty($result) && !empty($names['id']) ) {
            array_push($result, 'user'.$names['id']);
        }
    
        return (empty($result) ? 'NULL' : implode(' ', $result));
    }
    
    0 讨论(0)
  • 2021-01-12 18:03

    I'd go with:

    if( empty($names['display_name']) ) {
        $name = $names['first_name'] . ' ' $names['last_name'];
    else
        $name = $names['display_name'];
    
    $name = trim($name);
    if( empty($name) ) 
        $name = 'user'.$names['id'];
    if( empty($name) ) 
        $name = 'NULL';
    

    This would be the 'core logic'... there will need to be other checks like $names != NULL or something..

    0 讨论(0)
提交回复
热议问题