Conditions | 27 |
Paths | 845 |
Total Lines | 161 |
Code Lines | 97 |
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 |
||
126 | private function processPlayerChartData(array $data, int $index, array &$errors): ?array |
||
127 | { |
||
128 | try { |
||
129 | // Extraction des IDs depuis les formats API Platform ou simples |
||
130 | $chartId = $this->extractId($data['chart'] ?? null); |
||
131 | $playerId = $this->extractId($data['player'] ?? null); |
||
132 | |||
133 | // Validation des données requises |
||
134 | if (!$chartId || !$playerId) { |
||
135 | $errors[$index] = 'chart and player are required fields'; |
||
136 | return null; |
||
137 | } |
||
138 | |||
139 | // Récupération des entités référencées |
||
140 | $chart = $this->entityManager->getRepository(Chart::class)->find($chartId); |
||
141 | if (!$chart) { |
||
142 | $errors[$index] = sprintf('Chart with id %s not found', $chartId); |
||
143 | return null; |
||
144 | } |
||
145 | |||
146 | $player = $this->entityManager->getRepository(Player::class)->find($playerId); |
||
147 | if (!$player) { |
||
148 | $errors[$index] = sprintf('Player with id %s not found', $playerId); |
||
149 | return null; |
||
150 | } |
||
151 | |||
152 | // Vérifier si c'est une mise à jour ou une création |
||
153 | $isUpdate = false; |
||
154 | $playerChart = null; |
||
155 | $playerChartId = $data['id'] ?? null; |
||
156 | |||
157 | if ($playerChartId) { |
||
158 | // Mode modification : récupérer l'entité existante |
||
159 | $playerChart = $this->entityManager->getRepository(PlayerChart::class)->find($playerChartId); |
||
160 | if (!$playerChart) { |
||
161 | $errors[$index] = sprintf('PlayerChart with id %s not found', $playerChartId); |
||
162 | return null; |
||
163 | } |
||
164 | |||
165 | // Vérifier que l'utilisateur peut modifier ce PlayerChart |
||
166 | if ($playerChart->getPlayer()->getId() !== $player->getId()) { |
||
167 | $errors[$index] = 'Cannot modify PlayerChart of another player'; |
||
168 | return null; |
||
169 | } |
||
170 | |||
171 | $isUpdate = true; |
||
172 | } else { |
||
173 | // Mode création : vérifier qu'il n'existe pas déjà |
||
174 | $existingPlayerChart = $this->entityManager->getRepository(PlayerChart::class) |
||
175 | ->findOneBy(['chart' => $chart, 'player' => $player]); |
||
176 | |||
177 | if ($existingPlayerChart) { |
||
178 | $errors[$index] = sprintf( |
||
179 | 'PlayerChart already exists for player %d and chart %d', |
||
180 | $player->getId(), |
||
181 | $chart->getId() |
||
182 | ); |
||
183 | return null; |
||
184 | } |
||
185 | |||
186 | // Création du PlayerChart |
||
187 | $playerChart = new PlayerChart(); |
||
188 | $playerChart->setChart($chart); |
||
189 | $playerChart->setPlayer($player); |
||
190 | } |
||
191 | |||
192 | // Status - gérer les formats API Platform et simples |
||
193 | $statusId = $this->extractId($data['status'] ?? null) ?? 1; |
||
194 | $status = $this->entityManager->getRepository(PlayerChartStatus::class)->find($statusId); |
||
195 | if (!$status) { |
||
196 | $errors[$index] = 'Invalid status provided'; |
||
197 | return null; |
||
198 | } |
||
199 | $playerChart->setStatus($status); |
||
200 | $playerChart->setProof(); |
||
201 | |||
202 | // Platform optionnelle - gérer les formats API Platform et simples |
||
203 | if (isset($data['platform'])) { |
||
204 | $platformId = $this->extractId($data['platform']); |
||
205 | if ($platformId) { |
||
206 | $platform = $this->entityManager->getRepository(Platform::class)->find($platformId); |
||
207 | if (!$platform) { |
||
208 | $errors[$index] = sprintf('Platform with id %s not found', $platformId); |
||
209 | return null; |
||
210 | } |
||
211 | $playerChart->setPlatform($platform); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | // Gestion des libs (valeurs du score) |
||
216 | if (isset($data['libs']) && is_array($data['libs'])) { |
||
217 | if ($isUpdate) { |
||
218 | // En mode édition, mettre à jour les libs existantes |
||
219 | $this->updatePlayerChartLibs($playerChart, $data['libs'], $index, $errors); |
||
220 | } else { |
||
221 | // En mode création, ajouter les nouvelles libs |
||
222 | foreach ($data['libs'] as $libData) { |
||
223 | $chartLibId = $this->extractId($libData['libChart'] ?? $libData['chartLib'] ?? null); |
||
224 | $parseValue = $libData['parseValue'] ?? null; |
||
225 | $value = $libData['value'] ?? null; |
||
226 | |||
227 | if (!$chartLibId || ($parseValue === null && $value === null)) { |
||
228 | $errors[$index] = 'libChart/chartLib and parseValue (or value) are required for each lib'; |
||
229 | return null; |
||
230 | } |
||
231 | |||
232 | $chartLib = $this->entityManager |
||
233 | ->getRepository(ChartLib::class) |
||
234 | ->find($chartLibId); |
||
235 | |||
236 | if (!$chartLib) { |
||
237 | $errors[$index] = sprintf('ChartLib with id %s not found', $chartLibId); |
||
238 | return null; |
||
239 | } |
||
240 | |||
241 | $playerChartLib = new PlayerChartLib(); |
||
242 | $playerChartLib->setLibChart($chartLib); |
||
243 | |||
244 | if ($parseValue !== null) { |
||
245 | // Si parseValue est fourni, l'utiliser et appeler setValueFromPaseValue() |
||
246 | $playerChartLib->setParseValue($parseValue); |
||
247 | $playerChartLib->setValueFromPaseValue(); |
||
248 | } else { |
||
249 | // Sinon utiliser value directement (rétrocompatibilité) |
||
250 | $playerChartLib->setValue($value); |
||
251 | } |
||
252 | |||
253 | $playerChart->addLib($playerChartLib); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | if (!empty($errors)) { |
||
258 | return null; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | // Mettre à jour lastUpdate avec la date du jour et forcer le statut à 1 |
||
263 | $playerChart->setLastUpdate(new \DateTime()); |
||
264 | $defaultStatus = $this->entityManager->getRepository(PlayerChartStatus::class)->find(1); |
||
265 | if ($defaultStatus) { |
||
266 | $playerChart->setStatus($defaultStatus); |
||
267 | } |
||
268 | |||
269 | // Validation de l'entité |
||
270 | $violations = $this->validator->validate($playerChart); |
||
271 | if (count($violations) > 0) { |
||
272 | $violationMessages = []; |
||
273 | foreach ($violations as $violation) { |
||
274 | $violationMessages[] = $violation->getMessage(); |
||
275 | } |
||
276 | $errors[$index] = implode(', ', $violationMessages); |
||
277 | return null; |
||
278 | } |
||
279 | |||
280 | return [ |
||
281 | 'playerChart' => $playerChart, |
||
282 | 'isUpdate' => $isUpdate |
||
283 | ]; |
||
284 | } catch (\Exception $e) { |
||
285 | $errors[$index] = 'Error processing PlayerChart: ' . $e->getMessage(); |
||
286 | return null; |
||
287 | } |
||
377 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths