| Conditions | 9 |
| Paths | 16 |
| Total Lines | 56 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | 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 |
||
| 34 | public function index( |
||
| 35 | string $tag, |
||
| 36 | Request $request, |
||
| 37 | PaginatedFinderInterface $wanderFinder, |
||
| 38 | PaginatorInterface $paginator, |
||
| 39 | string $type = "any" |
||
| 40 | ): Response { |
||
| 41 | $boolQuery = new BoolQuery(); |
||
| 42 | if ($type === 'hand-tag' || $type === 'any') { |
||
| 43 | $boolQuery->addShould(new Term(['images.slugifiedTags' => ['value' => $tag]])); |
||
| 44 | } |
||
| 45 | if ($type === 'auto-tag' || $type === 'any') { |
||
| 46 | $boolQuery->addShould(new Term(['images.slugifiedAutoTags' => ['value' => $tag]])); |
||
| 47 | } |
||
| 48 | if ($type === 'text-tag' || $type === 'any') { |
||
| 49 | $boolQuery->addShould(new Term(['images.slugifiedTextTags' => ['value' => $tag]])); |
||
| 50 | } |
||
| 51 | |||
| 52 | $boolQuery->setMinimumShouldMatch(1); |
||
| 53 | |||
| 54 | $nested = new Nested(); |
||
| 55 | $nested->setPath('images'); |
||
| 56 | $nested->setQuery($boolQuery); |
||
| 57 | |||
| 58 | $innerHits = new InnerHits(); |
||
| 59 | // We want more than the default three inner hits, as there may be several related |
||
| 60 | // images on a particular wander, but we don't want to bump things up too high otherwise |
||
| 61 | // an overly-broad search will bring back way too many images even with pagination of |
||
| 62 | // the outer results. |
||
| 63 | $innerHits->setSize(10); |
||
| 64 | $nested->setInnerHits($innerHits); |
||
| 65 | |||
| 66 | $searchDescription = self::$translateParam[$type]; |
||
| 67 | |||
| 68 | $results = $wanderFinder->createHybridPaginatorAdapter($nested); |
||
| 69 | |||
| 70 | $perPage = 10; // TODO: Parameterise this results-per-page |
||
| 71 | $page = $request->query->getInt('page', 1); |
||
| 72 | |||
| 73 | // Avoid Elastica limit caused by malicious probing "Result window is too large, from + size must be less than or equal to: [10000]" |
||
| 74 | if (($page * $perPage > 10000) || $page < 1) { |
||
| 75 | $page = 1; |
||
| 76 | } |
||
| 77 | |||
| 78 | $pagination = $paginator->paginate( |
||
| 79 | $results, |
||
| 80 | $page, |
||
| 81 | $perPage |
||
| 82 | ); |
||
| 83 | |||
| 84 | return $this->render('tag/index.html.twig', [ |
||
| 85 | 'tag' => $tag, |
||
| 86 | // TODO: Take this back out; we'll only need to pass the pagination in the long run. I'm debugging. |
||
| 87 | 'results' => $results, |
||
| 88 | 'pagination' => $pagination, |
||
| 89 | 'search_description' => $searchDescription |
||
| 90 | ]); |
||
| 93 |