| Conditions | 2 |
| Paths | 2 |
| Total Lines | 103 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 41 | public function index( |
||
| 42 | StatsService $statsService, |
||
| 43 | ChartBuilderInterface $chartBuilder |
||
| 44 | ): Response { |
||
| 45 | $wanderStats = $statsService->getWanderStats(); |
||
| 46 | $imageStats = $statsService->getImageStats(); |
||
| 47 | $imageLocationStats = $statsService->getImageLocationStats(); |
||
| 48 | |||
| 49 | $wanderDataSeries = |
||
| 50 | [ |
||
| 51 | [ |
||
| 52 | 'label' => 'Number of Wanders', |
||
| 53 | 'colour' => self::WANDER_NUMBER_COLOUR, |
||
| 54 | 'extractFunction' => fn($dp): int => $dp['numberOfWanders'], |
||
| 55 | ], |
||
| 56 | [ |
||
| 57 | 'label' => 'Distance Walked (km)', |
||
| 58 | 'colour' => self::WANDER_DISTANCE_COLOUR, |
||
| 59 | 'extractFunction' => fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2) |
||
| 60 | ] |
||
| 61 | ]; |
||
| 62 | |||
| 63 | $monthlyWanderChart = $chartBuilder |
||
| 64 | ->createChart(Chart::TYPE_BAR) |
||
| 65 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $wanderDataSeries)); |
||
| 66 | |||
| 67 | $yearlyWanderChart = $chartBuilder |
||
| 68 | ->createChart(Chart::TYPE_BAR) |
||
| 69 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $wanderDataSeries)); |
||
| 70 | |||
| 71 | $imageDataSeries = []; |
||
| 72 | for ($rating = 0; $rating <= 5; $rating++) { |
||
| 73 | $imageDataSeries[] = [ |
||
| 74 | 'label' => "Photos (Rated: $rating stars)", |
||
| 75 | 'colour' => self::IMAGES_COLOUR_STACK[$rating], |
||
| 76 | 'extractFunction' => fn($dp): int => $dp['numberOfImagesByRating'][$rating], |
||
| 77 | 'rating' => $rating |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | |||
| 81 | $monthlyImagesChart = $chartBuilder |
||
| 82 | ->createChart(Chart::TYPE_BAR) |
||
| 83 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $imageDataSeries)) |
||
| 84 | ->setOptions([ |
||
| 85 | 'scales' => [ |
||
| 86 | 'x' => [ |
||
| 87 | 'stacked' => true |
||
| 88 | ], |
||
| 89 | 'y' => [ |
||
| 90 | 'stacked' => true |
||
| 91 | ] |
||
| 92 | ] |
||
| 93 | ]); |
||
| 94 | |||
| 95 | $yearlyImagesChart = $chartBuilder |
||
| 96 | ->createChart(Chart::TYPE_BAR) |
||
| 97 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $imageDataSeries)) |
||
| 98 | ->setOptions([ |
||
| 99 | 'scales' => [ |
||
| 100 | 'x' => [ |
||
| 101 | 'stacked' => true |
||
| 102 | ], |
||
| 103 | 'y' => [ |
||
| 104 | 'stacked' => true |
||
| 105 | ] |
||
| 106 | ] |
||
| 107 | ]); |
||
| 108 | |||
| 109 | $locationsChart = $chartBuilder |
||
| 110 | ->createChart(Chart::TYPE_BAR) |
||
| 111 | ->setOptions([ |
||
| 112 | 'maintainAspectRatio' => false, |
||
| 113 | 'indexAxis' => 'y', |
||
| 114 | 'plugins' => [ |
||
| 115 | 'legend' => [ |
||
| 116 | 'display' => false |
||
| 117 | ] |
||
| 118 | ] |
||
| 119 | ]) |
||
| 120 | ->setData([ |
||
| 121 | 'labels' => array_keys($imageLocationStats), |
||
| 122 | 'urls' => array_map(fn($l): string => $this->router->generate('image_index', ['location' => $l]), array_keys($imageLocationStats)), |
||
| 123 | 'datasets' => [ |
||
| 124 | [ |
||
| 125 | 'label' => 'Number of Photos', |
||
| 126 | 'backgroundColor' => RandomColor::many(count($imageLocationStats)), |
||
| 127 | 'borderColor' => 'black', |
||
| 128 | 'borderWidth' => 1, |
||
| 129 | 'borderRadius' => 5, |
||
| 130 | 'data' => array_values($imageLocationStats), |
||
| 131 | ] |
||
| 132 | ] |
||
| 133 | ]); |
||
| 134 | |||
| 135 | return $this->render('stats/index.html.twig', [ |
||
| 136 | 'imageStats' => $imageStats, |
||
| 137 | 'wanderStats' => $wanderStats, |
||
| 138 | 'imageLocationStats' => $imageLocationStats, |
||
| 139 | 'monthlyWanderChart' => $monthlyWanderChart, |
||
| 140 | 'yearlyWanderChart' => $yearlyWanderChart, |
||
| 141 | 'monthlyImagesChart' => $monthlyImagesChart, |
||
| 142 | 'yearlyImagesChart' => $yearlyImagesChart, |
||
| 143 | 'locationsChart' => $locationsChart |
||
| 144 | ]); |
||
| 184 |