| Conditions | 2 |
| Paths | 2 |
| Total Lines | 102 |
| Code Lines | 67 |
| 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 |
||
| 25 | public function index( |
||
| 26 | StatsService $statsService, |
||
| 27 | ChartBuilderInterface $chartBuilder |
||
| 28 | ): Response { |
||
| 29 | $wanderStats = $statsService->getWanderStats(); |
||
| 30 | $imageStats = $statsService->getImageStats(); |
||
| 31 | $imageLocationStats = $statsService->getImageLocationStats(); |
||
| 32 | |||
| 33 | $wanderDataSeries = |
||
| 34 | [ |
||
| 35 | [ |
||
| 36 | 'label' => 'Number of Wanders', |
||
| 37 | 'colour' => self::WANDER_NUMBER_COLOUR, |
||
| 38 | 'extractFunction' => fn($dp): int => $dp['numberOfWanders'], |
||
| 39 | ], |
||
| 40 | [ |
||
| 41 | 'label' => 'Distance Walked (km)', |
||
| 42 | 'colour' => self::WANDER_DISTANCE_COLOUR, |
||
| 43 | 'extractFunction' => fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2) |
||
| 44 | ] |
||
| 45 | ]; |
||
| 46 | |||
| 47 | $monthlyWanderChart = $chartBuilder |
||
| 48 | ->createChart(Chart::TYPE_BAR) |
||
| 49 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $wanderDataSeries)); |
||
| 50 | |||
| 51 | $yearlyWanderChart = $chartBuilder |
||
| 52 | ->createChart(Chart::TYPE_BAR) |
||
| 53 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $wanderDataSeries)); |
||
| 54 | |||
| 55 | $imageDataSeries = []; |
||
| 56 | for ($rating = 0; $rating <= 5; $rating++) { |
||
| 57 | $imageDataSeries[] = [ |
||
| 58 | 'label' => "Photos (Rated: $rating stars)", |
||
| 59 | 'colour' => self::IMAGES_COLOUR_STACK[$rating], |
||
| 60 | 'extractFunction' => fn($dp): int => $dp['numberOfImagesByRating'][$rating], |
||
| 61 | ]; |
||
| 62 | } |
||
| 63 | |||
| 64 | $monthlyImagesChart = $chartBuilder |
||
| 65 | ->createChart(Chart::TYPE_BAR) |
||
| 66 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $imageDataSeries)) |
||
| 67 | ->setOptions([ |
||
| 68 | 'scales' => [ |
||
| 69 | 'x' => [ |
||
| 70 | 'stacked' => true |
||
| 71 | ], |
||
| 72 | 'y' => [ |
||
| 73 | 'stacked' => true |
||
| 74 | ] |
||
| 75 | ] |
||
| 76 | ]); |
||
| 77 | |||
| 78 | $yearlyImagesChart = $chartBuilder |
||
| 79 | ->createChart(Chart::TYPE_BAR) |
||
| 80 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $imageDataSeries)) |
||
| 81 | ->setOptions([ |
||
| 82 | 'scales' => [ |
||
| 83 | 'x' => [ |
||
| 84 | 'stacked' => true |
||
| 85 | ], |
||
| 86 | 'y' => [ |
||
| 87 | 'stacked' => true |
||
| 88 | ] |
||
| 89 | ] |
||
| 90 | ]); |
||
| 91 | |||
| 92 | $locationsChart = $chartBuilder |
||
| 93 | ->createChart(Chart::TYPE_BAR) |
||
| 94 | ->setOptions([ |
||
| 95 | 'maintainAspectRatio' => false, |
||
| 96 | 'indexAxis' => 'y', |
||
| 97 | 'plugins' => [ |
||
| 98 | 'legend' => [ |
||
| 99 | 'display' => false |
||
| 100 | ] |
||
| 101 | ] |
||
| 102 | ]) |
||
| 103 | ->setData([ |
||
| 104 | 'labels' => array_keys($imageLocationStats), |
||
| 105 | 'datasets' => [ |
||
| 106 | [ |
||
| 107 | 'label' => 'Number of Photos', |
||
| 108 | 'backgroundColor' => RandomColor::many(count($imageLocationStats)), |
||
| 109 | 'borderColor' => 'black', |
||
| 110 | 'borderWidth' => 1, |
||
| 111 | 'borderRadius' => 5, |
||
| 112 | 'data' => array_values($imageLocationStats) |
||
| 113 | ] |
||
| 114 | ] |
||
| 115 | ]); |
||
| 116 | |||
| 117 | return $this->render('stats/index.html.twig', [ |
||
| 118 | 'controller_name' => 'StatsController', // TODO: Remove this boilerplate |
||
| 119 | 'imageStats' => $imageStats, |
||
| 120 | 'wanderStats' => $wanderStats, |
||
| 121 | 'imageLocationStats' => $imageLocationStats, |
||
| 122 | 'monthlyWanderChart' => $monthlyWanderChart, |
||
| 123 | 'yearlyWanderChart' => $yearlyWanderChart, |
||
| 124 | 'monthlyImagesChart' => $monthlyImagesChart, |
||
| 125 | 'yearlyImagesChart' => $yearlyImagesChart, |
||
| 126 | 'locationsChart' => $locationsChart |
||
| 127 | ]); |
||
| 155 |