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 |
||
10 | class MemcacheCache implements CacheInterface |
||
11 | { |
||
12 | |||
13 | protected $cache; |
||
14 | |||
15 | /** |
||
16 | * MemcacheCache constructor |
||
17 | */ |
||
18 | public function __construct() |
||
25 | |||
26 | /** |
||
27 | * MemcacheCache destructor closes the connection |
||
28 | */ |
||
29 | public function __destruct() |
||
35 | |||
36 | /** |
||
37 | * Connect to a server |
||
38 | * @param string $host This should be the host name or IP address you want to connect to |
||
39 | * @param int $port The port number where Memcache can be accessed |
||
40 | * @param boolean $persistent If you want this connection to be persistent set to true else set to false |
||
41 | * @return $this |
||
42 | */ |
||
43 | View Code Duplication | public function connect($host, $port, $persistent = false) |
|
52 | |||
53 | /** |
||
54 | * Add a server to connection pool |
||
55 | * @param string $host This should be the host name or IP address you want to add to the Memcache pool |
||
56 | * @param int $port The port number where Memcache can be accessed |
||
57 | * @param boolean $persistent If you want this connection to be persistent set to true else set to false |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function addServer($host, $port, $persistent = true) |
||
65 | |||
66 | /** |
||
67 | * Adds a value to be stored on the server |
||
68 | * @param string $key This should be the key for the value you wish to add |
||
69 | * @param mixed $value The value you wish to be stored with the given key |
||
70 | * @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
||
71 | * @return boolean Returns true if successfully added or false on failure |
||
72 | */ |
||
73 | public function save($key, $value, $time = 0) |
||
77 | |||
78 | /** |
||
79 | * Replaces a stored value for a given key |
||
80 | * @param string $key This should be the key for the value you wish to replace |
||
81 | * @param mixed $value The new value that you wish to give to that key |
||
82 | * @param int $time How long should the value be stored for in seconds (0 = never expire) (max set value = 2592000 (30 Days)) |
||
83 | * @return boolean Returns true if successfully replaced or false on failure |
||
84 | */ |
||
85 | public function replace($key, $value, $time = 0) |
||
89 | |||
90 | /** |
||
91 | * Returns the values store for the given key |
||
92 | * @param string $key This should be the unique query key to get the value |
||
93 | * @return mixed The store value will be returned |
||
94 | */ |
||
95 | public function fetch($key) |
||
99 | |||
100 | /** |
||
101 | * Deletes a single value from the server based on the given key |
||
102 | * @param string $key This should be the key that you wish to delete the value for |
||
103 | * @return boolean Returns true on success or false on failure |
||
104 | */ |
||
105 | public function delete($key) |
||
109 | |||
110 | /** |
||
111 | * Deletes all values from the server |
||
112 | * @return boolean Returns true on success or false on failure |
||
113 | */ |
||
114 | public function deleteAll() |
||
118 | } |
||
119 |