| Conditions | 8 |
| Paths | 25 |
| Total Lines | 55 |
| 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 |
||
| 69 | final protected function getMultipleCacheItems( |
||
| 70 | array $ids, |
||
| 71 | string $keyPrefix, |
||
| 72 | callable $missingLoader, |
||
| 73 | callable $loadedTagger, |
||
| 74 | array $keySuffixes = [] |
||
| 75 | ): array { |
||
| 76 | if (empty($ids)) { |
||
| 77 | return []; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Generate unique cache keys |
||
| 81 | $cacheKeys = []; |
||
| 82 | $cacheKeysToIdMap = []; |
||
| 83 | foreach (\array_unique($ids) as $id) { |
||
| 84 | $key = $keyPrefix . $id . ($keySuffixes[$id] ?? ''); |
||
| 85 | $cacheKeys[] = $key; |
||
| 86 | $cacheKeysToIdMap[$key] = $id; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Load cache items by cache keys (will contain hits and misses) |
||
| 90 | $list = []; |
||
| 91 | $cacheMisses = []; |
||
| 92 | foreach ($this->cache->getItems($cacheKeys) as $key => $cacheItem) { |
||
| 93 | $id = $cacheKeysToIdMap[$key]; |
||
| 94 | if ($cacheItem->isHit()) { |
||
| 95 | $list[$id] = $cacheItem->get(); |
||
| 96 | } else { |
||
| 97 | $cacheMisses[] = $id; |
||
| 98 | $list[$id] = $cacheItem; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // No misses, return completely cached list |
||
| 103 | if (empty($cacheMisses)) { |
||
| 104 | return $list; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Load missing items, save to cache & apply to list if found |
||
| 108 | $loadedList = $missingLoader($cacheMisses); |
||
| 109 | foreach ($cacheMisses as $id) { |
||
| 110 | if (isset($loadedList[$id])) { |
||
| 111 | $this->cache->save( |
||
| 112 | $list[$id] |
||
| 113 | ->set($loadedList[$id]) |
||
| 114 | ->tag($loadedTagger($loadedList[$id])) |
||
| 115 | ); |
||
| 116 | $list[$id] = $loadedList[$id]; |
||
| 117 | } else { |
||
| 118 | unset($list[$id]); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $list; |
||
| 123 | } |
||
| 124 | |||
| 134 |