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