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 |
||
7 | class RedisCacheEngine extends BaseCacheEngine |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * |
||
12 | * @var \Redis |
||
13 | */ |
||
14 | protected $redis = null; |
||
15 | |||
16 | protected $logger = null; |
||
17 | |||
18 | protected $server = null; |
||
19 | |||
20 | protected $password = null; |
||
21 | |||
22 | View Code Duplication | public function __construct($server = null, $password = null, $logger = null) |
|
36 | |||
37 | protected function lazyLoadRedisServer() |
||
52 | |||
53 | protected function fixKey($key) { |
||
56 | |||
57 | /** |
||
58 | * @param string $key The object KEY |
||
59 | * @param int $default IGNORED IN MEMCACHED. |
||
60 | * @return mixed Description |
||
61 | */ |
||
62 | View Code Duplication | public function get($key, $default = null) |
|
71 | |||
72 | /** |
||
73 | * @param string $key The object Key |
||
74 | * @param object $value The object to be cached |
||
75 | * @param int $ttl The time to live in seconds of this objects |
||
76 | * @return bool If the object is successfully posted |
||
77 | */ |
||
78 | View Code Duplication | public function set($key, $value, $ttl = null) |
|
87 | |||
88 | public function delete($key) |
||
96 | |||
97 | public function clear() |
||
106 | |||
107 | public function has($key) |
||
111 | |||
112 | public function isAvailable() |
||
125 | } |
||
126 |
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.