| Conditions | 16 |
| Paths | 397 |
| Total Lines | 85 |
| Code Lines | 44 |
| Lines | 35 |
| Ratio | 41.18 % |
| 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 |
||
| 84 | private function getIdentifiersFromItem($item): array |
||
| 85 | { |
||
| 86 | $identifiers = []; |
||
| 87 | $resourceClass = $this->getObjectClass($item); |
||
| 88 | |||
| 89 | $cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass); |
||
| 90 | |||
| 91 | try { |
||
| 92 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
||
| 93 | |||
| 94 | if ($cacheItem->isHit()) { |
||
| 95 | foreach ($cacheItem->get() as $propertyName) { |
||
| 96 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
||
| 97 | |||
| 98 | if (is_object($identifiers[$propertyName])) { |
||
| 99 | $relatedCacheKey = self::CACHE_KEY_PREFIX.md5($this->getObjectClass($identifiers[$propertyName])); |
||
| 100 | |||
| 101 | $cacheItem = $this->cacheItemPool->getItem($relatedCacheKey); |
||
| 102 | |||
| 103 | if (!$cacheItem->isHit()) { |
||
| 104 | throw new CacheException('No relation cache item founded, we need more cache to continue.'); |
||
| 105 | } |
||
| 106 | |||
| 107 | $relatedItem = $identifiers[$propertyName]; |
||
| 108 | |||
| 109 | unset($identifiers[$propertyName]); |
||
| 110 | |||
| 111 | // Because composite identifiers aren't allowed in this case, we can use the first identifier only |
||
| 112 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $cacheItem->get()[0]); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $identifiers; |
||
| 117 | } |
||
| 118 | } catch (CacheExceptionInterface $e) { |
||
| 119 | // do nothing |
||
| 120 | } |
||
| 121 | |||
| 122 | View Code Duplication | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
|
| 123 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
| 124 | |||
| 125 | $identifier = $propertyMetadata->isIdentifier(); |
||
| 126 | if (null === $identifier || false === $identifier) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
||
| 131 | |||
| 132 | if (!is_object($identifiers[$propertyName])) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | $relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]); |
||
| 137 | $relatedItem = $identifiers[$propertyName]; |
||
| 138 | |||
| 139 | unset($identifiers[$propertyName]); |
||
| 140 | |||
| 141 | foreach ($this->propertyNameCollectionFactory->create($relatedResourceClass) as $relatedPropertyName) { |
||
| 142 | $propertyMetadata = $this->propertyMetadataFactory->create($relatedResourceClass, $relatedPropertyName); |
||
| 143 | |||
| 144 | if ($propertyMetadata->isIdentifier()) { |
||
| 145 | if (isset($identifiers[$propertyName])) { |
||
| 146 | throw new RuntimeException(sprintf('Composite identifiers not supported in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
||
| 147 | } |
||
| 148 | |||
| 149 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!isset($identifiers[$propertyName])) { |
||
| 154 | throw new RuntimeException(sprintf('No identifier found in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if (isset($cacheItem)) { |
||
| 159 | try { |
||
| 160 | $cacheItem->set(array_keys($identifiers)); |
||
| 161 | $this->cacheItemPool->save($cacheItem); |
||
| 162 | } catch (CacheExceptionInterface $e) { |
||
| 163 | // do nothing |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $identifiers; |
||
| 168 | } |
||
| 169 | |||
| 175 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.