| Conditions | 24 |
| Paths | 1728 |
| Total Lines | 82 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | function chess_ratings_adj_cxr($white_rating, $white_games, $black_rating, $black_games, $pgn_result) |
||
| 52 | { |
||
| 53 | // compute score: +1 for win, 0 for draw, -1 for loss |
||
| 54 | |||
| 55 | switch ($pgn_result) { |
||
| 56 | case '1-0': |
||
| 57 | $S = 1; |
||
| 58 | break; |
||
| 59 | case '1/2-1/2': |
||
| 60 | default: // should not occur |
||
| 61 | $S = 0; |
||
| 62 | break; |
||
| 63 | case '0-1': |
||
| 64 | $S = -1; |
||
| 65 | break; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (($white_games < 5 && $black_games < 5) || ($white_games > 5 && $black_games > 5)) { |
||
| 69 | // Formula 1: Rnew = Rold + (S x 21) + (Ropponent - Rold) / 25 |
||
| 70 | |||
| 71 | $w_new = ($S * 21) + ($black_rating - $white_rating) / 25; |
||
| 72 | |||
| 73 | $b_new = (-$S * 21) + ($white_rating - $black_rating) / 25; |
||
| 74 | } elseif ($white_games > 5 && $black_games < 5) { |
||
| 75 | // Formula 2: Rnew = Rold + (S x 6) + (Ropponent - Rold) / 100 |
||
| 76 | |||
| 77 | $w_new = ($S * 6) + ($black_rating - $white_rating) / 100; |
||
| 78 | |||
| 79 | // Formula 3: Rnew = (4/5) x Rold + (1/5) x Ropponent + (S x 80) |
||
| 80 | |||
| 81 | $b_new = ($white_rating / 5) + ($S * -80) - ($black_rating / 5); |
||
| 82 | } else { |
||
| 83 | // Formula 2: Rnew = Rold + (S x 6) + (Ropponent - Rold) / 100 |
||
| 84 | |||
| 85 | $b_new = ($S * 6) + ($white_rating - $black_rating) / 100; |
||
| 86 | |||
| 87 | // Formula 3: Rnew = (4/5) x Rold + (1/5) x Ropponent + (S x 80) |
||
| 88 | |||
| 89 | $w_new = ($black_rating / 5) + ($S * -80) - ($white_rating / 5); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Rule R1: The winning rated player must gain at least two points. |
||
| 93 | |||
| 94 | // Rule R2: The losing rated player must lose at least two points. |
||
| 95 | |||
| 96 | if (abs($w_new) < 2) { |
||
| 97 | $w_new = $S * 2; |
||
| 98 | } |
||
| 99 | |||
| 100 | if (abs($b_new) < 2) { |
||
| 101 | $b_new = $S * -2; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Rule R3: The rated player must not gain nor lose more than 41 points. |
||
| 105 | |||
| 106 | if (abs($w_new) > 41) { |
||
| 107 | $w_new = $S * 41; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (abs($b_new) > 41) { |
||
| 111 | $b_new = $S * -41; |
||
| 112 | } |
||
| 113 | |||
| 114 | if (1 == $S) { |
||
| 115 | if ($white_games < 5 && $w_new < 0) { |
||
| 116 | $w_new = 2; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($black_games < 5 && $b_new > 0) { |
||
| 120 | $b_new = -2; |
||
| 121 | } |
||
| 122 | } elseif (-1 == $S) { |
||
| 123 | if ($white_games < 5 && $w_new > 0) { |
||
| 124 | $w_new = -2; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($black_games < 5 && $b_new < 0) { |
||
| 128 | $b_new = 2; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return [$white_rating + $w_new, $black_rating + $b_new]; |
||
| 133 | } |
||
| 144 |