| 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 |
||
| 122 | public function buildWithSubQuery(VictoireQueryInterface $containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em) |
||
| 123 | { |
||
| 124 | //get the query of the container entity |
||
| 125 | $query = $containerEntity->getQuery(); |
||
| 126 | if (method_exists($containerEntity, 'additionnalQueryPart')) { |
||
| 127 | $query = $containerEntity->additionnalQueryPart(); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($query !== '' && $query !== null) { |
||
| 131 | $subQuery = $em->createQueryBuilder() |
||
| 132 | ->select('item.id') |
||
| 133 | ->from($itemsQueryBuilder->getRootEntities()[0], 'item'); |
||
| 134 | |||
| 135 | $itemsQueryBuilder->andWhere( |
||
| 136 | sprintf('main_item.id IN (%s %s)', $subQuery->getQuery()->getDql(), $query) |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | //Add ORDER BY if set |
||
| 141 | if ($orderBy = json_decode($containerEntity->getOrderBy(), true)) { |
||
| 142 | foreach ($orderBy as $addOrderBy) { |
||
| 143 | $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]); |
||
| 144 | $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']); |
||
| 145 | |||
| 146 | //If ordering field is an association, treat it as a boolean |
||
| 147 | if ($this->isAssociationField($reflectionProperty)) { |
||
| 148 | $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder'); |
||
| 149 | $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']); |
||
| 150 | } else { |
||
| 151 | $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $currentView = $this->currentView; |
||
| 157 | |||
| 158 | // If the current page is a BEP, we parse all its properties and inject them as query parameters |
||
| 159 | if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) { |
||
| 160 | |||
| 161 | // NEW |
||
| 162 | $metadatas = $em->getClassMetadata(get_class($currentEntity)); |
||
| 163 | View Code Duplication | foreach ($metadatas->fieldMappings as $fieldName => $field) { |
|
| 164 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 165 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | View Code Duplication | foreach ($metadatas->associationMappings as $fieldName => $field) { |
|
| 169 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 170 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId()); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if (strpos($query, ':currentEntity') !== false) { |
||
| 175 | $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId()); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if (strpos($query, ':currentUser') !== false && is_object($this->getCurrentUser())) { |
||
| 180 | if (is_object($this->getCurrentUser())) { |
||
| 181 | $itemsQueryBuilder->setParameter('currentUser', $this->getCurrentUser()->getId()); |
||
| 182 | } else { |
||
| 183 | throw new AccessDeniedException(); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $itemsQueryBuilder; |
||
| 188 | } |
||
| 189 | |||
| 217 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: