| Conditions | 8 |
| Paths | 34 |
| Total Lines | 74 |
| 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 |
||
| 26 | public function getUserStatistics(User $user) |
||
| 27 | { |
||
| 28 | $downloads = $this->ponthubFileUserRepository->findBy(['user' => $user], ['date' => 'ASC']); |
||
| 29 | $totalFiles = count($downloads); |
||
| 30 | |||
| 31 | if ($totalFiles == 0) { |
||
| 32 | return ['repartition' => [], 'timeline' => [], 'totalSize' => 0, 'totalFiles' => 0, 'hipster' => 0]; |
||
| 33 | } |
||
| 34 | |||
| 35 | // Récupération de la date javascript du premier download |
||
| 36 | $date = 1000*($downloads[0]->getDate() - 10*3600); |
||
| 37 | |||
| 38 | // Initialisation des tableaux à retourner |
||
| 39 | $repartition = [ |
||
| 40 | ['Films', 0], |
||
| 41 | ['Épisodes', 0], |
||
| 42 | ['Jeux', 0], |
||
| 43 | ['Logiciels', 0], |
||
| 44 | ['Autres', 0] |
||
| 45 | ]; |
||
| 46 | $timeline = [ |
||
| 47 | ['name' => 'Films', 'data' => [[$date, 0]]], |
||
| 48 | ['name' => 'Épisodes', 'data' => [[$date, 0]]], |
||
| 49 | ['name' => 'Jeux', 'data' => [[$date, 0]]], |
||
| 50 | ['name' => 'Logiciels', 'data' => [[$date, 0]]], |
||
| 51 | ['name' => 'Autres', 'data' => [[$date, 0]]] |
||
| 52 | ]; |
||
| 53 | $totalSize = 0; |
||
| 54 | $hipster = 0; |
||
| 55 | |||
| 56 | // On complète les tableaux au fur et à mesure |
||
| 57 | foreach ($downloads as $download) { |
||
| 58 | $file = $download->getFile(); |
||
| 59 | // Conversion en millisecondes, unité javascript de base |
||
| 60 | $date = $download->getDate()*1000; |
||
| 61 | |||
| 62 | if ($file instanceof Movie) { |
||
| 63 | $repartition[0][1]++; |
||
| 64 | $this->updateSeries($timeline, $date, 0); |
||
| 65 | } |
||
| 66 | if ($file instanceof Episode) { |
||
| 67 | $repartition[1][1]++; |
||
| 68 | $this->updateSeries($timeline, $date, 1); |
||
| 69 | } |
||
| 70 | if ($file instanceof Game) { |
||
| 71 | $repartition[2][1]++; |
||
| 72 | $this->updateSeries($timeline, $date, 3); |
||
| 73 | } |
||
| 74 | if ($file instanceof Software) { |
||
| 75 | $repartition[3][1]++; |
||
| 76 | $this->updateSeries($timeline, $date, 4); |
||
| 77 | } |
||
| 78 | if ($file instanceof Other) { |
||
| 79 | $repartition[4][1]++; |
||
| 80 | $this->updateSeries($timeline, $date, 5); |
||
| 81 | } |
||
| 82 | $totalSize += $file->getSize(); |
||
| 83 | |||
| 84 | // Gain de points hipsteritude en fonction du nombre d'autres |
||
| 85 | // personnes qui ont téléchargé le fichier |
||
| 86 | $this->computeHipsteritude($hipster, $file->downloads() - 1); |
||
| 87 | } |
||
| 88 | |||
| 89 | // Dans le chart stacké, on met la date actuelle comme point de fin |
||
| 90 | $this->updateSeries($timeline, time()*1000, -1); |
||
| 91 | |||
| 92 | return [ |
||
| 93 | 'repartition' => $repartition, |
||
| 94 | 'timeline' => $timeline, |
||
| 95 | 'totalSize' => $totalSize, |
||
| 96 | 'totalFiles' => $totalFiles, |
||
| 97 | 'hipster' => (int)(10*$hipster/$totalFiles) |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 139 |