| Conditions | 11 |
| Paths | 256 |
| Total Lines | 84 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 47 | public function index( |
||
| 48 | Request $request, |
||
| 49 | ImageRepository $imageRepository, |
||
| 50 | PaginatorInterface $paginator |
||
| 51 | ): Response { |
||
| 52 | /** @var ImageFilterData $filterData */ |
||
| 53 | |||
| 54 | // Sensible defaults |
||
| 55 | $filterData = new ImageFilterData(); |
||
| 56 | $filterData->setStartDate($imageRepository->getEarliestImageCaptureDate()); |
||
| 57 | $filterData->setEndDate($imageRepository->getLatestImageCaptureDate()); |
||
| 58 | $filterData->setRatingComparison('eq'); |
||
| 59 | $locationChoices = $this->getLocationChoices($imageRepository); |
||
| 60 | |||
| 61 | // These are overrides that can be sent by links set up on our |
||
| 62 | // charts on the Statistics page. If we get a location, star |
||
| 63 | // rating or start & end dates via those, we override our defaults. |
||
| 64 | $filterData->setLocation((string) $request->query->get('location')); |
||
| 65 | if ($request->query->has('rating')) { |
||
| 66 | $filterData->setRating($request->query->getInt('rating')); |
||
| 67 | } |
||
| 68 | if ($request->query->has('periodStartDate')) { |
||
| 69 | $filterData->setStartDateFromUrlParam((string) $request->query->get('periodStartDate')); |
||
| 70 | } |
||
| 71 | if ($request->query->has('periodEndDate')) { |
||
| 72 | $filterData->setEndDateFromUrlParam((string) $request->query->get('periodEndDate')); |
||
| 73 | } |
||
| 74 | |||
| 75 | // Filtering form for the top of the page |
||
| 76 | $filterForm = $this->createForm( |
||
| 77 | ImageFilterType::class, |
||
| 78 | $filterData, |
||
| 79 | [ |
||
| 80 | 'locations' => array_combine($locationChoices, array_values($locationChoices)), |
||
| 81 | 'csrf_protection' => false // We're just a GET request, and nothing bad happens no matter what you do. |
||
| 82 | ] |
||
| 83 | ); |
||
| 84 | |||
| 85 | $filterForm->handleRequest($request); |
||
| 86 | if ($filterForm->isSubmitted() && $filterForm->isValid()) { |
||
| 87 | $filterData = $filterForm->getData(); |
||
| 88 | }; |
||
| 89 | |||
| 90 | $qb = $imageRepository->getReversePaginatorQueryBuilder(); |
||
| 91 | |||
| 92 | if ($filterData->hasRating() && $filterData->hasRatingComparison()) { |
||
| 93 | $this->filterQueryByRating( |
||
| 94 | $filterData->getRating(), |
||
| 95 | $filterData->getRatingComparison(), |
||
| 96 | $qb |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($filterData->hasStartDate()) { |
||
| 101 | $qb |
||
| 102 | ->andWhere('i.capturedAt >= :startDate') |
||
| 103 | ->setParameter('startDate', $filterData->getStartDate()); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($filterData->hasEndDate()) { |
||
| 107 | $endDate = (new CarbonImmutable($filterData->getEndDate()))->addDays(1); |
||
| 108 | $qb |
||
| 109 | ->andWhere('i.capturedAt < :endDate') |
||
| 110 | ->setParameter('endDate', $endDate); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($filterData->hasLocation()) { |
||
| 114 | $qb |
||
| 115 | ->andWhere('i.location = :location') |
||
| 116 | ->setParameter('location', $filterData->getLocation()); |
||
| 117 | } |
||
| 118 | |||
| 119 | $query = $qb->getQuery(); |
||
| 120 | |||
| 121 | $page = $request->query->getInt('page', 1); |
||
| 122 | $pagination = $paginator->paginate( |
||
| 123 | $query, |
||
| 124 | $page, |
||
| 125 | 20 |
||
| 126 | ); |
||
| 127 | |||
| 128 | return $this->render('image/index.html.twig', [ |
||
| 129 | 'image_pagination' => $pagination, |
||
| 130 | 'filter_form' => $filterForm->createView() |
||
| 131 | ]); |
||
| 162 |