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