Conditions | 8 |
Paths | 15 |
Total Lines | 67 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
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 |
||
129 | private function buildQuery(array $identifiers, array $context, QueryNameGenerator $queryNameGenerator, QueryBuilder $previousQueryBuilder, string $previousAlias, int $remainingIdentifiers, QueryBuilder $topQueryBuilder = null): QueryBuilder |
||
130 | { |
||
131 | if ($remainingIdentifiers <= 0) { |
||
132 | return $previousQueryBuilder; |
||
133 | } |
||
134 | |||
135 | $topQueryBuilder = $topQueryBuilder ?? $previousQueryBuilder; |
||
136 | |||
137 | list($identifier, $identifierResourceClass) = $context['identifiers'][$remainingIdentifiers - 1]; |
||
138 | $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property']; |
||
139 | |||
140 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
141 | |||
142 | if (!$manager instanceof EntityManagerInterface) { |
||
143 | throw new RuntimeException("The manager for $identifierResourceClass must be an EntityManager."); |
||
144 | } |
||
145 | |||
146 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
147 | |||
148 | if (!$classMetadata instanceof ClassMetadataInfo) { |
||
149 | throw new RuntimeException( |
||
150 | "The class metadata for $identifierResourceClass must be an instance of ClassMetadataInfo." |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | $qb = $manager->createQueryBuilder(); |
||
155 | $alias = $queryNameGenerator->generateJoinAlias($identifier); |
||
156 | $relationType = $classMetadata->getAssociationMapping($previousAssociationProperty)['type']; |
||
157 | $normalizedIdentifiers = isset($identifiers[$identifier]) ? $this->normalizeIdentifiers( |
||
158 | $identifiers[$identifier], |
||
159 | $manager, |
||
160 | $identifierResourceClass |
||
161 | ) : []; |
||
162 | |||
163 | switch ($relationType) { |
||
164 | // MANY_TO_MANY relations need an explicit join so that the identifier part can be retrieved |
||
165 | case ClassMetadataInfo::MANY_TO_MANY: |
||
166 | $joinAlias = $queryNameGenerator->generateJoinAlias($previousAssociationProperty); |
||
167 | |||
168 | $qb->select($joinAlias) |
||
169 | ->from($identifierResourceClass, $alias) |
||
170 | ->innerJoin("$alias.$previousAssociationProperty", $joinAlias); |
||
171 | break; |
||
172 | case ClassMetadataInfo::ONE_TO_MANY: |
||
173 | $mappedBy = $classMetadata->getAssociationMapping($previousAssociationProperty)['mappedBy']; |
||
174 | $previousAlias = "$previousAlias.$mappedBy"; |
||
175 | |||
176 | $qb->select($alias) |
||
177 | ->from($identifierResourceClass, $alias); |
||
178 | break; |
||
179 | default: |
||
180 | $qb->select("IDENTITY($alias.$previousAssociationProperty)") |
||
181 | ->from($identifierResourceClass, $alias); |
||
182 | } |
||
183 | |||
184 | // Add where clause for identifiers |
||
185 | foreach ($normalizedIdentifiers as $key => $value) { |
||
186 | $placeholder = $queryNameGenerator->generateParameterName($key); |
||
187 | $qb->andWhere("$alias.$key = :$placeholder"); |
||
188 | $topQueryBuilder->setParameter($placeholder, $value); |
||
189 | } |
||
190 | |||
191 | // Recurse queries |
||
192 | $qb = $this->buildQuery($identifiers, $context, $queryNameGenerator, $qb, $alias, --$remainingIdentifiers, $topQueryBuilder); |
||
193 | |||
194 | return $previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); |
||
195 | } |
||
196 | } |
||
197 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.