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