| Conditions | 15 |
| Paths | 384 |
| Total Lines | 67 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 93 | public function getPager(array $criteria, $page, $limit = 10, array $sort = array()) |
||
| 94 | { |
||
| 95 | if (!isset($criteria['mode'])) { |
||
| 96 | $criteria['mode'] = 'public'; |
||
| 97 | } |
||
| 98 | |||
| 99 | $parameters = array(); |
||
| 100 | $query = $this->getRepository() |
||
| 101 | ->createQueryBuilder('p') |
||
| 102 | ->select('p, t') |
||
| 103 | ->orderBy('p.publicationDateStart', 'DESC'); |
||
| 104 | |||
| 105 | if ($criteria['mode'] == 'admin') { |
||
| 106 | $query |
||
| 107 | ->leftJoin('p.tags', 't') |
||
| 108 | ->leftJoin('p.author', 'a') |
||
| 109 | ; |
||
| 110 | } else { |
||
| 111 | $query |
||
| 112 | ->leftJoin('p.tags', 't', Join::WITH, 't.enabled = true') |
||
| 113 | ->leftJoin('p.author', 'a', Join::WITH, 'a.enabled = true') |
||
| 114 | ; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (!isset($criteria['enabled']) && $criteria['mode'] == 'public') { |
||
| 118 | $criteria['enabled'] = true; |
||
| 119 | } |
||
| 120 | if (isset($criteria['enabled'])) { |
||
| 121 | $query->andWhere('p.enabled = :enabled'); |
||
| 122 | $parameters['enabled'] = $criteria['enabled']; |
||
| 123 | } |
||
| 124 | |||
| 125 | if (isset($criteria['date']) && isset($criteria['date']['query']) && isset($criteria['date']['params'])) { |
||
| 126 | $query->andWhere($criteria['date']['query']); |
||
| 127 | $parameters = array_merge($parameters, $criteria['date']['params']); |
||
| 128 | } |
||
| 129 | |||
| 130 | if (isset($criteria['tag'])) { |
||
| 131 | $query |
||
| 132 | ->leftJoin('p.tags', 't2') |
||
| 133 | ->andWhere('t2.slug LIKE :tag'); |
||
| 134 | $parameters['tag'] = (string) $criteria['tag']; |
||
| 135 | } |
||
| 136 | |||
| 137 | if (isset($criteria['author'])) { |
||
| 138 | if (!is_array($criteria['author']) && stristr($criteria['author'], 'NULL')) { |
||
| 139 | $query->andWhere('p.author IS '.$criteria['author']); |
||
| 140 | } else { |
||
| 141 | $query->andWhere(sprintf('p.author IN (%s)', implode((array) $criteria['author'], ','))); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | if (isset($criteria['collection']) && $criteria['collection'] instanceof CollectionInterface) { |
||
| 146 | $query->andWhere('p.collection = :collectionid'); |
||
| 147 | $parameters['collectionid'] = $criteria['collection']->getId(); |
||
| 148 | } |
||
| 149 | |||
| 150 | $query->setParameters($parameters); |
||
| 151 | |||
| 152 | $pager = new Pager(); |
||
| 153 | $pager->setMaxPerPage($limit); |
||
| 154 | $pager->setQuery(new ProxyQuery($query)); |
||
| 155 | $pager->setPage($page); |
||
| 156 | $pager->init(); |
||
| 157 | |||
| 158 | return $pager; |
||
| 159 | } |
||
| 160 | |||
| 198 |