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