2017-06-10 7 views
1

switch文を削除したいと思いますが、実際にはわかりません。多態性コードへのリファクタリング・ステートメント

public function isDirectEliminationCompetitor() 
{ 
    return !$this->category->isTeam() && $this->isDirectEliminationType() && !$this->hasPreliminary(); 
} 

任意のアイデアはどのように私は、この方法の複雑さを向上させる必要があります:isDirectEliminationCompetitorよう

/** 
* @return TreeGenerable 
*/ 
public function chooseGenerationStrategy() 
{ 
    switch (true) { 
     case $this->isDirectEliminationCompetitor(): 
      $generation = new DirectEliminationCompetitorTreeGen($this, null); 
      break; 
     case $this->isDirectEliminationTeam(): 
      $generation = new DirectEliminationTeamTreeGen($this, null); 
      break; 
     case $this->isPlayoffCompetitor(): 
      $generation = new PlayOffCompetitorTreeGen($this, null); 
      break; 
     case $this->isPlayoffTeam(): 
      $generation = new PlayOffTeamTreeGen($this, null); 
      break; 
     default: 
      $generation = new PlayOffCompetitorTreeGen($this, null); 
    } 
    return $generation; 
} 

すべてのメソッドは、そのような条件はありますか?

答えて

2

cyclomatic and npath complexityを見ると、あなたの現在のアプローチはかなり低く、両方の複雑さは5です。 if文を使用する方法を変更する

、循環的複雑度は5のまましかしNPATH複雑さは16に上がる:

/** 
* @return TreeGenerable 
*/ 
public function chooseGenerationStrategy() 
{ 
    if ($this->isDirectEliminationCompetitor()) { 
     return new DirectEliminationCompetitorTreeGen($this, null); 
    } 

    if ($this->isDirectEliminationTeam()) { 
     return new DirectEliminationTeamTreeGen($this, null); 
    } 

    if ($this->isPlayoffCompetitor()) { 
     return new PlayOffCompetitorTreeGen($this, null); 
    } 

    if ($this->isPlayoffTeam()) { 
     return new PlayOffTeamTreeGen($this, null); 
    } 

    return new PlayOffCompetitorTreeGen($this, null); 
} 

elseifを使う代わりの文が5に両方戻ってそれらを下げた場合、それはまたそれを作りますはるかに少ない読める:

/** 
* @return TreeGenerable 
*/ 
public function chooseGenerationStrategy() 
{ 
    if ($this->isDirectEliminationCompetitor()) { 
     return new DirectEliminationCompetitorTreeGen($this, null); 
    } elseif ($this->isDirectEliminationTeam()) { 
     return new DirectEliminationTeamTreeGen($this, null); 
    } elseif ($this->isPlayoffCompetitor()) { 
     return new PlayOffCompetitorTreeGen($this, null); 
    } elseif ($this->isPlayoffTeam()) { 
     return new PlayOffTeamTreeGen($this, null); 
    } else { 
     return new PlayOffCompetitorTreeGen($this, null); 
    } 
} 

あなたは、このメソッドのために作ることができる唯一の簡素化がdefaultケースを削除し、結果を変数に代入するのではなく返すことです

/** 
* @return TreeGenerable 
*/ 
public function chooseGenerationStrategy() 
{ 
    switch (true) { 
     case $this->isDirectEliminationCompetitor(): 
      return new DirectEliminationCompetitorTreeGen($this, null); 
      break; 
     case $this->isDirectEliminationTeam(): 
      return new DirectEliminationTeamTreeGen($this, null); 
      break; 
     case $this->isPlayoffCompetitor(): 
      return new PlayOffCompetitorTreeGen($this, null); 
      break; 
     case $this->isPlayoffTeam(): 
      return new PlayOffTeamTreeGen($this, null); 
      break; 
    } 

    return new PlayOffCompetitorTreeGen($this, null); 
} 

これは5の循環的複雑度を維持しますが、4

にNPATHの複雑性を低下だから、あなたの現在のアプローチは、それが正確に最善かきれいに見えるではない可能性があっても、かなりの最も効率的であるようです。