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 |
||
17 | class RedisCache implements CacheInterface |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var \Redis |
||
22 | */ |
||
23 | protected $redis; |
||
24 | |||
25 | protected $prefix; |
||
26 | |||
27 | /** |
||
28 | * @param string $host |
||
29 | * @param int $port |
||
30 | * @param int $database |
||
31 | * @param string $prefix |
||
32 | */ |
||
33 | 3 | public function __construct( |
|
58 | |||
59 | /** |
||
60 | * close redis connection |
||
61 | */ |
||
62 | public function __destruct() |
||
66 | |||
67 | /** |
||
68 | * close the connection |
||
69 | */ |
||
70 | 3 | public function close() |
|
74 | |||
75 | /** |
||
76 | * get var |
||
77 | * |
||
78 | * @param $key |
||
79 | * @param null $default |
||
80 | * @return bool|string|null |
||
81 | */ |
||
82 | 3 | public function get($key, $default = null) |
|
89 | |||
90 | /** |
||
91 | * set var |
||
92 | * |
||
93 | * @param $key |
||
94 | * @param null $value |
||
95 | * @return boolean |
||
96 | */ |
||
97 | 3 | public function set($key, $value) |
|
101 | |||
102 | /** |
||
103 | * has var ? |
||
104 | * |
||
105 | * @param $key |
||
106 | * @return bool |
||
107 | */ |
||
108 | 3 | public function has($key) |
|
112 | |||
113 | /** |
||
114 | * delete var |
||
115 | * |
||
116 | * @param $key |
||
117 | * @return bool |
||
118 | */ |
||
119 | 3 | public function delete($key) |
|
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.