0

PHPMD is telling me that I should avoid else block in this test, but in those case, I can't find a way to remove them.

Here is the code:

if ($fight->c1 == NULL) {
    if ($fight->c2 == NULL) {
        // C1 and C2 Is Bye
        $this->assertEquals($parentFight->$toUpdate, NULL);
    }
    else {
        // C1 Is Bye
        $this->assertEquals($parentFight->$toUpdate, $fight->c2);
    }
}
else {
    if ($fight->c2 == NULL) {
        // C2 Is Bye
        $this->assertEquals($parentFight->$toUpdate, $fight->c1);
    }
    else {
        // C1 and C2 Are all set
        $this->assertEquals($parentFight->$toUpdate, NULL);
    }
}

Any Idea???

lighter
  • 2,808
  • 3
  • 40
  • 59
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
  • is // C1 and C2 Are all set :: $this->assertEquals($parentFight->$toUpdate, null); OR $this->assertEquals($parentFight->$toUpdate, $flight->c2); ? – Shan May 22 '17 at 06:47
  • Have you found a solution. Did any of our answers helped? – Gayan May 23 '17 at 08:40

7 Answers7

1

There is another way to do this :

if(($fight->c1 == null && $fight->c2 == null) || ($fight->c1 != null && $fight->c2 != null)) {
    // C1 and C2 Is Bye
    // C1 and C2 Are all set
    $this->assertEquals($parentFight->$toUpdate, null);
} else if($fight->c1 == null && $fight->c2 != null) {
    // C1 Is Bye
    $this->assertEquals($parentFight->$toUpdate, $fight->c2);
} else if($fight->c1 != null && $fight->c2 == null) {
    // C2 Is Bye
    $this->assertEquals($parentFight->$toUpdate, $fight->c1);
}
Sagar Jajoriya
  • 2,377
  • 1
  • 9
  • 17
1

It could also be done with a ternary operator, something like this.

if (!$fight->c1) {
    $this->assertEquals($parentFight->$toUpdate, ($fight->c2 ?: null));
}

if (!$fight->c2) {
    $this->assertEquals($parentFight->$toUpdate, ($fight->c2 ?: null));
}
1
$checkValue = null;
$cntNulls = (int)is_null($fight->c1) + (int)is_null($fight->c2);
if ($cntNulls === 1) {
    $checkValue = is_null($fight->c1) ? $fight->c2 : $fight->c1;
}

$this->assertEquals($parentFight->$toUpdate, $checkValue);
maximkou
  • 5,252
  • 1
  • 20
  • 41
1

It seems like when $fight->c1 not null, you want to pass $fight->c1. And when $fight->c2 not null, you want to pass $fight->c2. And when both are null you want to pass null.

What you'd simply do is,

$param = null;
if($fight->c1 != null)
{
    $param = $fight->c1;
}
if($fight->c2 != null)
{
    $param = $fight->c2;
}

$this->assertEquals($parentFight->$toUpdate, $param);

But I'd go one step further and abstract the $param resolving process like,

private function relolveParam($fight) {
    $param = null;
    if($fight->c1 != null)
    {
        $param = $fight->c1;
    }
    if($fight->c2 != null)
    {
        $param = $fight->c2;
    }
    return $param;
}

Then you're only end up with,

$this->assertEquals($parentFight->$toUpdate, $this->relolveParam($fight));
Gayan
  • 3,614
  • 1
  • 27
  • 34
0

Use else if rather then multiple if...else

if ($fight->c1 == null && $fight->c2 == null) {
    // C1 and C2 Is Bye
    $this->assertEquals($parentFight->$toUpdate, null);
} else if($fight->c1 == null &&  $fight->c2 != null) {
    // C1 Is Bye
    $this->assertEquals($parentFight->$toUpdate, $fight->c2);
} else if($fight->c1 != null &&  $fight->c2 == null) {
    // C2 Is Bye
    $this->assertEquals($parentFight->$toUpdate, $fight->c1);
} else {
    // C1 and C2 Are all set
    $this->assertEquals($parentFight->$toUpdate, null);
}
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

You can use two if{} to instead of if{}else{} like this,

if(a){
  //do a
}else{
  //do !a
}

if(a){
  //do a
}
if(!a){
  //do !a
} 
LF00
  • 27,015
  • 29
  • 156
  • 295
0

You could also make one test for each of the cases you are testing for, having 4 clear tests instead of one test where it is not obvious how all paths are tested

rypskar
  • 2,012
  • 13
  • 13