Conditions | 10 |
Paths | 16 |
Total Lines | 35 |
Code Lines | 17 |
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 |
||
64 | public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
||
65 | { |
||
66 | /** @var DocumentManager $manager */ |
||
67 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
68 | |||
69 | if (!\is_array($id) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) { |
||
70 | $id = $this->normalizeIdentifiers($id, $manager, $resourceClass); |
||
71 | } |
||
72 | |||
73 | $id = (array) $id; |
||
74 | |||
75 | if (!$fetchData = $context['fetch_data'] ?? true) { |
||
|
|||
76 | return $manager->getReference($resourceClass, reset($id)); |
||
77 | } |
||
78 | |||
79 | $repository = $manager->getRepository($resourceClass); |
||
80 | if (!$repository instanceof DocumentRepository) { |
||
81 | throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class)); |
||
82 | } |
||
83 | |||
84 | $aggregationBuilder = $repository->createAggregationBuilder(); |
||
85 | |||
86 | foreach ($id as $propertyName => $value) { |
||
87 | $aggregationBuilder->match()->field($propertyName)->equals($value); |
||
88 | } |
||
89 | |||
90 | foreach ($this->itemExtensions as $extension) { |
||
91 | $extension->applyToItem($aggregationBuilder, $resourceClass, $id, $operationName, $context); |
||
92 | |||
93 | if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
||
94 | return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $aggregationBuilder->hydrate($resourceClass)->execute()->current() ?: null; |
||
99 | } |
||
101 |