| Conditions | 11 |
| Paths | 6 |
| Total Lines | 62 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 80 | public static function getLevel(int $userXP): array |
||
| 81 | { |
||
| 82 | $infos = [ |
||
| 83 | 'previousLevelExperience' => 0, |
||
| 84 | 'previousLevel' => 0, |
||
| 85 | 'currentLevel' => 0, |
||
| 86 | 'currentLevelExperience' => 0, |
||
| 87 | 'currentUserExperience' => 0, |
||
| 88 | 'nextLevel' => 1, |
||
| 89 | 'experienceNeededNextLevel' => 400, |
||
| 90 | 'nextLevelExperience' => 400, |
||
| 91 | 'matchExactXPLevel' => false, |
||
| 92 | 'maxLevel' => false |
||
| 93 | ]; |
||
| 94 | |||
| 95 | if ($userXP === 0) { |
||
| 96 | return $infos; |
||
| 97 | } |
||
| 98 | |||
| 99 | for ($i = 0; $i < count(static::$levels); $i++) { |
||
|
|
|||
| 100 | // The XP of the user match the exact XP of the rank and there's another rank after this one. |
||
| 101 | if ($userXP === static::$levels[$i] && isset(static::$levels[$i + 1])) { |
||
| 102 | return array_merge($infos, [ |
||
| 103 | 'previousLevelExperience' => static::$levels[$i - 1], |
||
| 104 | 'previousLevel' => $i - 1, |
||
| 105 | 'currentLevel' => $i, |
||
| 106 | 'currentLevelExperience' => static::$levels[$i], |
||
| 107 | 'currentUserExperience' => $userXP, |
||
| 108 | 'nextLevel' => $i + 1, |
||
| 109 | 'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, |
||
| 110 | 'nextLevelExperience' => static::$levels[$i + 1], |
||
| 111 | 'matchExactXPLevel' => true |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | // If there's another rank after this one and the user XP is higher than the current rank. |
||
| 115 | if (isset(static::$levels[$i + 1]) && $userXP > static::$levels[$i]) { |
||
| 116 | // If the user XP is higher than the current rank but lower than the next rank. |
||
| 117 | if ($userXP > static::$levels[$i] && $userXP < static::$levels[$i + 1]) { |
||
| 118 | return array_merge($infos, [ |
||
| 119 | 'previousLevelExperience' => static::$levels[$i], |
||
| 120 | 'previousLevel' => $i === 0 ? 0 : $i - 1, |
||
| 121 | 'currentLevel' => $i, |
||
| 122 | 'currentLevelExperience' => static::$levels[$i], |
||
| 123 | 'currentUserExperience' => $userXP, |
||
| 124 | 'nextLevel' => $i + 1, |
||
| 125 | 'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, |
||
| 126 | 'nextLevelExperience' => static::$levels[$i + 1] |
||
| 127 | ]); |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | // The user has reached the max lvl |
||
| 131 | return array_merge($infos, [ |
||
| 132 | 'previousLevelExperience' => static::$levels[$i], |
||
| 133 | 'previousLevel' => $i === 0 ? 0 : $i - 1, |
||
| 134 | 'currentLevel' => $i, |
||
| 135 | 'currentLevelExperience' => static::$levels[$i], |
||
| 136 | 'currentUserExperience' => $userXP, |
||
| 137 | 'nextLevel' => 0, |
||
| 138 | 'experienceNeededNextLevel' => 0, |
||
| 139 | 'nextLevelExperience' => 0, |
||
| 140 | 'matchExactXPLevel' => (static::$levels[$i] - $userXP) === 0, |
||
| 141 | 'maxLevel' => true |
||
| 142 | ]); |
||
| 147 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: