| Conditions | 1 |
| Paths | 1 |
| Total Lines | 54 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 20 | public function index( |
||
| 21 | StatsService $statsService, |
||
| 22 | ChartBuilderInterface $chartBuilder |
||
| 23 | ): Response { |
||
| 24 | $wanderStats = $statsService->getWanderStats(); |
||
| 25 | $imageStats = $statsService->getImageStats(); |
||
| 26 | |||
| 27 | $wanderDataSeries = |
||
| 28 | [ |
||
| 29 | [ |
||
| 30 | 'label' => 'Number of Wanders', |
||
| 31 | 'colour' => self::WANDER_NUMBER_COLOUR, |
||
| 32 | 'extractFunction' => fn($dp): int => $dp['numberOfWanders'], |
||
| 33 | ], |
||
| 34 | [ |
||
| 35 | 'label' => 'Distance Walked (km)', |
||
| 36 | 'colour' => self::WANDER_DISTANCE_COLOUR, |
||
| 37 | 'extractFunction' => fn($dp): string => number_format($dp['totalDistance'] / 1000.0, 2) |
||
| 38 | ] |
||
| 39 | ]; |
||
| 40 | |||
| 41 | $monthlyWanderChart = $chartBuilder |
||
| 42 | ->createChart(Chart::TYPE_BAR) |
||
| 43 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $wanderDataSeries)); |
||
| 44 | |||
| 45 | $yearlyWanderChart = $chartBuilder |
||
| 46 | ->createChart(Chart::TYPE_BAR) |
||
| 47 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $wanderDataSeries)); |
||
| 48 | |||
| 49 | $imageDataSeries = |
||
| 50 | [ |
||
| 51 | [ |
||
| 52 | 'label' => 'Photos Taken', |
||
| 53 | 'colour' => self::IMAGES_NUMBER_COLOUR, |
||
| 54 | 'extractFunction' => fn($dp): int => $dp['numberOfImages'], |
||
| 55 | ] |
||
| 56 | ]; |
||
| 57 | |||
| 58 | $monthlyImagesChart = $chartBuilder |
||
| 59 | ->createChart(Chart::TYPE_BAR) |
||
| 60 | ->setData($this->generatePeriodicChartData($wanderStats['monthlyStats'], $imageDataSeries)); |
||
| 61 | |||
| 62 | $yearlyImagesChart = $chartBuilder |
||
| 63 | ->createChart(Chart::TYPE_BAR) |
||
| 64 | ->setData($this->generatePeriodicChartData($wanderStats['yearlyStats'], $imageDataSeries)); |
||
| 65 | |||
| 66 | return $this->render('stats/index.html.twig', [ |
||
| 67 | 'controller_name' => 'StatsController', // TODO: Remove this boilerplate |
||
| 68 | 'imageStats' => $imageStats, |
||
| 69 | 'wanderStats' => $wanderStats, |
||
| 70 | 'monthlyWanderChart' => $monthlyWanderChart, |
||
| 71 | 'yearlyWanderChart' => $yearlyWanderChart, |
||
| 72 | 'monthlyImagesChart' => $monthlyImagesChart, |
||
| 73 | 'yearlyImagesChart' => $yearlyImagesChart |
||
| 74 | ]); |
||
| 102 |