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 |
||
6 | class RedisCache implements CacheInterface{ |
||
7 | |||
8 | protected $cache; |
||
9 | |||
10 | /** |
||
11 | * constructor |
||
12 | */ |
||
13 | 2 | public function __construct(){ |
|
14 | 2 | $this->cache = new Redis(); |
|
15 | 2 | } |
|
16 | |||
17 | /** |
||
18 | * destructor closes the connection |
||
19 | */ |
||
20 | 2 | public function __destruct(){ |
|
21 | 2 | if (is_object($this->cache)) { |
|
22 | 2 | $this->cache->close(); |
|
23 | } |
||
24 | 2 | } |
|
25 | |||
26 | /** |
||
27 | * Connect to a server |
||
28 | * @param string $host This should be the host name or IP address you want to connect to |
||
29 | * @param int $port The port number where Memcache can be accessed |
||
30 | * @param boolean $persistent If you want this connection to be persistent set to true else set to false |
||
31 | * @return $this |
||
32 | */ |
||
33 | 2 | public function connect($host, $port, $persistent = false){ |
|
34 | 2 | $this->addServer($host, $port, $persistent); |
|
35 | 2 | return $this; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Add a server to connection pool |
||
40 | * @param string $host This should be the host name or IP address you want to add to the Memcache pool |
||
41 | * @param int $port The port number where Memcache can be accessed |
||
42 | * @param boolean $persistent If you want this connection to be persistent set to true else set to false |
||
43 | * @return $this |
||
44 | */ |
||
45 | 2 | View Code Duplication | public function addServer($host, $port, $persistent = false){ |
46 | 2 | if ($persistent === false) { |
|
47 | 2 | $this->cache->connect($host, intval($port)); |
|
48 | } |
||
49 | else { |
||
50 | $this->cache->pconnect($host, intval($port)); |
||
51 | } |
||
52 | 2 | return $this; |
|
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Adds a value to be stored on the server |
||
58 | * @param string $key This should be the key for the value you wish to add |
||
59 | * @param mixed $value The value you wish to be stored with the given key |
||
60 | * @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
||
61 | * @return boolean Returns true if successfully added or false on failure |
||
62 | */ |
||
63 | 1 | public function save($key, $value, $time = 0){ |
|
64 | 1 | return $this->cache->set($key, $value, intval($time)); |
|
65 | } |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Replaces a stored value for a given key |
||
70 | * @param string $key This should be the key for the value you wish to replace |
||
71 | * @param mixed $value The new value that you wish to give to that key |
||
72 | * @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
||
73 | * @return boolean Returns true if successfully replaced or false on failure |
||
74 | */ |
||
75 | public function replace($key, $value, $time = 0){ |
||
78 | |||
79 | /** |
||
80 | * Returns the values store for the given key |
||
81 | * @param string $key This should be the unique query key to get the value |
||
82 | * @return mixed The store value will be returned |
||
83 | */ |
||
84 | 1 | public function fetch($key){ |
|
85 | 1 | return $this->cache->get($key); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Deletes a single value from the server based on the given key |
||
90 | * @param string $key This should be the key that you wish to delete the value for |
||
91 | * @return boolean Returns true on success or false on failure |
||
92 | */ |
||
93 | public function delete($key){ |
||
96 | |||
97 | /** |
||
98 | * Deletes all values from the server |
||
99 | * @return boolean Returns true on success or false on failure |
||
100 | */ |
||
101 | public function deleteAll(){ |
||
104 | } |
||
105 |