0

PHPMD は、このテストでは 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);
    }
}

何か案が???

4

7 に答える 7

1

これを行う別の方法があります:

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);
}
于 2017-05-22T06:35:06.650 に答える
1

次のような三項演算子を使用して実行することもできます。

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

if (!$fight->c2) {
    $this->assertEquals($parentFight->$toUpdate, ($fight->c2 ?: null));
}
于 2017-05-22T06:49:58.243 に答える
0

テストするケースごとに 1 つのテストを作成することもできます。すべてのパスがどのようにテストされるかが明らかでない 1 つのテストではなく、4 つの明確なテストがあります。

于 2017-05-22T06:51:42.030 に答える