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 |
||
18 | class PhileToPsr16CacheAdapter implements \Phile\ServiceLocator\CacheInterface |
||
19 | { |
||
20 | /** @var string slug */ |
||
21 | const SLUG_PREFIX = '-phile.phpFastCache.slug-'; |
||
22 | |||
23 | const SLUG = ['{', '}' , '(', ')','/' , '\\' , '@', ':']; |
||
24 | |||
25 | /** |
||
26 | * @var CacheInterface the cache engine |
||
27 | */ |
||
28 | protected $cacheEngine; |
||
29 | |||
30 | /** |
||
31 | * the constructor |
||
32 | * |
||
33 | * @param CacheInterface $cacheEngine |
||
34 | */ |
||
35 | 28 | public function __construct(CacheInterface $cacheEngine) |
|
39 | |||
40 | /** |
||
41 | * method to check if cache has entry for given key |
||
42 | * |
||
43 | * @param string $key |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | 20 | public function has($key) |
|
48 | { |
||
49 | 20 | $key = $this->slug($key); |
|
50 | 20 | return $this->cacheEngine->has($key); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * method to get cache entry |
||
55 | * |
||
56 | * @param string $key |
||
57 | * |
||
58 | * @return mixed|null |
||
59 | */ |
||
60 | 2 | public function get($key) |
|
65 | |||
66 | /** |
||
67 | * method to set cache entry |
||
68 | * |
||
69 | * @param string $key |
||
70 | * @param mixed $value |
||
71 | * @param int $time |
||
72 | * @param array $options deprecated |
||
73 | * |
||
74 | * @return mixed|void |
||
75 | */ |
||
76 | 21 | View Code Duplication | public function set($key, $value, $time = 300, array $options = array()) |
85 | |||
86 | /** |
||
87 | * method to delete cache entry |
||
88 | * |
||
89 | * @param string $key |
||
90 | * @param array $options deprecated |
||
91 | * |
||
92 | * @return mixed|void |
||
93 | */ |
||
94 | View Code Duplication | public function delete($key, array $options = array()) |
|
103 | |||
104 | /** |
||
105 | * clean complete cache and delete all cached entries |
||
106 | */ |
||
107 | public function clean() |
||
111 | |||
112 | /** |
||
113 | * replaces chars forbidden in PSR-16 cache-keys |
||
114 | * |
||
115 | * @param string $key key to slug |
||
116 | * @return string $key slugged key |
||
117 | */ |
||
118 | 21 | protected function slug(string $key): string |
|
128 | } |
||
129 |
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.