| Conditions | 18 |
| Paths | 1936 |
| Total Lines | 130 |
| Code Lines | 72 |
| 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 |
||
| 43 | public function processResults(Ranking $ranking, $factions, $factionRankingManager) |
||
| 44 | { |
||
| 45 | #---------------- COMPUTING -------------------# |
||
| 46 | |||
| 47 | # copy the arrays |
||
| 48 | $listG = $this->results; |
||
| 49 | $listW = $this->results; |
||
| 50 | $listT = $this->results; |
||
| 51 | |||
| 52 | # sort all the copies |
||
| 53 | uasort($listG, [$this, 'cmpFactionGeneral']); |
||
| 54 | uasort($listW, [$this, 'cmpWealth']); |
||
| 55 | uasort($listT, [$this, 'cmpTerritorial']); |
||
| 56 | |||
| 57 | /*foreach ($list as $key => $value) { |
||
| 58 | echo $key . ' => ' . $value['general'] . '<br/>'; |
||
| 59 | }*/ |
||
| 60 | |||
| 61 | # put the position in each array |
||
| 62 | $listG = $this->setPositions($listG, 'general'); |
||
| 63 | $listW = $this->setPositions($listW, 'wealth'); |
||
| 64 | $listT = $this->setPositions($listT, 'territorial'); |
||
| 65 | |||
| 66 | #-------------------------------- POINTS RANKING -----------------------------# |
||
| 67 | |||
| 68 | # faire ce classement uniquement après x jours de jeu |
||
| 69 | if (Utils::interval(SERVER_START_TIME, Utils::now(), 'h') > HOURS_BEFORE_START_OF_RANKING) { |
||
| 70 | # points qu'on gagne en fonction de sa place dans le classement |
||
| 71 | $pointsToEarn = [40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0]; |
||
| 72 | $coefG = 0.1; # 4 3 2 1 0 ... |
||
| 73 | $coefW = 0.4; # 16 12 8 4 0 ... |
||
| 74 | $coefT = 0.5; # 20 15 10 5 0 ... |
||
| 75 | |||
| 76 | foreach ($factions as $faction) { |
||
| 77 | $factionId = $faction->id; |
||
| 78 | $additionalPoints = 0; |
||
| 79 | |||
| 80 | # general |
||
| 81 | $additionalPoints += intval($pointsToEarn[$listG[$factionId]['position'] - 1] * $coefG); |
||
| 82 | |||
| 83 | # wealth |
||
| 84 | $additionalPoints += intval($pointsToEarn[$listW[$factionId]['position'] - 1] * $coefW); |
||
| 85 | |||
| 86 | # territorial |
||
| 87 | $additionalPoints += intval($pointsToEarn[$listT[$factionId]['position'] - 1] * $coefT); |
||
| 88 | |||
| 89 | $this->results[$factionId]['points'] += $additionalPoints; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | #---------------- LAST COMPUTING -------------------# |
||
| 95 | |||
| 96 | $listP = $this->results; |
||
| 97 | uasort($listP, [$this, 'cmpPoints']); |
||
| 98 | |||
| 99 | $position = 1; |
||
| 100 | foreach ($listP as $key => $value) { $listP[$key]['position'] = $position++;} |
||
| 101 | |||
| 102 | #---------------- SAVING -------------------# |
||
| 103 | |||
| 104 | $rankings = []; |
||
| 105 | |||
| 106 | foreach ($factions as $faction) { |
||
| 107 | $factionId = $faction->getId(); |
||
| 108 | $fr = new FactionRanking(); |
||
| 109 | $fr->rRanking = $ranking->getId(); |
||
| 110 | $fr->rFaction = $factionId; |
||
| 111 | |||
| 112 | $firstRanking = true; |
||
| 113 | for ($i = 0; $i < $factionRankingManager->size(); $i++) { |
||
| 114 | if ($factionRankingManager->get($i)->rFaction == $factionId) { |
||
| 115 | $oldRanking = $factionRankingManager->get($i); |
||
| 116 | $firstRanking = false; |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $fr->general = $listG[$factionId]['general']; |
||
| 122 | $fr->generalPosition = $listG[$factionId]['position']; |
||
| 123 | $fr->generalVariation = $firstRanking ? 0 : $oldRanking->generalPosition - $fr->generalPosition; |
||
|
|
|||
| 124 | |||
| 125 | $fr->wealth = $listW[$factionId]['wealth']; |
||
| 126 | $fr->wealthPosition = $listW[$factionId]['position']; |
||
| 127 | $fr->wealthVariation = $firstRanking ? 0 : $oldRanking->wealthPosition - $fr->wealthPosition; |
||
| 128 | |||
| 129 | $fr->territorial = $listT[$factionId]['territorial']; |
||
| 130 | $fr->territorialPosition = $listT[$factionId]['position']; |
||
| 131 | $fr->territorialVariation = $firstRanking ? 0 : $oldRanking->territorialPosition - $fr->territorialPosition; |
||
| 132 | |||
| 133 | if ($this->isGameOver === true) { |
||
| 134 | $fr->points = $oldRanking->points; |
||
| 135 | $fr->pointsPosition = $oldRanking->pointsPosition; |
||
| 136 | $fr->pointsVariation = 0; |
||
| 137 | $fr->newPoints = 0; |
||
| 138 | } else { |
||
| 139 | $fr->points = $listP[$factionId]['points']; |
||
| 140 | $fr->pointsPosition = $listP[$factionId]['position']; |
||
| 141 | $fr->pointsVariation = $firstRanking ? 0 : $oldRanking->pointsPosition - $fr->pointsPosition; |
||
| 142 | $fr->newPoints = $firstRanking ? $fr->points : $fr->points - $oldRanking->points; |
||
| 143 | } |
||
| 144 | |||
| 145 | # update faction infos |
||
| 146 | $faction->rankingPoints = $listP[$factionId]['points']; |
||
| 147 | $faction->points = $listG[$factionId]['general']; |
||
| 148 | $faction->sectors = $listT[$factionId]['territorial']; |
||
| 149 | |||
| 150 | //$this->colorManager->updateInfos($faction); |
||
| 151 | |||
| 152 | $rankings[] = $fr; |
||
| 153 | $factionRankingManager->add($fr); |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($this->isGameOver === false) { |
||
| 157 | # check if a faction wins the game |
||
| 158 | $winRanking = NULL; |
||
| 159 | foreach ($rankings as $ranking) { |
||
| 160 | if ($ranking->points >= POINTS_TO_WIN) { |
||
| 161 | if ($winRanking !== NULL) { |
||
| 162 | if ($winRanking->points < $ranking->points) { |
||
| 163 | return $ranking->rFaction; |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | return $ranking->rFaction; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return null; |
||
| 172 | } |
||
| 173 | |||
| 247 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: