| Conditions | 12 | 
| Paths | 6 | 
| Total Lines | 62 | 
| Code Lines | 49 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 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 | ||
| 71 | public static function getLevel(int $userXP) | ||
| 72 |     { | ||
| 73 | $infos = [ | ||
| 74 | 'previousLevelExperience' => 0, | ||
| 75 | 'previousLevel' => 0, | ||
| 76 | 'currentLevel' => 0, | ||
| 77 | 'currentLevelExperience' => 0, | ||
| 78 | 'currentUserExperience' => 0, | ||
| 79 | 'nextLevel' => 1, | ||
| 80 | 'experienceNeededNextLevel' => 400, | ||
| 81 | 'nextLevelExperience' => 400, | ||
| 82 | 'matchExactXPLevel' => false, | ||
| 83 | 'maxLevel' => false | ||
| 84 | ]; | ||
| 85 | |||
| 86 |         if ($userXP == 0) { | ||
| 87 | return $infos; | ||
| 88 | } | ||
| 89 | |||
| 90 |         for ($i = 0; $i < count(static::$levels); $i++) { | ||
|  | |||
| 91 | // The XP of the user match the exact XP of the rank and there's another rank after this one. | ||
| 92 |             if ($userXP == static::$levels[$i] && isset(static::$levels[$i + 1])) { | ||
| 93 | return array_merge($infos, [ | ||
| 94 | 'previousLevelExperience' => static::$levels[$i - 1], | ||
| 95 | 'previousLevel' => $i - 1, | ||
| 96 | 'currentLevel' => $i, | ||
| 97 | 'currentLevelExperience' => static::$levels[$i], | ||
| 98 | 'currentUserExperience' => $userXP, | ||
| 99 | 'nextLevel' => $i + 1, | ||
| 100 | 'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, | ||
| 101 | 'nextLevelExperience' => static::$levels[$i + 1], | ||
| 102 | 'matchExactXPLevel' => true | ||
| 103 | ]); | ||
| 104 |             } else { | ||
| 105 | // If there's another rank after this one and the user XP is higher than the current rank. | ||
| 106 |                 if (isset(static::$levels[$i + 1]) && $userXP > static::$levels[$i]) { | ||
| 107 | // If the user XP is higher than the current rank but lower than the next rank. | ||
| 108 |                     if ($userXP > static::$levels[$i] && $userXP < static::$levels[$i + 1]) { | ||
| 109 | return array_merge($infos, [ | ||
| 110 | 'previousLevelExperience' => static::$levels[$i], | ||
| 111 | 'previousLevel' => $i == 0 ? 0 : $i - 1, | ||
| 112 | 'currentLevel' => $i, | ||
| 113 | 'currentLevelExperience' => static::$levels[$i], | ||
| 114 | 'currentUserExperience' => $userXP, | ||
| 115 | 'nextLevel' => $i + 1, | ||
| 116 | 'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP, | ||
| 117 | 'nextLevelExperience' => static::$levels[$i + 1] | ||
| 118 | ]); | ||
| 119 | } | ||
| 120 |                 } else { | ||
| 121 | // The user has reached the max lvl | ||
| 122 | return array_merge($infos, [ | ||
| 123 | 'previousLevelExperience' => static::$levels[$i], | ||
| 124 | 'previousLevel' => $i == 0 ? 0 : $i - 1, | ||
| 125 | 'currentLevel' => $i, | ||
| 126 | 'currentLevelExperience' => static::$levels[$i], | ||
| 127 | 'currentUserExperience' => $userXP, | ||
| 128 | 'nextLevel' => 0, | ||
| 129 | 'experienceNeededNextLevel' => 0, | ||
| 130 | 'nextLevelExperience' => 0, | ||
| 131 | 'matchExactXPLevel' => (static::$levels[$i] - $userXP) == 0 ? true : false, | ||
| 132 | 'maxLevel' => true | ||
| 133 | ]); | ||
| 209 | 
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: