-2
\$\begingroup\$

I have a very long switch statement with more than 18 cases. I want to make it more comfortable and easier for the maintenance, but I have no idea how ...

switch($key)
    {
        case 'male':
            return $filter = array($gender , '=', 'male');
            break;

        case 'female':
            return $filter = array($gender , '=', 'female');
            break;

        case '10':
            return $filter = array($lookingFor, $like, '%Mann%');
            break;

        case '11':
            return $filter = array($lookingFor, $like, '%Frau%');
            break;

        case '12':
            return $filter = array($lookingFor, $like, '%Paar%');
            break;

        case '13':
            return $filter = array($lookingFor, $like, '%Herren/-in%');
            break;

        case '14':
            return $filter = array($lookingFor, $like, '%Sklave/-in%');
            break;

        case '15':
            return $filter = array($lookingFor, $like, '%Domina%');
            break;

        case '20':
            return $filter = array($lookFor , $like, '%Affäre%');
            break;

        case '21':
            return $filter = array($lookFor , $like, '%Cybersex%');
            break;

        case '22':
            return $filter = array($lookFor , $like, '%Ehe%');
            break;

        case '23':
            return $filter = array($lookFor , $like, '%Flirt%');
            break;

        case '24':
            return $filter = array($lookFor , $like, '%Freundschaft%');
            break;

        case '25':
            return $filter = array($lookFor , $like, '%für alles offen%');
            break;

        case '26':
            return $filter = array($lookFor , $like, '%Partnerschaft%');
            break;

        case '27':
            return $filter = array($lookFor , $like, '%Sex%');
            break;

        case 'default':
        default:
            return $filter = array('is_old', '=', '0');
          break;
    }

I'm very grateful for every hint or idea.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You're using one key for all these different cases? You should edit the post to explain a bit about how you're using this and what it does, it's quite possible you'd be better off splitting this into different switches. \$\endgroup\$ Commented Sep 17, 2015 at 13:01
  • 1
    \$\begingroup\$ The desire to improve code is implied for all questions on this site. Question titles should reflect the purpose of the code, not how you wish to have it reworked. See How to Ask. \$\endgroup\$
    – Jamal
    Commented Sep 17, 2015 at 16:09
  • \$\begingroup\$ What do you think to replace switch($key) { case 'male': return something; break; by if / elseif / else? no break and more readable. \$\endgroup\$
    – ChoiZ
    Commented Sep 17, 2015 at 17:55

1 Answer 1

2
\$\begingroup\$

Well, to break you out of the switch statemens I suggest you use an array. You might want to make this database stored so you can fetch the possible iterations etc.. .as a list there, or json encode it and retrieve the translations that way. That's up to you.

By using an array method you can be very flexible in extending or limiting the the possible translations for the search query. With a switch you have to hardcode in each new option.

I would definately search for an option where you can populate an array like this automatically, so you have easier maintenace in the future in adding or removing options by just adding it, without having to edit the php code.

This is just an example, but the array instantiation should be automated tbh. Please dont copy paste this but use it as an inspiration.

function getString($key) {
  $filters = ['male' => [
                 'val'=>'male',
                 'type'=>'=',
                 'field'=>'gender'
                 ],
         'female' => [
                 'val'=>'female',
                 'type'=>'=',
                 'field'=>'gender'
                 ],
         '10'=>[
                 'val'=>'%Mann%',
                 'type'=>'LIKE',
                 'field'=>'gender'
                 ],
         '11'=>[
                 'val'=>'%Frau%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '12'=>[
                 'val'=>'%Paar%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '13'=>[
                 'val'=>'%Herren/-in%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '14'=>[
                 'val'=>'%Sklave/-in%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '15'=>[
                 'val'=>'%Domina%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '20'=>[
                 'val'=>'%Affäre%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
          '21'=>[
                 'val'=>'%Cybersex%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '22'=>[
                 'val'=>'%Ehe%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '23'=>[
                 'val'=>'%Flirt%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '24'=>[
                 'val'=>'%Freundschaft%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '25'=>[
                 'val'=>'%für alles offen%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '26'=>[
                 'val'=>'%Partnerschaft%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         '27'=>[
                 'val'=>'%Sex%',
                 'type'=>'LIKE',
                 'field'=>'interests'
                 ],
         ];
    if(isset($filters[$key])) {
          $cur = $filters[$key];
          return array($cur['field'],$cur['type'],$cur['val']);
    }
    return array('is_old', '=', '0');
}
$ret = getString('25');
var_dump($ret);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.