Conditions | 11 |
Paths | 16 |
Total Lines | 34 |
Code Lines | 18 |
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 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
67 | |||
68 | if (!\is_array($id) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) { |
||
69 | $id = $this->normalizeIdentifiers($id, $manager, $resourceClass); |
||
|
|||
70 | } |
||
71 | $id = (array) $id; |
||
72 | |||
73 | $fetchData = $context['fetch_data'] ?? true; |
||
74 | if (!$fetchData && $manager instanceof DocumentManager) { |
||
75 | return $manager->getReference($resourceClass, reset($id)); |
||
76 | } |
||
77 | |||
78 | $repository = $manager->getRepository($resourceClass); |
||
79 | if (!method_exists($repository, 'createAggregationBuilder')) { |
||
80 | throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.'); |
||
81 | } |
||
82 | /** @var Builder $aggregationBuilder */ |
||
83 | $aggregationBuilder = $repository->createAggregationBuilder(); |
||
84 | |||
85 | foreach ($id as $propertyName => $value) { |
||
86 | $aggregationBuilder->match()->field($propertyName)->equals($value); |
||
87 | } |
||
88 | |||
89 | foreach ($this->itemExtensions as $extension) { |
||
90 | $extension->applyToItem($aggregationBuilder, $resourceClass, $id, $operationName, $context); |
||
91 | |||
92 | if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
||
93 | return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $aggregationBuilder->hydrate($resourceClass)->execute()->current() ?: null; |
||
98 | } |
||
100 |