Conditions | 20 |
Paths | 96 |
Total Lines | 69 |
Code Lines | 38 |
Lines | 10 |
Ratio | 14.49 % |
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 |
||
135 | public function buildWithSubQuery(VictoireQueryInterface $containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em) |
||
136 | { |
||
137 | //get the query of the container entity |
||
138 | $query = $containerEntity->getQuery(); |
||
139 | if (method_exists($containerEntity, 'additionnalQueryPart')) { |
||
140 | $query = $containerEntity->additionnalQueryPart(); |
||
141 | } |
||
142 | |||
143 | if ($query !== '' && $query !== null) { |
||
144 | $subQuery = $em->createQueryBuilder() |
||
145 | ->select('item.id') |
||
146 | ->from($itemsQueryBuilder->getRootEntities()[0], 'item'); |
||
147 | |||
148 | $itemsQueryBuilder->andWhere( |
||
149 | sprintf('main_item.id IN (%s %s)', $subQuery->getQuery()->getDql(), $query) |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | //Add ORDER BY if set |
||
154 | if ($orderBy = json_decode($containerEntity->getOrderBy(), true)) { |
||
155 | foreach ($orderBy as $addOrderBy) { |
||
156 | $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]); |
||
157 | $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']); |
||
158 | |||
159 | //If ordering field is an association, treat it as a boolean |
||
160 | if ($this->isAssociationField($reflectionProperty)) { |
||
161 | $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder'); |
||
162 | $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']); |
||
163 | } else { |
||
164 | $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | $currentView = $this->currentView; |
||
170 | |||
171 | // If the current page is a BEP, we parse all its properties and inject them as query parameters |
||
172 | if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) { |
||
173 | |||
174 | // NEW |
||
175 | $metadatas = $em->getClassMetadata(get_class($currentEntity)); |
||
176 | View Code Duplication | foreach ($metadatas->fieldMappings as $fieldName => $field) { |
|
177 | if (strpos($query, ':'.$fieldName) !== false) { |
||
178 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)); |
||
179 | } |
||
180 | } |
||
181 | View Code Duplication | foreach ($metadatas->associationMappings as $fieldName => $field) { |
|
182 | if (strpos($query, ':'.$fieldName) !== false) { |
||
183 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId()); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | if (strpos($query, ':currentEntity') !== false) { |
||
188 | $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId()); |
||
189 | } |
||
190 | } elseif ($currentView() instanceof BusinessTemplate && strpos($query, ':currentEntity') !== false) { |
||
191 | $itemsQueryBuilder->setParameter('currentEntity', $containerEntity->getEntity()->getId()); |
||
192 | } |
||
193 | |||
194 | if (strpos($query, ':currentUser') !== false && is_object($this->getCurrentUser())) { |
||
195 | if (is_object($this->getCurrentUser())) { |
||
196 | $itemsQueryBuilder->setParameter('currentUser', $this->getCurrentUser()->getId()); |
||
197 | } else { |
||
198 | throw new AccessDeniedException(); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return $itemsQueryBuilder; |
||
203 | } |
||
204 | |||
232 |