Conditions | 10 |
Paths | 27 |
Total Lines | 44 |
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 |
||
60 | public function cache($key, callable $cachedCallable, $ttl, callable $onNoStaleCacheCallable = null) |
||
61 | { |
||
62 | $value = $this->getValue($key); |
||
63 | |||
64 | if ($value->hasResult() && !$value->isStale()) { |
||
65 | return $value->getResult(); |
||
66 | } |
||
67 | |||
68 | if (!($ttl instanceof Ttl)) { |
||
69 | $ttl = new Ttl($ttl); |
||
70 | } |
||
71 | |||
72 | $lock_acquired = $this->lockManager->acquire($key, $ttl->getLockTtl()); |
||
73 | |||
74 | if (!$lock_acquired) { |
||
75 | if ($value->hasResult()) { // serve stale if present |
||
76 | return $value->getResult(); |
||
77 | } |
||
78 | |||
79 | if (!$onNoStaleCacheCallable) { |
||
80 | $onNoStaleCacheCallable = $this->onNoStaleCacheCallable; |
||
81 | } |
||
82 | |||
83 | if ($onNoStaleCacheCallable !== null) { |
||
84 | $event = new NoStaleCacheEvent($this, $key, $cachedCallable, $ttl); |
||
85 | |||
86 | call_user_func($onNoStaleCacheCallable, $event); |
||
87 | |||
88 | if ($event->hasResult()) { |
||
89 | return $event->getResult(); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $result = call_user_func($cachedCallable); |
||
95 | |||
96 | $this->setResult($key, $result, $ttl); |
||
97 | |||
98 | if ($lock_acquired) { |
||
99 | $this->lockManager->release($key); |
||
100 | } |
||
101 | |||
102 | return $result; |
||
103 | } |
||
104 | |||
170 |