2017-05-22 3 views
0

PHPMDは、このテストでelseブロックを避けるべきだと言っていますが、その場合は削除する方法がありません。ここでelseブロックを削除する方法

はコードです:

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); 
    } 
} 

任意のアイデア?

+0

は、C1とC2は、すべてのセット:: ます$ this->のassertEquals($ parentFightです//ですte、null); OR $ this-> assertEquals($ parentFight - > $ toUpdate、$ flight-> c2); ? – Shan

+0

あなたは解決策を見つけましたか?私たちの答えのいずれかが助けられましたか? – Gayan

答えて

1

これはまた、三元オペラトr、これのようなもの。

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

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

使用else ifではなく、複数の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); 
} 
1

これを行うための別の方法があります:あなたは2 if{}への代わりに、このようなif{}else{}を使用することができます

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); 
} 
0

は、

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

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

また、代わりに、それはそう、すべてのパスが

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); 
1

をテストしているか明らかでない一つのテストの4つの明確なテストを持つ、あなたがテストしている例ごとに1つのテストを作ることができます$fight->c1nullでない場合のように、$fight->c1を渡したいとします。 $fight->c2nullではない場合は、$fight->c2を渡します。両方ともnullの場合は、nullを渡します。

あなたがあるか、単純に思い何

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

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

しかし、私はさらに一歩行くだろうと抽象$param解決プロセスのような、そして、

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

はあなただけで終わるています> $ toUpda - 、

$this->assertEquals($parentFight->$toUpdate, $this->relolveParam($fight)); 
関連する問題