Conditions | 33 |
Paths | 1193 |
Total Lines | 108 |
Code Lines | 61 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
126 | private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, bool $forceEager, bool $fetchPartial, string $parentAlias, array $options = [], array $normalizationContext = [], bool $wasLeftJoin = false, int &$joinCount = 0, int $currentDepth = null) |
||
127 | { |
||
128 | if ($joinCount > $this->maxJoins) { |
||
129 | throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary, or use the "max_depth" option of the Symfony serializer.'); |
||
130 | } |
||
131 | |||
132 | $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth; |
||
133 | $entityManager = $queryBuilder->getEntityManager(); |
||
134 | $classMetadata = $entityManager->getClassMetadata($resourceClass); |
||
135 | $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($resourceClass)->getAttributesMetadata() : null; |
||
136 | |||
137 | if (!empty($normalizationContext[AbstractNormalizer::GROUPS])) { |
||
138 | $options['serializer_groups'] = $normalizationContext[AbstractNormalizer::GROUPS]; |
||
139 | } |
||
140 | |||
141 | foreach ($classMetadata->associationMappings as $association => $mapping) { |
||
142 | //Don't join if max depth is enabled and the current depth limit is reached |
||
143 | if (0 === $currentDepth && ($normalizationContext[AbstractObjectNormalizer::ENABLE_MAX_DEPTH] ?? false)) { |
||
144 | continue; |
||
145 | } |
||
146 | |||
147 | try { |
||
148 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $options); |
||
149 | } catch (PropertyNotFoundException $propertyNotFoundException) { |
||
150 | //skip properties not found |
||
151 | continue; |
||
152 | } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { |
||
153 | //skip associations that are not resource classes |
||
154 | continue; |
||
155 | } |
||
156 | |||
157 | if ( |
||
158 | // Always skip extra lazy associations |
||
159 | ClassMetadataInfo::FETCH_EXTRA_LAZY === $mapping['fetch'] || |
||
160 | // We don't want to interfere with doctrine on this association |
||
161 | (false === $forceEager && ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch']) |
||
162 | ) { |
||
163 | continue; |
||
164 | } |
||
165 | |||
166 | if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) { |
||
167 | if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) { |
||
168 | // prepare the child context |
||
169 | $normalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association]; |
||
170 | } else { |
||
171 | unset($normalizationContext[AbstractNormalizer::ATTRIBUTES]); |
||
172 | } |
||
173 | } else { |
||
174 | $inAttributes = null; |
||
175 | } |
||
176 | |||
177 | if ( |
||
178 | (null === $fetchEager = $propertyMetadata->getAttribute('fetch_eager')) && |
||
179 | (null !== $fetchEager = $propertyMetadata->getAttribute('fetchEager')) |
||
180 | ) { |
||
181 | @trigger_error('The "fetchEager" attribute is deprecated since 2.3. Please use "fetch_eager" instead.', E_USER_DEPRECATED); |
||
182 | } |
||
183 | |||
184 | if (false === $fetchEager) { |
||
185 | continue; |
||
186 | } |
||
187 | |||
188 | $isNotReadableLink = false === $propertyMetadata->isReadableLink(); |
||
189 | if (null === $fetchEager && (false === $propertyMetadata->isReadable() || ((null === $inAttributes && $isNotReadableLink) || (false === $inAttributes)))) { |
||
190 | continue; |
||
191 | } |
||
192 | |||
193 | $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true; |
||
194 | if (false !== $wasLeftJoin || true === $isNullable) { |
||
195 | $method = 'leftJoin'; |
||
196 | } else { |
||
197 | $method = 'innerJoin'; |
||
198 | } |
||
199 | |||
200 | $associationAlias = $queryNameGenerator->generateJoinAlias($association); |
||
201 | $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias); |
||
202 | ++$joinCount; |
||
203 | |||
204 | if (true === $fetchPartial) { |
||
205 | try { |
||
206 | $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $options); |
||
207 | } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { |
||
208 | continue; |
||
209 | } |
||
210 | } else { |
||
211 | $queryBuilder->addSelect($associationAlias); |
||
212 | } |
||
213 | |||
214 | // Avoid recursive joins |
||
215 | if ($mapping['targetEntity'] === $resourceClass) { |
||
216 | // Avoid joining the same association twice (see #1959) |
||
217 | if (!\in_array($associationAlias, $queryBuilder->getAllAliases(), true)) { |
||
218 | $queryBuilder->addSelect($associationAlias); |
||
219 | } |
||
220 | |||
221 | continue; |
||
222 | } |
||
223 | |||
224 | if (isset($attributesMetadata[$association])) { |
||
225 | $maxDepth = $attributesMetadata[$association]->getMaxDepth(); |
||
226 | |||
227 | // The current depth is the lowest max depth available in the ancestor tree. |
||
228 | if (null !== $maxDepth && (null === $currentDepth || $maxDepth < $currentDepth)) { |
||
229 | $currentDepth = $maxDepth; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $options, $normalizationContext, 'leftJoin' === $method, $joinCount, $currentDepth); |
||
234 | } |
||
303 |