| Conditions | 11 |
| Paths | 41 |
| Total Lines | 73 |
| Code Lines | 44 |
| Lines | 7 |
| Ratio | 9.59 % |
| 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 |
||
| 56 | public function getSubcollection(string $resourceClass, array $identifiers, array $context, string $operationName) |
||
| 57 | { |
||
| 58 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
| 59 | if (null === $manager) { |
||
| 60 | throw new ResourceClassNotSupportedException(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $repository = $manager->getRepository($resourceClass); |
||
| 64 | if (!method_exists($repository, 'createQueryBuilder')) { |
||
| 65 | throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); |
||
| 66 | } |
||
| 67 | |||
| 68 | $queryBuilder = $repository->createQueryBuilder('o'); |
||
| 69 | $queryNameGenerator = new QueryNameGenerator(); |
||
| 70 | $previousQueryBuilder = null; |
||
| 71 | $previousAlias = null; |
||
| 72 | |||
| 73 | $num = count($context['identifiers']); |
||
| 74 | |||
| 75 | while ($num--) { |
||
| 76 | list($identifier, $identifierResourceClass) = $context['identifiers'][$num]; |
||
| 77 | $previousIdentifier = $context['identifiers'][$num + 1][0] ?? $context['property']; |
||
| 78 | |||
| 79 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
| 80 | |||
| 81 | $qb = $manager->createQueryBuilder(); |
||
| 82 | $alias = $queryNameGenerator->generateJoinAlias($identifier); |
||
| 83 | |||
| 84 | if (null !== $previousIdentifier) { |
||
| 85 | //MANY_TO_MANY relations needs an explicit join so that the identifier part can be retrieved |
||
| 86 | if ($classMetadata->getAssociationMapping($previousIdentifier)['type'] === ClassMetadataInfo::MANY_TO_MANY) { |
||
| 87 | $joinAlias = $queryNameGenerator->generateJoinAlias($previousIdentifier); |
||
| 88 | |||
| 89 | $qb->select($joinAlias) |
||
| 90 | ->from($identifierResourceClass, $alias) |
||
| 91 | ->innerJoin("$alias.$previousIdentifier", $joinAlias); |
||
| 92 | } else { |
||
| 93 | $qb->select("IDENTITY($alias.$previousIdentifier)") |
||
| 94 | ->from($identifierResourceClass, $alias); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
| 99 | |||
| 100 | foreach ($normalizedIdentifiers as $key => $value) { |
||
| 101 | $placeholder = $queryNameGenerator->generateParameterName($key); |
||
| 102 | $qb->andWhere("$alias.$key = :$placeholder"); |
||
| 103 | $queryBuilder->setParameter($placeholder, $value); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (null === $previousQueryBuilder) { |
||
| 107 | $previousQueryBuilder = $qb; |
||
| 108 | } else { |
||
| 109 | $previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); |
||
| 110 | } |
||
| 111 | |||
| 112 | $previousAlias = $alias; |
||
| 113 | } |
||
| 114 | |||
| 115 | $queryBuilder->where( |
||
| 116 | $queryBuilder->expr()->in('o', $previousQueryBuilder->getDQL()) |
||
| 117 | ); |
||
| 118 | |||
| 119 | View Code Duplication | foreach ($this->collectionExtensions as $extension) { |
|
| 120 | $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName); |
||
| 121 | |||
| 122 | if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) { |
||
| 123 | return $extension->getResult($queryBuilder); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return $queryBuilder->getQuery()->getResult(); |
||
| 128 | } |
||
| 129 | } |
||
| 130 |
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.