| Conditions | 10 |
| Paths | 12 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 7 |
| Ratio | 13.46 % |
| 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 |
||
| 52 | public function getSubcollection(string $resourceClass, $id, string $subcollectionProperty, string $operationName = null, array $context = []) |
||
| 53 | { |
||
| 54 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
| 55 | if (null === $manager) { |
||
| 56 | throw new ResourceClassNotSupportedException(); |
||
| 57 | } |
||
| 58 | |||
| 59 | $identifiers = $this->identifiersHelper->normalizeIdentifiers($id, $manager, $resourceClass); |
||
| 60 | |||
| 61 | $fetchData = $context['fetch_data'] ?? true; |
||
| 62 | if (!$fetchData && $manager instanceof EntityManagerInterface) { |
||
| 63 | return $manager->getReference($resourceClass, $identifiers); |
||
| 64 | } |
||
| 65 | |||
| 66 | $classMetadata = $manager->getClassMetadata($resourceClass); |
||
| 67 | $subResourceClass = $classMetadata->getAssociationTargetClass($subcollectionProperty); |
||
| 68 | $subResourceClassMetadata = $manager->getClassMetadata($subResourceClass); |
||
| 69 | |||
| 70 | $repository = $manager->getRepository($subResourceClass); |
||
| 71 | if (!method_exists($repository, 'createQueryBuilder')) { |
||
| 72 | throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | $queryBuilder = $repository->createQueryBuilder('o'); |
||
| 76 | |||
| 77 | $where = null; |
||
| 78 | foreach ($identifiers as $identifier => $value) { |
||
| 79 | $placeholder = ':id_'.$identifier; |
||
| 80 | $expression = sprintf('parentResourceClass.%s = %s', $identifier, $placeholder); |
||
| 81 | |||
| 82 | $where = !$where ? $expression : $with.' AND '.$expression; |
||
| 83 | $queryBuilder->setParameter($placeholder, $value); |
||
| 84 | } |
||
| 85 | |||
| 86 | //@TODO support composite identifiers relation |
||
| 87 | $queryBuilder->andWhere(sprintf( |
||
| 88 | 'o.%s IN (SELECT child.%1$s FROM %s parentResourceClass JOIN parentResourceClass.%s child WHERE %s)', |
||
| 89 | $subResourceClassMetadata->getSingleIdentifierFieldName(), $resourceClass, $subcollectionProperty, $where |
||
| 90 | )); |
||
| 91 | |||
| 92 | $queryNameGenerator = new QueryNameGenerator(); |
||
| 93 | |||
| 94 | View Code Duplication | foreach ($this->collectionExtensions as $extension) { |
|
| 95 | $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName); |
||
| 96 | |||
| 97 | if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) { |
||
| 98 | return $extension->getResult($queryBuilder); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return $queryBuilder->getQuery()->getResult(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |
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.