| Conditions | 18 |
| Paths | 1445 |
| Total Lines | 133 |
| Code Lines | 86 |
| 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 |
||
| 32 | public function handle($mixed): void |
||
| 33 | { |
||
| 34 | /** @var Chart $chart */ |
||
| 35 | $chart = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Chart')->find($mixed); |
||
| 36 | if (null === $chart) { |
||
| 37 | return; |
||
| 38 | } |
||
| 39 | |||
| 40 | $game = $chart->getGroup()->getGame(); |
||
| 41 | $this->groups[$chart->getGroup()->getId()] = $chart->getGroup(); |
||
| 42 | $this->games[$game->getId()] = $game; |
||
| 43 | |||
| 44 | if ($game->getSerie() !== null && $game->getSerie()->getStatus()->isActive()) { |
||
| 45 | $this->series[$game->getSerie()->getId()] = $game->getSerie(); |
||
| 46 | } |
||
| 47 | |||
| 48 | $ranking = $this->playerChartRankingProvider->getRanking($chart); |
||
| 49 | $pointsChart = Ranking::chartPointProvider(count($ranking)); |
||
| 50 | |||
| 51 | $topScoreLibValue = ''; |
||
| 52 | $previousLibValue = ''; |
||
| 53 | $rank = 1; |
||
| 54 | $nbEqual = 1; |
||
| 55 | $playerChartEqual = []; |
||
| 56 | |||
| 57 | $result = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\PlayerChart')->getPlatforms($chart); |
||
| 58 | $platforms = []; |
||
| 59 | foreach ($result as $row) { |
||
| 60 | $platforms[$row['id']] = [ |
||
| 61 | 'count' => $row['nb'], |
||
| 62 | 'points' => Ranking::platformPointProvider($row['nb']), |
||
| 63 | 'previousLibValue' => '', |
||
| 64 | 'rank' => 0, |
||
| 65 | 'nbEqual' => 1, |
||
| 66 | 'playerChartEqual' => [], |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | |||
| 70 | foreach ($ranking as $k => $item) { |
||
| 71 | $libValue = ''; |
||
| 72 | /** @var PlayerChart $playerChart */ |
||
| 73 | $playerChart = $item[0]; |
||
| 74 | |||
| 75 | $this->players[$playerChart->getPlayer()->getId()] = $playerChart->getPlayer(); |
||
| 76 | |||
| 77 | // Lost position ? |
||
| 78 | $oldRank = $playerChart->getRank(); |
||
| 79 | $oldNbEqual = $playerChart->getNbEqual(); |
||
| 80 | $playerChart->setTopScore(false); |
||
| 81 | |||
| 82 | foreach ($chart->getLibs() as $lib) { |
||
| 83 | $libValue .= $item['value_' . $lib->getIdLibChart()] . '/'; |
||
| 84 | } |
||
| 85 | if ($k === 0) { |
||
| 86 | // Premier élément => topScore |
||
| 87 | $playerChart->setTopScore(true); |
||
| 88 | $topScoreLibValue = $libValue; |
||
| 89 | } else { |
||
| 90 | if ($libValue === $topScoreLibValue) { |
||
| 91 | $playerChart->setTopScore(true); |
||
| 92 | } |
||
| 93 | if ($previousLibValue === $libValue) { |
||
| 94 | ++$nbEqual; |
||
| 95 | } else { |
||
| 96 | $rank += $nbEqual; |
||
| 97 | $nbEqual = 1; |
||
| 98 | $playerChartEqual = []; |
||
| 99 | } |
||
| 100 | |||
| 101 | } |
||
| 102 | // Platform point |
||
| 103 | if ($playerChart->getPlatform() != null) { |
||
| 104 | $idPlatForm = $playerChart->getPlatform()->getId(); |
||
| 105 | if ($platforms[$idPlatForm]['previousLibValue'] === $libValue) { |
||
| 106 | ++$platforms[$idPlatForm]['nbEqual']; |
||
| 107 | } else { |
||
| 108 | $platforms[$idPlatForm]['rank'] += $platforms[$idPlatForm]['nbEqual']; |
||
| 109 | $platforms[$idPlatForm]['nbEqual'] = 1; |
||
| 110 | $platforms[$idPlatForm]['playerChartEqual'] = []; |
||
| 111 | } |
||
| 112 | $platforms[$idPlatForm]['playerChartEqual'][] = $playerChart; |
||
| 113 | } |
||
| 114 | |||
| 115 | $playerChartEqual[] = $playerChart; |
||
| 116 | |||
| 117 | $playerChart |
||
| 118 | ->setNbEqual($nbEqual) |
||
| 119 | ->setRank($rank) |
||
| 120 | ->setPointChart((int) ( |
||
| 121 | array_sum( |
||
| 122 | array_slice(array_values($pointsChart), $playerChart->getRank() - 1, $playerChart->getNbEqual()) |
||
| 123 | ) / $playerChart->getNbEqual() |
||
| 124 | )); |
||
| 125 | |||
| 126 | if ($nbEqual > 1) { |
||
| 127 | // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points |
||
| 128 | foreach ($playerChartEqual as $playerChartToModify) { |
||
| 129 | $playerChartToModify |
||
| 130 | ->setNbEqual($nbEqual) |
||
| 131 | ->setPointChart($playerChart->getPointChart()); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($playerChart->getPlatform() != null) { |
||
| 136 | $idPlatForm = $playerChart->getPlatform()->getId(); |
||
| 137 | $playerChart->setPointPlatform((int) ( |
||
| 138 | array_sum( |
||
| 139 | array_slice(array_values($platforms[$idPlatForm]['points']), $platforms[$idPlatForm]['rank'] - 1, $platforms[$idPlatForm]['nbEqual']) |
||
| 140 | ) / $platforms[$idPlatForm]['nbEqual'] |
||
| 141 | )); |
||
| 142 | if ($platforms[$idPlatForm]['nbEqual'] > 1) { |
||
| 143 | // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points |
||
| 144 | foreach ($platforms[$idPlatForm]['playerChartEqual'] as $playerChartToModify) { |
||
| 145 | $playerChartToModify |
||
| 146 | ->setPointPlatform($playerChart->getPointPlatform()); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $playerChart->setPointPlatform(0); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | $event = new PlayerChartEvent($playerChart, $oldRank, $oldNbEqual); |
||
| 155 | $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED); |
||
| 156 | |||
| 157 | $previousLibValue = $libValue; |
||
| 158 | |||
| 159 | // Platform point |
||
| 160 | if ($playerChart->getPlatform() != null) { |
||
| 161 | $platforms[$playerChart->getPlatform()->getId()]['previousLibValue'] = $libValue; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $this->em->flush(); |
||
| 165 | } |
||
| 187 |