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

I am creating a website containing users with user profiles. Many fields in the profile are optional.

There is an opportunity for a large number of user content, so I need to display the author of this content in many different places on the site (comments, posts, etc.). In the user profile, he can (optionally) fill in his "first name", his "last name" and "display name".

To display the author, I wrote a helper method that looks at the provided array of these fields and returns the most suitable name for the user in this order of preference:

  • If the user populated display_name , this will be displayed.
  • If the user filled in first_name and last_name but not display_name , it will display both names
  • If the user has filled in only first_name , first_name will be displayed.
  • If the user fills only last_name , it displays last_name .
  • If all else fails, the user ID will be displayed, i.e. user123
  • If none of the array keys is present or the parameter is NULL, the name will be displayed as NULL

The method works fine, but it's ugly. There should be a way to decorate this with an alternative to nested if / else statements.

 public function nameify($names = NULL) { $name = ''; 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 (empty($name) && !empty($names['id'])) { $name = 'user' . $names['id']; } else { $name = 'NULL'; } } else { $name = 'NULL'; } return $name; } 
+6
string php user-input helper
source share
9 answers
 public function nameify($names = NULL) { if ($names) { 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']; } if (empty($name) && !empty($names['id'])) { $name = 'user' . $names['id']; } } return $name ? ltrim($name) : 'NULL'; } 

First set the default value and return it if nothing matches. Then, since we always want to return the display name, if we do just that.

EDIT: set to prevent returning "NULL"

+6
source share

Using triple conditions, we can shorten and decorate the code:

 public function nameify($names = NULL) { $name = 'NULL'; if (!empty($names)) { $name = ($names['display_name']) ? $names['display_name'] : trim($names['first_name']." ".$names['last_name']); if(!$name) $name = ($names['id'] > 0) ? 'user'.$names['id'] : 'NULL'; } return $name; } 
+2
source share

I would suggest the following:

  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;
 }
+1
source share

This is not much, but since $ name 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
source share

I would 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 will be the "main logic" ... there should be other checks, such as $names != NULL or something like that.

0
source share
 //pointers to functions $arrayOfSulutions{"display_name_strategy", "full_name_strategy" ..., "null_strategy" } function display_name_strategy{ return $names['display_name']; } $i = 0; while($res == null){ $res = call($arrayOfSulutions[$i++]); } 
0
source share

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
source share

A machine condition works very well for the logic involved. It is very simple to implement (using the switch statement).

0
source share

I'm not sure my version will be simpler, 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
source share

All Articles