| Conditions | 7 |
| Paths | 32 |
| Total Lines | 68 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 53 | public function findFrontPages(array $slugs = [], $host = null, $locale = null) |
||
| 54 | { |
||
| 55 | $qb = $this->createQueryBuilder('page') |
||
| 56 | ->where('page.enabled = :enabled') |
||
| 57 | ->leftJoin('page.category', 'category') |
||
| 58 | ->andWhere('page.category is null OR category.enabled = :enabled') |
||
| 59 | ->setParameter('enabled', true) |
||
| 60 | ; |
||
| 61 | |||
| 62 | // Will search differently if we're looking for homepage. |
||
| 63 | $searchForHomepage = 0 === count($slugs); |
||
| 64 | |||
| 65 | if (true === $searchForHomepage) { |
||
| 66 | // If we are looking for homepage, let's get only the first one. |
||
| 67 | $qb |
||
| 68 | ->andWhere('page.homepage = :homepage') |
||
| 69 | ->setParameter('homepage', true) |
||
| 70 | ->setMaxResults(1) |
||
| 71 | ; |
||
| 72 | } else { |
||
| 73 | $qb |
||
| 74 | ->andWhere('page.slug IN ( :slugs )') |
||
| 75 | ->setParameter('slugs', $slugs) |
||
| 76 | ; |
||
| 77 | } |
||
| 78 | |||
| 79 | $hostWhere = 'page.host IS NULL'; |
||
| 80 | if (null !== $host) { |
||
| 81 | $hostWhere .= ' OR page.host = :host'; |
||
| 82 | $qb->setParameter('host', $host); |
||
| 83 | $qb->addOrderBy('page.host', 'asc'); |
||
| 84 | } |
||
| 85 | $qb->andWhere($hostWhere); |
||
| 86 | |||
| 87 | $localeWhere = 'page.locale IS NULL'; |
||
| 88 | if (null !== $locale) { |
||
| 89 | $localeWhere .= ' OR page.locale = :locale'; |
||
| 90 | $qb->setParameter('locale', $locale); |
||
| 91 | $qb->addOrderBy('page.locale', 'asc'); |
||
| 92 | } |
||
| 93 | $qb->andWhere($localeWhere); |
||
| 94 | |||
| 95 | // Then the last page will automatically be one that has both properties. |
||
| 96 | $qb |
||
| 97 | ->orderBy('page.host', 'asc') |
||
| 98 | ->addOrderBy('page.locale', 'asc') |
||
| 99 | ; |
||
| 100 | |||
| 101 | /** @var Page[] $results */ |
||
| 102 | $results = $qb->getQuery() |
||
| 103 | ->useResultCache($this->cacheEnabled, $this->cacheTtl) |
||
| 104 | ->getResult() |
||
| 105 | ; |
||
| 106 | |||
| 107 | // If we're looking for a homepage, only get the last result (matching more properties). |
||
| 108 | if (true === $searchForHomepage && count($results) > 0) { |
||
| 109 | reset($results); |
||
| 110 | $results = [$results[0]]; |
||
| 111 | } |
||
| 112 | |||
| 113 | $resultsSortedBySlug = []; |
||
| 114 | |||
| 115 | foreach ($results as $page) { |
||
| 116 | $resultsSortedBySlug[$page->getSlug()] = $page; |
||
| 117 | } |
||
| 118 | |||
| 119 | return $resultsSortedBySlug; |
||
| 120 | } |
||
| 121 | } |
||
| 122 |