Complex classes like Cache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class Cache extends Component implements CacheInterface |
||
69 | { |
||
70 | /** |
||
71 | * @var \Psr\SimpleCache\CacheInterface|array|\Closure|string actual cache handler or its DI compatible configuration. |
||
72 | * After the Cache object is created, if you want to change this property, you should only assign it |
||
73 | * with a [[\Psr\SimpleCache\CacheInterface]] instance. |
||
74 | * @since 2.1.0 |
||
75 | */ |
||
76 | public $handler; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 199 | public function init() |
|
87 | |||
88 | /** |
||
89 | * Builds a normalized cache key from a given key. |
||
90 | * |
||
91 | * If the given key is a string containing alphanumeric characters only and no more than 32 characters, |
||
92 | * then the key will be returned back as it is. Otherwise, a normalized key is generated by serializing |
||
93 | * the given key and applying MD5 hashing. |
||
94 | * |
||
95 | * @param mixed $key the key to be normalized |
||
96 | * @return string the generated cache key |
||
97 | */ |
||
98 | 198 | protected function buildKey($key) |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 105 | public function get($key, $default = null) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 8 | public function has($key) |
|
136 | |||
137 | /** |
||
138 | * Retrieves multiple values from cache with the specified keys. |
||
139 | * Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
||
140 | * which may improve the performance. In case a cache does not support this feature natively, |
||
141 | * this method will try to simulate it. |
||
142 | * @param string[] $keys list of string keys identifying the cached values |
||
143 | * @param mixed $default Default value to return for keys that do not exist. |
||
144 | * @return array list of cached values corresponding to the specified keys. The array |
||
145 | * is returned in terms of (key, value) pairs. |
||
146 | * If a value is not cached or expired, the corresponding array value will be false. |
||
147 | * @since 2.0.7 |
||
148 | */ |
||
149 | 11 | public function getMultiple($keys, $default = null) |
|
174 | |||
175 | /** |
||
176 | * Stores a value identified by a key into cache. |
||
177 | * If the cache already contains such a key, the existing value and |
||
178 | * expiration time will be replaced with the new ones, respectively. |
||
179 | * |
||
180 | * @param mixed $key a key identifying the value to be cached. This can be a simple string or |
||
181 | * a complex data structure consisting of factors representing the key. |
||
182 | * @param mixed $value the value to be cached |
||
183 | * @param null|int|\DateInterval $ttl the TTL value of this item. If not set, default value is used. |
||
184 | * @param Dependency $dependency dependency of the cached item. If the dependency changes, |
||
185 | * the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
||
186 | * This parameter is ignored if [[serializer]] is false. |
||
187 | * @return bool whether the value is successfully stored into cache |
||
188 | */ |
||
189 | 96 | public function set($key, $value, $ttl = null, $dependency = null) |
|
198 | |||
199 | /** |
||
200 | * Stores multiple items in cache. Each item contains a value identified by a key. |
||
201 | * If the cache already contains such a key, the existing value and |
||
202 | * expiration time will be replaced with the new ones, respectively. |
||
203 | * |
||
204 | * @param array $items the items to be cached, as key-value pairs. |
||
205 | * @param null|int|\DateInterval $ttl the TTL value of this item. If not set, default value is used. |
||
206 | * @param Dependency $dependency dependency of the cached items. If the dependency changes, |
||
207 | * the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
||
208 | * This parameter is ignored if [[serializer]] is false. |
||
209 | * @return array array of failed keys |
||
210 | * @since 2.0.7 |
||
211 | */ |
||
212 | 16 | public function setMultiple($items, $ttl = 0, $dependency = null) |
|
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | * @since 2.1 |
||
233 | */ |
||
234 | public function deleteMultiple($keys) |
||
242 | |||
243 | /** |
||
244 | * Stores multiple items in cache. Each item contains a value identified by a key. |
||
245 | * If the cache already contains such a key, the existing value and expiration time will be preserved. |
||
246 | * |
||
247 | * @param array $values the items to be cached, as key-value pairs. |
||
248 | * @param null|int|\DateInterval $ttl the TTL value of this item. If not set, default value is used. |
||
249 | * @param Dependency $dependency dependency of the cached items. If the dependency changes, |
||
250 | * the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
||
251 | * This parameter is ignored if [[serializer]] is false. |
||
252 | * @return array array of failed keys |
||
253 | * @since 2.0.7 |
||
254 | */ |
||
255 | 5 | public function addMultiple($values, $ttl = 0, $dependency = null) |
|
279 | |||
280 | /** |
||
281 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
282 | * Nothing will be done if the cache already contains the key. |
||
283 | * @param mixed $key a key identifying the value to be cached. This can be a simple string or |
||
284 | * a complex data structure consisting of factors representing the key. |
||
285 | * @param mixed $value the value to be cached |
||
286 | * @param null|int|\DateInterval $ttl the TTL value of this item. If not set, default value is used. |
||
287 | * @param Dependency $dependency dependency of the cached item. If the dependency changes, |
||
288 | * the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
||
289 | * This parameter is ignored if [[serializer]] is false. |
||
290 | * @return bool whether the value is successfully stored into cache |
||
291 | */ |
||
292 | 8 | public function add($key, $value, $ttl = null, $dependency = null) |
|
307 | |||
308 | /** |
||
309 | * Deletes a value with the specified key from cache |
||
310 | * @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or |
||
311 | * a complex data structure consisting of factors representing the key. |
||
312 | * @return bool if no error happens during deletion |
||
313 | */ |
||
314 | 93 | public function delete($key) |
|
320 | |||
321 | /** |
||
322 | * Deletes all values from cache. |
||
323 | * Be careful of performing this operation if the cache is shared among multiple applications. |
||
324 | * @return bool whether the flush operation was successful. |
||
325 | */ |
||
326 | 55 | public function clear() |
|
330 | |||
331 | /** |
||
332 | * Returns whether there is a cache entry with a specified key. |
||
333 | * This method is required by the interface [[\ArrayAccess]]. |
||
334 | * @param string $key a key identifying the cached value |
||
335 | * @return bool |
||
336 | */ |
||
337 | public function offsetExists($key) |
||
341 | |||
342 | /** |
||
343 | * Retrieves the value from cache with a specified key. |
||
344 | * This method is required by the interface [[\ArrayAccess]]. |
||
345 | * @param string $key a key identifying the cached value |
||
346 | * @return mixed the value stored in cache, false if the value is not in the cache or expired. |
||
347 | */ |
||
348 | 5 | public function offsetGet($key) |
|
352 | |||
353 | /** |
||
354 | * Stores the value identified by a key into cache. |
||
355 | * If the cache already contains such a key, the existing value will be |
||
356 | * replaced with the new ones. To add expiration and dependencies, use the [[set()]] method. |
||
357 | * This method is required by the interface [[\ArrayAccess]]. |
||
358 | * @param string $key the key identifying the value to be cached |
||
359 | * @param mixed $value the value to be cached |
||
360 | */ |
||
361 | 50 | public function offsetSet($key, $value) |
|
365 | |||
366 | /** |
||
367 | * Deletes the value with the specified key from cache |
||
368 | * This method is required by the interface [[\ArrayAccess]]. |
||
369 | * @param string $key the key of the value to be deleted |
||
370 | */ |
||
371 | public function offsetUnset($key) |
||
375 | |||
376 | /** |
||
377 | * Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key, |
||
378 | * or to store the result of $callable execution if there is no cache available for the $key. |
||
379 | * |
||
380 | * Usage example: |
||
381 | * |
||
382 | * ```php |
||
383 | * public function getTopProducts($count = 10) { |
||
384 | * $cache = $this->cache; // Could be Yii::$app->cache |
||
385 | * return $cache->getOrSet(['top-n-products', 'n' => $count], function ($cache) use ($count) { |
||
386 | * return Products::find()->mostPopular()->limit(10)->all(); |
||
387 | * }, 1000); |
||
388 | * } |
||
389 | * ``` |
||
390 | * |
||
391 | * @param mixed $key a key identifying the value to be cached. This can be a simple string or |
||
392 | * a complex data structure consisting of factors representing the key. |
||
393 | * @param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached. |
||
394 | * In case $callable returns `false`, the value will not be cached. |
||
395 | * @param null|int|\DateInterval $ttl the TTL value of this item. If not set, default value is used. |
||
396 | * @param Dependency $dependency dependency of the cached item. If the dependency changes, |
||
397 | * the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
||
398 | * This parameter is ignored if [[serializer]] is `false`. |
||
399 | * @return mixed result of $callable execution |
||
400 | * @since 2.0.11 |
||
401 | */ |
||
402 | 10 | public function getOrSet($key, $callable, $ttl = null, $dependency = null) |
|
415 | } |
||
416 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: