| Conditions | 13 |
| Paths | 31 |
| Total Lines | 81 |
| Code Lines | 48 |
| Lines | 7 |
| Ratio | 8.64 % |
| 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 |
||
| 57 | public function getSubcollection(string $resourceClass, array $identifiers, array $context, string $operationName = null) |
||
| 58 | { |
||
| 59 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
| 60 | if (null === $manager) { |
||
| 61 | throw new ResourceClassNotSupportedException(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $repository = $manager->getRepository($resourceClass); |
||
| 65 | if (!method_exists($repository, 'createQueryBuilder')) { |
||
| 66 | throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!isset($context['identifiers']) || !isset($context['property'])) { |
||
| 70 | throw new ResourceClassNotSupportedException('The given resource class is not a subcollection.'); |
||
| 71 | } |
||
| 72 | |||
| 73 | $queryBuilder = $repository->createQueryBuilder('o'); |
||
| 74 | $queryNameGenerator = new QueryNameGenerator(); |
||
| 75 | $previousQueryBuilder = null; |
||
| 76 | $previousAlias = null; |
||
| 77 | |||
| 78 | $num = count($context['identifiers']); |
||
| 79 | |||
| 80 | while ($num--) { |
||
| 81 | list($identifier, $identifierResourceClass) = $context['identifiers'][$num]; |
||
| 82 | $previousIdentifier = $context['identifiers'][$num + 1][0] ?? $context['property']; |
||
| 83 | |||
| 84 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
| 85 | |||
| 86 | if (!($manager instanceof EntityManagerInterface)) { |
||
| 87 | throw new RuntimeException("The manager for $identifierResourceClass must be an EntityManager."); |
||
| 88 | } |
||
| 89 | |||
| 90 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
| 91 | |||
| 92 | $qb = $manager->createQueryBuilder(); |
||
| 93 | $alias = $queryNameGenerator->generateJoinAlias($identifier); |
||
| 94 | |||
| 95 | //MANY_TO_MANY relations needs an explicit join so that the identifier part can be retrieved |
||
| 96 | if ($classMetadata->getAssociationMapping($previousIdentifier)['type'] === ClassMetadataInfo::MANY_TO_MANY) { |
||
| 97 | $joinAlias = $queryNameGenerator->generateJoinAlias($previousIdentifier); |
||
| 98 | |||
| 99 | $qb->select($joinAlias) |
||
| 100 | ->from($identifierResourceClass, $alias) |
||
| 101 | ->innerJoin("$alias.$previousIdentifier", $joinAlias); |
||
| 102 | } else { |
||
| 103 | $qb->select("IDENTITY($alias.$previousIdentifier)") |
||
| 104 | ->from($identifierResourceClass, $alias); |
||
| 105 | } |
||
| 106 | |||
| 107 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
| 108 | |||
| 109 | foreach ($normalizedIdentifiers as $key => $value) { |
||
| 110 | $placeholder = $queryNameGenerator->generateParameterName($key); |
||
| 111 | $qb->andWhere("$alias.$key = :$placeholder"); |
||
| 112 | $queryBuilder->setParameter($placeholder, $value); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (null === $previousQueryBuilder) { |
||
| 116 | $previousQueryBuilder = $qb; |
||
| 117 | } else { |
||
| 118 | $previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); |
||
| 119 | } |
||
| 120 | |||
| 121 | $previousAlias = $alias; |
||
| 122 | } |
||
| 123 | |||
| 124 | $queryBuilder->where( |
||
| 125 | $queryBuilder->expr()->in('o', $previousQueryBuilder->getDQL()) |
||
| 126 | ); |
||
| 127 | |||
| 128 | View Code Duplication | foreach ($this->collectionExtensions as $extension) { |
|
| 129 | $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName); |
||
| 130 | |||
| 131 | if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) { |
||
| 132 | return $extension->getResult($queryBuilder); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return $queryBuilder->getQuery()->getResult(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.