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