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