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