Conditions | 10 |
Paths | 65 |
Total Lines | 58 |
Code Lines | 31 |
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 |
||
47 | public function getIdentifiersFromItem($item): array |
||
48 | { |
||
49 | $identifiers = []; |
||
50 | $resourceClass = $this->getObjectClass($item); |
||
51 | |||
52 | $cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass); |
||
53 | |||
54 | // This is to avoid setting the cache twice in the case where the related item cache doesn't exist |
||
55 | $cacheIsHit = false; |
||
56 | |||
57 | try { |
||
58 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
||
59 | $isRelationCached = true; |
||
60 | |||
61 | if ($cacheIsHit = $cacheItem->isHit()) { |
||
62 | foreach ($cacheItem->get() as $propertyName) { |
||
63 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
||
64 | |||
65 | if (!is_object($identifiers[$propertyName])) { |
||
66 | continue; |
||
67 | } |
||
68 | |||
69 | $relatedItem = $identifiers[$propertyName]; |
||
70 | $relatedCacheKey = self::CACHE_KEY_PREFIX.md5($this->getObjectClass($relatedItem)); |
||
71 | |||
72 | $relatedCacheItem = $this->cacheItemPool->getItem($relatedCacheKey); |
||
73 | |||
74 | if (!$relatedCacheItem->isHit()) { |
||
75 | $isRelationCached = false; |
||
76 | break; |
||
77 | } |
||
78 | |||
79 | unset($identifiers[$propertyName]); |
||
80 | |||
81 | $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedCacheItem->get()[0]); |
||
82 | } |
||
83 | |||
84 | if (true === $isRelationCached) { |
||
85 | return $identifiers; |
||
86 | } |
||
87 | } |
||
88 | } catch (CacheException $e) { |
||
89 | // do nothing |
||
90 | } |
||
91 | |||
92 | $identifiers = $this->decorated->getIdentifiersFromItem($item); |
||
93 | |||
94 | if (isset($cacheItem) && false === $cacheIsHit) { |
||
95 | try { |
||
96 | $cacheItem->set(array_keys($identifiers)); |
||
97 | $this->cacheItemPool->save($cacheItem); |
||
98 | } catch (CacheException $e) { |
||
99 | // do nothing |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $identifiers; |
||
104 | } |
||
105 | } |
||
106 |