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:
Complex classes like FileCache 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 FileCache, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Cache; |
||
25 | class FileCache extends CacheObject { |
||
26 | |||
27 | /** |
||
28 | * Current cache folder |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $cache_folder = null; |
||
33 | |||
34 | /** |
||
35 | * Class constructor |
||
36 | * |
||
37 | * @throws \Comodojo\Exception\CacheException |
||
38 | */ |
||
39 | 186 | public function __construct($cache_folder = null, \Monolog\Logger $logger = null) { |
|
80 | |||
81 | /** |
||
82 | * Set cache element |
||
83 | * |
||
84 | * This method will throw only logical exceptions. |
||
85 | * In case of failures, it will return a boolean false. |
||
86 | * |
||
87 | * @param string $name Name for cache element |
||
88 | * @param mixed $data Data to cache |
||
89 | * @param int $ttl Time to live |
||
90 | * |
||
91 | * @return bool |
||
92 | * @throws \Comodojo\Exception\CacheException |
||
93 | */ |
||
94 | 72 | public function set($name, $data, $ttl = null) { |
|
133 | |||
134 | /** |
||
135 | * Get cache element |
||
136 | * |
||
137 | * This method will throw only logical exceptions. |
||
138 | * In case of failures, it will return a null value. |
||
139 | * In case of cache not found, it will return a null value. |
||
140 | * |
||
141 | * @param string $name Name for cache element |
||
142 | * |
||
143 | * @return mixed |
||
144 | * @throws \Comodojo\Exception\CacheException |
||
145 | */ |
||
146 | 26 | public function get($name) { |
|
169 | |||
170 | /** |
||
171 | * Delete cache object (or entire namespace if $name is null) |
||
172 | * |
||
173 | * This method will throw only logical exceptions. |
||
174 | * In case of failures, it will return a boolean false. |
||
175 | * |
||
176 | * @param string $name Name for cache element |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 18 | public function delete($name = null) { |
|
181 | |||
182 | 18 | if ( !$this->isEnabled() ) return false; |
|
183 | |||
184 | 18 | $this->resetErrorState(); |
|
185 | |||
186 | 18 | $return = true; |
|
187 | |||
188 | 18 | if ( is_null($name) ) { |
|
189 | |||
190 | $filesList = glob($this->cache_folder."*-".$this->getNamespace().".{cache,expire}", GLOB_BRACE); |
||
191 | |||
192 | } else { |
||
193 | |||
194 | 18 | $filesList = glob($this->cache_folder.md5($name)."-".$this->getNamespace().".{cache,expire}", GLOB_BRACE); |
|
195 | |||
196 | } |
||
197 | |||
198 | 18 | View Code Duplication | foreach ( $filesList as $file ) { |
|
|||
199 | |||
200 | 18 | if ( unlink($file) === false ) { |
|
201 | |||
202 | $this->raiseError("Failed to unlink cache file (File), exiting gracefully", pathinfo($file)); |
||
203 | |||
204 | $this->setErrorState(); |
||
205 | |||
206 | $return = false; |
||
207 | |||
208 | } |
||
209 | |||
210 | 18 | } |
|
211 | |||
212 | 18 | return $return; |
|
213 | |||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Clean cache objects in all namespaces |
||
218 | * |
||
219 | * This method will throw only logical exceptions. |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | 18 | public function flush() { |
|
224 | |||
225 | 18 | if ( !$this->isEnabled() ) return false; |
|
226 | |||
227 | 18 | $this->resetErrorState(); |
|
228 | |||
229 | 18 | $return = true; |
|
230 | |||
231 | 18 | $filesList = glob($this->cache_folder."*.{cache,expire}", GLOB_BRACE); |
|
232 | |||
233 | 18 | View Code Duplication | foreach ( $filesList as $file ) { |
234 | |||
235 | 18 | if ( unlink($file) === false ) { |
|
236 | |||
237 | $this->raiseError("Failed to unlink whole cache (File), exiting gracefully", pathinfo($file)); |
||
238 | |||
239 | $this->setErrorState(); |
||
240 | |||
241 | $return = false; |
||
242 | |||
243 | } |
||
244 | |||
245 | 18 | } |
|
246 | |||
247 | 18 | return $return; |
|
248 | |||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get cache status |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 18 | public function status() { |
|
257 | |||
258 | 18 | $filesList = glob($this->cache_folder."*.cache"); |
|
259 | |||
260 | 18 | if ( self::checkXattrSupport() ) { |
|
261 | |||
262 | $options = array( |
||
263 | "xattr_supported" => true, |
||
264 | "xattr_enabled" => $this->checkXattrFilesystemSupport($this->cache_folder) |
||
265 | ); |
||
266 | |||
267 | } else { |
||
268 | |||
269 | $options = array( |
||
270 | 18 | "xattr_supported" => false, |
|
271 | "xattr_enabled" => false |
||
272 | 18 | ); |
|
273 | |||
274 | } |
||
275 | |||
276 | return array( |
||
277 | 18 | "provider" => "file", |
|
278 | 18 | "enabled" => $this->isEnabled(), |
|
279 | 18 | "objects" => count($filesList), |
|
280 | "options" => $options |
||
281 | 18 | ); |
|
282 | |||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Set a cache element using xattr |
||
287 | * |
||
288 | * @param string $name Name for cache element |
||
289 | * @param mixed $data Data to cache |
||
290 | * @param int $ttl Time to live |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | View Code Duplication | private function setXattr($name, $data, $ttl) { |
|
325 | |||
326 | /** |
||
327 | * Set a cache element using ghost file |
||
328 | * |
||
329 | * @param string $name Name for cache element |
||
330 | * @param mixed $data Data to cache |
||
331 | * @param int $ttl Time to live |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | 72 | View Code Duplication | private function setGhost($name, $data, $ttl) { |
368 | |||
369 | /** |
||
370 | * Get a cache element using xattr |
||
371 | * |
||
372 | * @param string $name Name for cache element |
||
373 | * @param int $time |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | private function getXattr($name, $time) { |
||
426 | |||
427 | /** |
||
428 | * Get a cache element using ghost file |
||
429 | * |
||
430 | * @param string $name Name for cache element |
||
431 | * @param int $time |
||
432 | * |
||
433 | * @return mixed |
||
434 | */ |
||
435 | 26 | private function getGhost($name, $time) { |
|
482 | |||
483 | /** |
||
484 | * Check if cache folder is writable |
||
485 | * |
||
486 | * @param string $folder |
||
487 | * |
||
488 | * @return bool |
||
489 | */ |
||
490 | 186 | private static function checkCacheFolder($folder) { |
|
495 | |||
496 | /** |
||
497 | * Check xattr (extension) support |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | 105 | private static function checkXattrSupport() { |
|
506 | |||
507 | /** |
||
508 | * Check xattr (filesystem) support |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | private static function checkXattrFilesystemSupport($folder) { |
||
517 | |||
518 | } |
||
519 |
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.