| Conditions | 4 |
| Paths | 10 |
| Total Lines | 66 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 49 | public function index( |
||
| 50 | Request $request, |
||
| 51 | ImageRepository $imageRepository, |
||
| 52 | PaginatorInterface $paginator, |
||
| 53 | LoggerInterface $logger |
||
| 54 | ): Response { |
||
| 55 | /** @var ImageFilterData $filterData */ |
||
| 56 | $filterData = new ImageFilterData( |
||
| 57 | $imageRepository->getEarliestImageCaptureDate(), |
||
| 58 | $imageRepository->getLatestImageCaptureDate(), |
||
| 59 | ); |
||
| 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 | try { |
||
| 65 | $filterData->overrideLocationFromUrlParam((string) $request->query->get('location')); |
||
| 66 | $filterData->overrideRatingFromUrlParam((string) $request->query->get('rating')); |
||
| 67 | $filterData->overrideStartDateFromUrlParam((string) $request->query->get('periodStartDate')); |
||
| 68 | $filterData->overrideEndDateFromUrlParam((string) $request->query->get('periodEndDate')); |
||
| 69 | } catch (Exception $e) { |
||
| 70 | // Someone may be trying to fiddle with our URL parameters. Don't fail; the override |
||
| 71 | // functions are sensible enough to ignore invalid inputs. But we should log. |
||
| 72 | $logger->error( |
||
| 73 | 'Image controller override parameters caused an exception to be thrown. Quietly ignoring them.', |
||
| 74 | [ |
||
| 75 | 'exception_message' => $e->getMessage() |
||
| 76 | ] |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | // Filtering form for the top of the page |
||
| 81 | $locationChoices = $this->getLocationChoices($imageRepository); |
||
| 82 | $filterForm = $this->createForm( |
||
| 83 | ImageFilterType::class, |
||
| 84 | $filterData, |
||
| 85 | [ |
||
| 86 | 'locations' => array_combine($locationChoices, array_values($locationChoices)), |
||
| 87 | 'csrf_protection' => false, // We're just a GET request, and nothing bad happens no matter what you do. |
||
| 88 | 'attr' => [ |
||
| 89 | 'data-controller' => 'imagefilter' |
||
| 90 | ] |
||
| 91 | ] |
||
| 92 | ); |
||
| 93 | |||
| 94 | $filterForm->handleRequest($request); |
||
| 95 | if ($filterForm->isSubmitted() && $filterForm->isValid()) { |
||
| 96 | $filterData = $filterForm->getData(); |
||
| 97 | }; |
||
| 98 | |||
| 99 | $qb = $imageRepository->getReversePaginatorQueryBuilder(); |
||
| 100 | |||
| 101 | $this->filterQuery($filterData, $qb); |
||
| 102 | |||
| 103 | $query = $qb->getQuery(); |
||
| 104 | |||
| 105 | $page = $request->query->getInt('page', 1); |
||
| 106 | $pagination = $paginator->paginate( |
||
| 107 | $query, |
||
| 108 | $page, |
||
| 109 | 20 |
||
| 110 | ); |
||
| 111 | |||
| 112 | return $this->render('image/index.html.twig', [ |
||
| 113 | 'image_pagination' => $pagination, |
||
| 114 | 'filter_form' => $filterForm->createView() |
||
| 115 | ]); |
||
| 177 |