| Conditions | 21 |
| Paths | 2019 |
| Total Lines | 113 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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("The class metadata for $identifierResourceClass must be an instance of ClassMetadataInfo."); |
||
| 146 | } |
||
| 147 | |||
| 148 | $qb = $manager->createQueryBuilder(); |
||
| 149 | $alias = $queryNameGenerator->generateJoinAlias($identifier); |
||
| 150 | $normalizedIdentifiers = []; |
||
| 151 | |||
| 152 | if (isset($identifiers[$identifier])) { |
||
| 153 | // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated |
||
| 154 | if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) { |
||
| 155 | $normalizedIdentifiers = $identifiers[$identifier]; |
||
| 156 | } else { |
||
| 157 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($classMetadata->hasAssociation($previousAssociationProperty)) { |
||
| 162 | $relationType = $classMetadata->getAssociationMapping($previousAssociationProperty)['type']; |
||
| 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 | case ClassMetadataInfo::ONE_TO_ONE: |
||
| 180 | $association = $classMetadata->getAssociationMapping($previousAssociationProperty); |
||
| 181 | if (!isset($association['mappedBy'])) { |
||
| 182 | $qb->select("IDENTITY($alias.$previousAssociationProperty)") |
||
| 183 | ->from($identifierResourceClass, $alias); |
||
| 184 | break; |
||
| 185 | } |
||
| 186 | $mappedBy = $association['mappedBy']; |
||
| 187 | $previousAlias = "$previousAlias.$mappedBy"; |
||
| 188 | |||
| 189 | $qb->select($alias) |
||
| 190 | ->from($identifierResourceClass, $alias); |
||
| 191 | break; |
||
| 192 | default: |
||
| 193 | $qb->select("IDENTITY($alias.$previousAssociationProperty)") |
||
| 194 | ->from($identifierResourceClass, $alias); |
||
| 195 | } |
||
| 196 | } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) { |
||
| 197 | $qb->select($alias) |
||
| 198 | ->from($identifierResourceClass, $alias); |
||
| 199 | } |
||
| 200 | |||
| 201 | $lastIdentifer = 1 === $remainingIdentifiers; |
||
| 202 | $association = $classMetadata->hasAssociation($previousAssociationProperty) ? $classMetadata->getAssociationMapping($previousAssociationProperty) : []; |
||
| 203 | $optimizable = $lastIdentifer && ( |
||
| 204 | (isset($association['inversedBy']) && ClassMetadataInfo::ONE_TO_ONE === $association['type']) |
||
| 205 | || (isset($association['mappedBy']) && ClassMetadataInfo::ONE_TO_MANY === $association['type']) |
||
| 206 | ); |
||
| 207 | |||
| 208 | // Add where clause for identifiers |
||
| 209 | foreach ($normalizedIdentifiers as $key => $value) { |
||
| 210 | $placeholder = $queryNameGenerator->generateParameterName($key); |
||
| 211 | if ($optimizable) { |
||
| 212 | // Add where clause for identifiers, but not via a WHERE ... IN ( ...subquery... ). Instead we use |
||
| 213 | // a direct identifier equality clause, to speed thing up when dealing with large tables. |
||
| 214 | // We may do so as there is no more recursion levels from here. |
||
| 215 | |||
| 216 | if (ClassMetadataInfo::ONE_TO_ONE === $association['type']) { |
||
| 217 | $joinAlias = $queryNameGenerator->generateJoinAlias($association['inversedBy']); |
||
| 218 | |||
| 219 | $previousQueryBuilder->innerJoin("$previousAlias.{$association['inversedBy']}", $joinAlias) |
||
| 220 | ->andWhere("$joinAlias.$key = :$placeholder"); |
||
| 221 | } else { |
||
| 222 | $previousQueryBuilder->andWhere("IDENTITY($previousAlias) = :$placeholder"); |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | $qb->andWhere("$alias.$key = :$placeholder"); |
||
| 226 | } |
||
| 227 | $topQueryBuilder->setParameter($placeholder, $value, (string) $classMetadata->getTypeOfField($key)); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Recurse queries |
||
| 231 | $qb = $this->buildQuery($identifiers, $context, $queryNameGenerator, $qb, $alias, --$remainingIdentifiers, $topQueryBuilder); |
||
| 232 | |||
| 233 | if ($optimizable) { |
||
| 234 | return $previousQueryBuilder; |
||
| 235 | } |
||
| 236 | |||
| 237 | return $previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); |
||
| 238 | } |
||
| 240 |
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.