Conditions | 12 |
Paths | 35 |
Total Lines | 54 |
Code Lines | 30 |
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 |
||
60 | public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
||
61 | { |
||
62 | if ($this->decorated) { |
||
63 | try { |
||
64 | return $this->decorated->getItem($resourceClass, $id, $operationName, $context); |
||
65 | } catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) { |
||
66 | // Ignore it |
||
67 | } |
||
68 | } |
||
69 | |||
70 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
71 | if (null === $manager) { |
||
72 | throw new ResourceClassNotSupportedException(); |
||
73 | } |
||
74 | |||
75 | $identifierValues = explode('-', (string) $id); |
||
76 | $identifiers = []; |
||
77 | $i = 0; |
||
78 | |||
79 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
||
80 | $itemMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
81 | |||
82 | $identifier = $itemMetadata->isIdentifier(); |
||
83 | if (null === $identifier || false === $identifier) { |
||
84 | continue; |
||
85 | } |
||
86 | |||
87 | if (!isset($identifierValues[$i])) { |
||
88 | throw new InvalidArgumentException(sprintf('Invalid identifier "%s".', $id)); |
||
89 | } |
||
90 | |||
91 | $identifiers[$propertyName] = $identifierValues[$i]; |
||
92 | ++$i; |
||
93 | } |
||
94 | |||
95 | $fetchData = $context['fetch_data'] ?? true; |
||
96 | if (!$fetchData && $manager instanceof DocumentManager) { |
||
97 | return $manager->getReference($resourceClass, reset($identifiers)); |
||
98 | } |
||
99 | |||
100 | /** @var DocumentRepository $repository */ |
||
101 | $repository = $manager->getRepository($resourceClass); |
||
102 | $queryBuilder = $repository->createQueryBuilder(); |
||
103 | |||
104 | foreach ($identifiers as $propertyName => $value) { |
||
105 | $queryBuilder |
||
106 | ->field($propertyName)->equals($value); |
||
107 | } |
||
108 | |||
109 | foreach ($this->itemExtensions as $extension) { |
||
110 | $extension->applyToItem($queryBuilder, $resourceClass, $identifiers, $operationName); |
||
111 | } |
||
112 | |||
113 | return $queryBuilder->getQuery()->getSingleResult(); |
||
|
|||
114 | } |
||
116 |