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 |
||
19 | class Redis extends Cache |
||
20 | { |
||
21 | /** |
||
22 | * Default hostname of redis server |
||
23 | */ |
||
24 | const REDIS_HOST = 'localhost'; |
||
25 | |||
26 | /** |
||
27 | * Default port of redis server |
||
28 | */ |
||
29 | const REDIS_PORT = 6379; |
||
30 | |||
31 | /** |
||
32 | * @var \Redis The redis driver. |
||
33 | * @since 1.0 |
||
34 | */ |
||
35 | private $driver; |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
||
41 | * |
||
42 | * @since 1.0 |
||
43 | * @throws \RuntimeException |
||
44 | */ |
||
45 | public function __construct($options = array()) |
||
54 | |||
55 | /** |
||
56 | * This will wipe out the entire cache's keys |
||
57 | * |
||
58 | * @return boolean The result of the clear operation. |
||
59 | * |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | public function clear() |
||
68 | |||
69 | /** |
||
70 | * Method to get a storage entry value from a key. |
||
71 | * |
||
72 | * @param string $key The storage entry identifier. |
||
73 | * |
||
74 | * @return CacheItemInterface |
||
75 | * |
||
76 | * @since 1.0 |
||
77 | */ |
||
78 | View Code Duplication | public function get($key) |
|
92 | |||
93 | /** |
||
94 | * Method to remove a storage entry for a key. |
||
95 | * |
||
96 | * @param string $key The storage entry identifier. |
||
97 | * |
||
98 | * @return boolean |
||
99 | * |
||
100 | * @since 1.0 |
||
101 | */ |
||
102 | public function remove($key) |
||
108 | |||
109 | /** |
||
110 | * Method to set a value for a storage entry. |
||
111 | * |
||
112 | * @param string $key The storage entry identifier. |
||
113 | * @param mixed $value The data to be stored. |
||
114 | * @param integer $ttl The number of seconds before the stored data expires. |
||
115 | * |
||
116 | * @return boolean |
||
117 | * |
||
118 | * @since 1.0 |
||
119 | */ |
||
120 | public function set($key, $value, $ttl = null) |
||
136 | |||
137 | /** |
||
138 | * Method to determine whether a storage entry has been set for a key. |
||
139 | * |
||
140 | * @param string $key The storage entry identifier. |
||
141 | * |
||
142 | * @return boolean |
||
143 | * |
||
144 | * @since 1.0 |
||
145 | */ |
||
146 | public function exists($key) |
||
152 | |||
153 | /** |
||
154 | * Connect to the Redis servers if the connection does not already exist. |
||
155 | * |
||
156 | * @return void |
||
157 | * |
||
158 | * @since 1.0 |
||
159 | */ |
||
160 | private function connect() |
||
174 | } |
||
175 |
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.