| Conditions | 15 |
| Paths | 24 |
| Total Lines | 59 |
| Code Lines | 31 |
| Lines | 10 |
| Ratio | 16.95 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 99 | public function buildWithSubQuery(VictoireQueryInterface $containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em) |
||
| 100 | { |
||
| 101 | //get the query of the container entity |
||
| 102 | $query = $containerEntity->getQuery(); |
||
| 103 | if (method_exists($containerEntity, 'additionnalQueryPart')) { |
||
| 104 | $query = $containerEntity->additionnalQueryPart(); |
||
|
|
|||
| 105 | } |
||
| 106 | |||
| 107 | if ($query !== '' && $query !== null) { |
||
| 108 | $subQuery = $em->createQueryBuilder() |
||
| 109 | ->select('item.id') |
||
| 110 | ->from($itemsQueryBuilder->getRootEntities()[0], 'item'); |
||
| 111 | |||
| 112 | $itemsQueryBuilder->andWhere( |
||
| 113 | sprintf('main_item.id IN (%s %s)', $subQuery->getQuery()->getDql(), $query) |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | //Add ORDER BY if set |
||
| 118 | if ($orderBy = json_decode($containerEntity->getOrderBy(), true)) { |
||
| 119 | foreach ($orderBy as $addOrderBy) { |
||
| 120 | $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]); |
||
| 121 | $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']); |
||
| 122 | |||
| 123 | //If ordering field is an association, treat it as a boolean |
||
| 124 | if ($this->isAssociationField($reflectionProperty)) { |
||
| 125 | $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder'); |
||
| 126 | $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']); |
||
| 127 | } else { |
||
| 128 | $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | $currentView = $this->currentView; |
||
| 134 | |||
| 135 | // If the current page is a BEP, we parse all its properties and inject them as query parameters |
||
| 136 | if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) { |
||
| 137 | |||
| 138 | // NEW |
||
| 139 | $metadatas = $em->getClassMetadata(get_class($currentEntity)); |
||
| 140 | View Code Duplication | foreach ($metadatas->fieldMappings as $fieldName => $field) { |
|
| 141 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 142 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | View Code Duplication | foreach ($metadatas->associationMappings as $fieldName => $field) { |
|
| 146 | if (strpos($query, ':'.$fieldName) !== false) { |
||
| 147 | $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId()); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if (strpos($query, ':currentEntity') !== false) { |
||
| 152 | $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId()); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $itemsQueryBuilder; |
||
| 157 | } |
||
| 158 | |||
| 178 |
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: