Conditions | 14 |
Paths | 112 |
Total Lines | 74 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 3 | Features | 1 |
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 |
||
52 | public function findFrontPages(array $slugs = array(), $host = null, $locale = null) |
||
53 | { |
||
54 | $qb = $this->createQueryBuilder('page') |
||
55 | ->where('page.enabled = :enabled') |
||
56 | ->leftJoin('page.category', 'category') |
||
57 | ->andWhere('page.category is null OR category.enabled = :enabled') |
||
58 | ->setParameter('enabled', true) |
||
59 | ; |
||
60 | |||
61 | // Will search differently if we're looking for homepage. |
||
62 | $searchForHomepage = count($slugs) === 0; |
||
63 | |||
64 | if ($searchForHomepage) { |
||
65 | $qb |
||
66 | ->andWhere('page.homepage = :homepage') |
||
67 | ->setParameter('homepage', true) |
||
68 | ; |
||
69 | } else { |
||
70 | $qb |
||
71 | ->andWhere('page.slug IN ( :slugs )') |
||
72 | ->setParameter('slugs', $slugs) |
||
73 | ; |
||
74 | } |
||
75 | |||
76 | $hostWhere = 'page.host IS NULL'; |
||
77 | if (null !== $host) { |
||
78 | $hostWhere .= ' OR page.host = :host'; |
||
79 | $qb->setParameter('host', $host); |
||
80 | } |
||
81 | $qb->andWhere($hostWhere); |
||
82 | |||
83 | $localeWhere = 'page.locale IS NULL'; |
||
84 | if (null !== $locale) { |
||
85 | $localeWhere .= ' OR page.locale = :locale'; |
||
86 | $qb->setParameter('locale', $locale); |
||
87 | } |
||
88 | $qb->andWhere($localeWhere); |
||
89 | |||
90 | // This will allow getting first the pages that match both criteria |
||
91 | $qb |
||
92 | ->orderBy('page.host', 'DESC') |
||
93 | ->addOrderBy('page.locale', 'DESC') |
||
94 | ; |
||
95 | |||
96 | /** @var Page[] $results */ |
||
97 | $results = $qb->getQuery() |
||
98 | ->useResultCache($this->cacheEnabled, $this->cacheTtl) |
||
99 | ->getResult(); |
||
100 | |||
101 | if ($searchForHomepage) { |
||
102 | $homepage = null; |
||
103 | |||
104 | foreach ($results as $page) { |
||
105 | if ( |
||
106 | ($page->getLocale() && $page->getHost()) |
||
107 | || $page->getHost() || $page->getLocale() |
||
108 | || !$page->getLocale() || !$page->getHost() |
||
109 | ) { |
||
110 | $homepage = $page; |
||
111 | break; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $results = $homepage ? array($homepage) : array(); |
||
116 | } |
||
117 | |||
118 | $resultsSortedBySlug = array(); |
||
119 | |||
120 | foreach ($results as $page) { |
||
121 | $resultsSortedBySlug[$page->getSlug()] = $page; |
||
122 | } |
||
123 | |||
124 | return $resultsSortedBySlug; |
||
125 | } |
||
126 | } |
||
127 |