Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class Nette implements CacheInterface |
||
13 | { |
||
14 | /** |
||
15 | * Cache. |
||
16 | * |
||
17 | * @var \Nette\Caching\Cache |
||
18 | */ |
||
19 | protected $cache; |
||
20 | |||
21 | /** |
||
22 | * Config. |
||
23 | * |
||
24 | * @var Config |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | /** |
||
29 | * Cache directory. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $dir; |
||
34 | |||
35 | /** |
||
36 | * Cache constructor. |
||
37 | * @param object $config |
||
38 | * @param null $path |
||
39 | */ |
||
40 | 31 | public function __construct($config = null, $path = null) |
|
48 | |||
49 | /** |
||
50 | * Check if cache is enabled. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | protected function enabled() |
||
58 | |||
59 | /** |
||
60 | * Get the cache directory. |
||
61 | * |
||
62 | * @return mixed|string|static |
||
63 | */ |
||
64 | 31 | public function getCacheDir() |
|
76 | |||
77 | /** |
||
78 | * Get the file storage. |
||
79 | * |
||
80 | * @param null $path |
||
81 | * @return FileStorage |
||
82 | */ |
||
83 | 31 | public function getStorage($path = null) |
|
91 | |||
92 | /** |
||
93 | * Fetches a value from the cache. |
||
94 | * |
||
95 | * @param string $key |
||
96 | * @param null $default |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function get($key, $default = null) |
||
105 | |||
106 | /** |
||
107 | * @param $ttl |
||
108 | * @return string |
||
109 | */ |
||
110 | protected function makeExpiration($ttl) |
||
116 | |||
117 | /** |
||
118 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
119 | * |
||
120 | * @param string $key |
||
121 | * @param mixed $value |
||
122 | * @param null $ttl |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function set($key, $value, $ttl = null) |
||
133 | |||
134 | /** |
||
135 | * Delete an item from the cache by its unique key. |
||
136 | * |
||
137 | * @param string $key |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function delete($key) |
||
144 | |||
145 | /** |
||
146 | * Wipe clean the entire cache's keys. |
||
147 | */ |
||
148 | 31 | public function clear() |
|
152 | |||
153 | /** |
||
154 | * Obtains multiple cache items by their unique keys. |
||
155 | * |
||
156 | * @param $keys |
||
157 | * @param null $default |
||
158 | * @return array |
||
159 | */ |
||
160 | public function getMultiple($keys, $default = null) |
||
166 | |||
167 | /** |
||
168 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
169 | * |
||
170 | * @param $values |
||
171 | * @param null $ttl |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function setMultiple($values, $ttl = null) |
||
180 | |||
181 | /** |
||
182 | * Deletes multiple cache items in a single operation. |
||
183 | * |
||
184 | * @param $keys |
||
185 | * @return bool|void |
||
186 | */ |
||
187 | public function deleteMultiple($keys) |
||
193 | |||
194 | /** |
||
195 | * Determines whether an item is present in the cache. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function has($key) |
||
204 | |||
205 | /** |
||
206 | * Get an item from the cache, or store the default value. |
||
207 | * |
||
208 | * @param string $key |
||
209 | * @param \DateTimeInterface|\DateInterval|float|int $minutes |
||
210 | * @param Closure $callback |
||
211 | * @return mixed |
||
212 | */ |
||
213 | View Code Duplication | public function remember($key, $minutes, Closure $callback) |
|
225 | } |
||
226 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.