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 |
||
13 | class FileSystemAdapter extends Adapter implements CacheAdapter |
||
14 | { |
||
15 | const CACHE_FILE_SUFFIX = '.php.cache'; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $cacheDir; |
||
21 | |||
22 | /** |
||
23 | * @param string $cacheDir |
||
24 | * @param CacheAdapter $next |
||
25 | * |
||
26 | * @throws \InvalidArgumentException |
||
27 | */ |
||
28 | public function __construct($cacheDir, CacheAdapter $next = null) |
||
48 | |||
49 | /** |
||
50 | * Get a value identified by $key. |
||
51 | * |
||
52 | * @param string $key |
||
53 | * |
||
54 | * @return bool|mixed |
||
55 | */ |
||
56 | public function get($key) |
||
82 | |||
83 | /** |
||
84 | * @param string $key |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | private function getFilenameFromCacheKey($key) |
||
97 | |||
98 | /** |
||
99 | * @param string $key |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | private function getDirectoryHash($key) |
||
116 | |||
117 | /** |
||
118 | * @param $directoryHash |
||
119 | */ |
||
120 | private function createCacheHashDirectory($directoryHash) |
||
127 | |||
128 | /** |
||
129 | * @param $fileKey |
||
130 | * |
||
131 | * @throws \RuntimeException |
||
132 | */ |
||
133 | private function removeCacheFile($fileKey) |
||
137 | |||
138 | /** |
||
139 | * Set a value identified by $key and with an optional $ttl. |
||
140 | * |
||
141 | * @param string $key |
||
142 | * @param mixed $value |
||
143 | * @param int $ttl |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function set($key, $value, $ttl = 0) |
||
160 | |||
161 | /** |
||
162 | * @param $ttl |
||
163 | * |
||
164 | * @return int |
||
165 | */ |
||
166 | View Code Duplication | private function getCalculatedTtl($ttl) |
|
175 | |||
176 | /** |
||
177 | * @param $value |
||
178 | * @param $calculatedTtl |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | private function buildDataCache($value, $calculatedTtl) |
||
191 | |||
192 | /** |
||
193 | * Delete a value identified by $key. |
||
194 | * |
||
195 | * @param string $key |
||
196 | * |
||
197 | * @throws \RuntimeException |
||
198 | */ |
||
199 | public function delete($key) |
||
209 | |||
210 | /** |
||
211 | * Checks the availability of the cache service. |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function isAvailable() |
||
219 | |||
220 | /** |
||
221 | * Clears all expired values from cache. |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | public function clear() |
||
230 | |||
231 | /** |
||
232 | * @param string $directory |
||
233 | */ |
||
234 | private function clearCacheFiles($directory) |
||
247 | |||
248 | /** |
||
249 | * Clears all values from the cache. |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function drop() |
||
258 | |||
259 | /** |
||
260 | * @param string $directory |
||
261 | */ |
||
262 | private function removeCacheFiles($directory) |
||
272 | } |
||
273 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.