Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class Histogram implements HistogramInterface |
||
| 19 | { |
||
| 20 | private $serie = []; |
||
| 21 | private int $differentHands; |
||
|
|
|||
| 22 | |||
| 23 | /** |
||
| 24 | * Get the serie. |
||
| 25 | * |
||
| 26 | * @return array with the serie. |
||
| 27 | */ |
||
| 28 | public function getSerie() |
||
| 29 | { |
||
| 30 | $pokerhighscores = Pokerhighscore::all(); |
||
| 31 | |||
| 32 | $royalstraightflush = $pokerhighscores->sum('count_royalstraightflush'); |
||
| 33 | $straightflush = $pokerhighscores->sum('count_straightflush'); |
||
| 34 | $fourofakind = $pokerhighscores->sum('count_fourofakind'); |
||
| 35 | $fullhouse = $pokerhighscores->sum('count_fullhouse'); |
||
| 36 | $flush = $pokerhighscores->sum('count_flush'); |
||
| 37 | $straight = $pokerhighscores->sum('count_straight'); |
||
| 38 | $threeofakind = $pokerhighscores->sum('count_threeofakind'); |
||
| 39 | $twopairs = $pokerhighscores->sum('count_twopairs'); |
||
| 40 | $pair = $pokerhighscores->sum('count_pair'); |
||
| 41 | $nothing = $pokerhighscores->sum('count_nothing'); |
||
| 42 | $sumAll = ( |
||
| 43 | $royalstraightflush + |
||
| 44 | $straightflush + |
||
| 45 | $fourofakind + |
||
| 46 | $fullhouse + |
||
| 47 | $flush + |
||
| 48 | $straight + |
||
| 49 | $threeofakind + |
||
| 50 | $twopairs + |
||
| 51 | $pair + |
||
| 52 | $nothing |
||
| 53 | ); |
||
| 54 | |||
| 55 | $this->serie = [ |
||
| 56 | ['Royal straight flush', round(($royalstraightflush / $sumAll) * 100, 1) . '%', $royalstraightflush], |
||
| 57 | ['Straight flush', round(($straightflush / $sumAll) * 100, 1) . '%', $straightflush], |
||
| 58 | ['Four of a kind', round(($fourofakind / $sumAll) * 100, 1) . '%',$fourofakind], |
||
| 59 | ['Full house', round(($fullhouse / $sumAll) * 100, 1) . '%',$fullhouse], |
||
| 60 | ['Flush', round(($flush / $sumAll) * 100, 1) . '%',$flush], |
||
| 61 | ['Straight', round(($straight / $sumAll) * 100, 1) . '%',$straight], |
||
| 62 | ['Three of a kind', round(($threeofakind / $sumAll) * 100, 1) . '%',$threeofakind], |
||
| 63 | ['Two pairs', round(($twopairs / $sumAll) * 100, 1) . '%',$twopairs], |
||
| 64 | ['Pair', round(($pair / $sumAll) * 100, 1) . '%',$pair], |
||
| 65 | ['Nothing', round(($nothing / $sumAll) * 100, 1) . '%',$nothing], |
||
| 66 | ]; |
||
| 67 | |||
| 68 | $this->differentHands = count($this->serie); |
||
| 69 | |||
| 70 | for ($hand = 0; $hand < $this->differentHands; $hand++) { |
||
| 71 | // change third element with count in each above series to * output |
||
| 72 | $this->serie[$hand][2] = str_repeat("*", $this->serie[$hand][2]); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $this->serie; |
||
| 78 |