| Conditions | 10 |
| Paths | 19 |
| Total Lines | 45 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 58 | public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
||
| 59 | { |
||
| 60 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
| 61 | |||
| 62 | $identifierValues = explode('-', (string) $id); |
||
| 63 | $identifiers = []; |
||
| 64 | $i = 0; |
||
| 65 | |||
| 66 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
||
| 67 | $itemMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
| 68 | |||
| 69 | $identifier = $itemMetadata->isIdentifier(); |
||
| 70 | if (null === $identifier || false === $identifier) { |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (!isset($identifierValues[$i])) { |
||
| 75 | throw new InvalidArgumentException(sprintf('Invalid identifier "%s".', $id)); |
||
| 76 | } |
||
| 77 | |||
| 78 | $identifiers[$propertyName] = $identifierValues[$i]; |
||
| 79 | ++$i; |
||
| 80 | } |
||
| 81 | |||
| 82 | $fetchData = $context['fetch_data'] ?? true; |
||
| 83 | if (!$fetchData && $manager instanceof DocumentManager) { |
||
| 84 | return $manager->getReference($resourceClass, reset($identifiers)); |
||
| 85 | } |
||
| 86 | |||
| 87 | $repository = $manager->getRepository($resourceClass); |
||
| 88 | if (!method_exists($repository, 'createAggregationBuilder')) { |
||
| 89 | throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.'); |
||
| 90 | } |
||
| 91 | /** @var Builder $aggregationBuilder */ |
||
| 92 | $aggregationBuilder = $repository->createAggregationBuilder(); |
||
| 93 | |||
| 94 | foreach ($identifiers as $propertyName => $value) { |
||
| 95 | $aggregationBuilder->match()->field($propertyName)->equals($value); |
||
| 96 | } |
||
| 97 | |||
| 98 | foreach ($this->itemExtensions as $extension) { |
||
| 99 | $extension->applyToItem($aggregationBuilder, $resourceClass, $identifiers, $operationName); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $aggregationBuilder->hydrate($resourceClass)->execute()->getSingleResult(); |
||
|
|
|||
| 103 | } |
||
| 105 |