Conditions | 18 |
Paths | 72 |
Total Lines | 67 |
Code Lines | 36 |
Lines | 10 |
Ratio | 14.93 % |
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 |
||
123 | public function buildWithSubQuery(VictoireQueryInterface $containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em) |
||
124 | { |
||
125 | //get the query of the container entity |
||
126 | $query = $containerEntity->getQuery(); |
||
127 | if (method_exists($containerEntity, 'additionnalQueryPart')) { |
||
128 | $query = $containerEntity->additionnalQueryPart(); |
||
|
|||
129 | } |
||
130 | |||
131 | if ($query !== '' && $query !== null) { |
||
132 | $subQuery = $em->createQueryBuilder() |
||
133 | ->select('item.id') |
||
134 | ->from($itemsQueryBuilder->getRootEntities()[0], 'item'); |
||
135 | |||
136 | $itemsQueryBuilder->andWhere( |
||
137 | sprintf('main_item.id IN (%s %s)', $subQuery->getQuery()->getDql(), $query) |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | //Add ORDER BY if set |
||
142 | if ($orderBy = json_decode($containerEntity->getOrderBy(), true)) { |
||
143 | foreach ($orderBy as $addOrderBy) { |
||
144 | $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]); |
||
145 | $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']); |
||
146 | |||
147 | //If ordering field is an association, treat it as a boolean |
||
148 | if ($this->isAssociationField($reflectionProperty)) { |
||
149 | $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder'); |
||
150 | $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']); |
||
151 | } else { |
||
152 | $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $currentView = $this->currentView; |
||
158 | |||
159 | // If the current page is a BEP, we parse all its properties and inject them as query parameters |
||
160 | if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) { |
||
161 | |||
162 | // NEW |
||
163 | $metadatas = $em->getClassMetadata(get_class($currentEntity)); |
||
164 | View Code Duplication | foreach ($metadatas->fieldMappings as $fieldName => $field) { |
|
165 | if (strpos($query, ':'.$fieldName) !== false) { |
||
166 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)); |
||
167 | } |
||
168 | } |
||
169 | View Code Duplication | foreach ($metadatas->associationMappings as $fieldName => $field) { |
|
170 | if (strpos($query, ':'.$fieldName) !== false) { |
||
171 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId()); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (strpos($query, ':currentEntity') !== false) { |
||
176 | $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId()); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | if (strpos($query, ':currentUser') !== false && is_object($this->getCurrentUser())) { |
||
181 | if (is_object($this->getCurrentUser())) { |
||
182 | $itemsQueryBuilder->setParameter('currentUser', $this->getCurrentUser()->getId()); |
||
183 | } else { |
||
184 | throw new AccessDeniedException(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | return $itemsQueryBuilder; |
||
189 | } |
||
190 | |||
218 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: