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