Conditions | 13 |
Paths | 45 |
Total Lines | 89 |
Code Lines | 58 |
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 |
||
125 | private function buildQuery(array $identifiers, array $context, QueryNameGenerator $queryNameGenerator, QueryBuilder $previousQueryBuilder, string $previousAlias, int $remainingIdentifiers, QueryBuilder $topQueryBuilder = null): QueryBuilder |
||
126 | { |
||
127 | if ($remainingIdentifiers <= 0) { |
||
128 | return $previousQueryBuilder; |
||
129 | } |
||
130 | |||
131 | $topQueryBuilder = $topQueryBuilder ?? $previousQueryBuilder; |
||
132 | |||
133 | [$identifier, $identifierResourceClass] = $context['identifiers'][$remainingIdentifiers - 1]; |
||
134 | $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property']; |
||
135 | |||
136 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
137 | |||
138 | if (!$manager instanceof EntityManagerInterface) { |
||
139 | throw new RuntimeException("The manager for $identifierResourceClass must be an EntityManager."); |
||
140 | } |
||
141 | |||
142 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
143 | |||
144 | if (!$classMetadata instanceof ClassMetadataInfo) { |
||
145 | throw new RuntimeException( |
||
146 | "The class metadata for $identifierResourceClass must be an instance of ClassMetadataInfo." |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | $qb = $manager->createQueryBuilder(); |
||
151 | $alias = $queryNameGenerator->generateJoinAlias($identifier); |
||
152 | $normalizedIdentifiers = []; |
||
153 | |||
154 | if (isset($identifiers[$identifier])) { |
||
155 | // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated |
||
156 | if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) { |
||
157 | $normalizedIdentifiers = $identifiers[$identifier]; |
||
158 | } else { |
||
159 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | if ($classMetadata->hasAssociation($previousAssociationProperty)) { |
||
164 | $relationType = $classMetadata->getAssociationMapping($previousAssociationProperty)['type']; |
||
165 | switch ($relationType) { |
||
166 | // MANY_TO_MANY relations need an explicit join so that the identifier part can be retrieved |
||
167 | case ClassMetadataInfo::MANY_TO_MANY: |
||
168 | $joinAlias = $queryNameGenerator->generateJoinAlias($previousAssociationProperty); |
||
169 | |||
170 | $qb->select($joinAlias) |
||
171 | ->from($identifierResourceClass, $alias) |
||
172 | ->innerJoin("$alias.$previousAssociationProperty", $joinAlias); |
||
173 | break; |
||
174 | case ClassMetadataInfo::ONE_TO_MANY: |
||
175 | $mappedBy = $classMetadata->getAssociationMapping($previousAssociationProperty)['mappedBy']; |
||
176 | $previousAlias = "$previousAlias.$mappedBy"; |
||
177 | |||
178 | $qb->select($alias) |
||
179 | ->from($identifierResourceClass, $alias); |
||
180 | break; |
||
181 | case ClassMetadataInfo::ONE_TO_ONE: |
||
182 | $association = $classMetadata->getAssociationMapping($previousAssociationProperty); |
||
183 | if (!isset($association['mappedBy'])) { |
||
184 | $qb->select("IDENTITY($alias.$previousAssociationProperty)") |
||
185 | ->from($identifierResourceClass, $alias); |
||
186 | break; |
||
187 | } |
||
188 | $mappedBy = $association['mappedBy']; |
||
189 | $previousAlias = "$previousAlias.$mappedBy"; |
||
190 | |||
191 | $qb->select($alias) |
||
192 | ->from($identifierResourceClass, $alias); |
||
193 | break; |
||
194 | default: |
||
195 | $qb->select("IDENTITY($alias.$previousAssociationProperty)") |
||
196 | ->from($identifierResourceClass, $alias); |
||
197 | } |
||
198 | } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) { |
||
199 | $qb->select($alias) |
||
200 | ->from($identifierResourceClass, $alias); |
||
201 | } |
||
202 | |||
203 | // Add where clause for identifiers |
||
204 | foreach ($normalizedIdentifiers as $key => $value) { |
||
205 | $placeholder = $queryNameGenerator->generateParameterName($key); |
||
206 | $qb->andWhere("$alias.$key = :$placeholder"); |
||
207 | $topQueryBuilder->setParameter($placeholder, $value, (string) $classMetadata->getTypeOfField($key)); |
||
208 | } |
||
209 | |||
210 | // Recurse queries |
||
211 | $qb = $this->buildQuery($identifiers, $context, $queryNameGenerator, $qb, $alias, --$remainingIdentifiers, $topQueryBuilder); |
||
212 | |||
213 | return $previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); |
||
214 | } |
||
216 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.