-3

I've an if condition which have four or operators in it and it is possible that in future more or operators condition could be added in the if statement. I want to know is there any way we could refactor the condition or improve it.

$setRequired = FALSE;
if($conditionValue['value1'] || $conditionValue['value2'] || $conditionValue['value3'] || $conditionValue['value4']){
    $setRequired = TRUE;
}

Any help would be appreaciated.

user3588408
  • 301
  • 6
  • 9

5 Answers5

1

Try using EnumSet.

I will generally use this one

  if(condA()) {
  if(condB()) {
  //foo
  } else { //!condB
  //bar
  } else { //!condA
  //etc
  }
Ram Ki
  • 282
  • 3
  • 16
1

Your if is fine, the only thing you can do to make it more readable if you have more conditions is to format it like the example below:

if(
    $conditionValue['value1']
    || $conditionValue['value2']
    || $conditionValue['value3']
    || $conditionValue['value4']
) {
    $setRequired = TRUE;
}

If you just want to know if any of the values in your $conditionValue array is true, you can use in_array(). This only works when your array looks like in the below example:

$conditionValue = array(
    "value1" => false,
    "value2" => true,
    "value3" => false,
    "value4" => false,
    "value5" => false,
    "value6" => false
);

$setRequired = FALSE;

if( in_array( true, $conditionValue ) ){
    $setRequired = TRUE;
}
swidmann
  • 2,787
  • 1
  • 18
  • 32
  • 1
    This is the best answer. While the OP may be asking for (and as others are answering) a clever "fewer characters of code" solution, none of them are obvious to understand, and code "should" be obvious. – Flosculus Dec 11 '15 at 09:53
  • 1
    I think it's more important to have a readable code, then some magic, that looks insane, but no one can see when the condition is true. I have always [this](http://stackoverflow.com/a/316233/5297359) in my mind – swidmann Dec 11 '15 at 09:59
0

Create an array with the keys you want to check

$referenceKeys = array('value1','value2','value3','value4');

You can add to this as needed.

Then loop over the initial array and do an if clause to check your initial condition first and if the current key is in the reference key array.

foreach($conditionValue as $key=>$value){
    if ( $value and in_array($key,$referenceKeys)){
        $setRequired = TRUE;
    }
}
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
0

Try the foreach loop through the key values:

$setRequired = FALSE;
$variables = array('value1','value2','value3','value4');
foreach ($variables AS $var) {
    if ($conditionValue[$var]) {
        $setRequired = TRUE;
        break;
    }
}
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
0

talking about speed I think that your actual solution is the best.

talking about readability, or maintainability maybe you can sum all values and test result.

supposing $conditionValue['valueX'] = 0 for false or 1 (or -1 or something <>0) for true, you can add up all your $conditionValues and then only test sum result <> 0

example:

$sumResult = $conditionValue['value1'] +  $conditionValue['value2'] + .. + $conditionValue['valueX'];

if($sumResult){
    $setRequired = TRUE;
}

pay attention if $conditionValue['valueX'] can contain both positive and negative values this trick does not work.

if value1, value2.. valueX are sequential, you can also use a loop for adding up $conditionValue(s)

for ($i = 1; $i <= $lastValueToTest; $i++) {
    $sumResult = $sumResult + $conditionValue['value' . $i];
}
MtwStark
  • 3,866
  • 1
  • 18
  • 32