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 |
||
21 | class Memcached extends Cache |
||
22 | { |
||
23 | /** |
||
24 | * @var \Memcached The memcached driver, initialize to false to avoid having to repeated run isset before using it.. |
||
25 | * @since 1.0 |
||
26 | */ |
||
27 | private $driver = false; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * @throws \RuntimeException |
||
36 | */ |
||
37 | 3 | public function __construct($options = array()) |
|
58 | |||
59 | /** |
||
60 | * This will wipe out the entire cache's keys |
||
61 | * |
||
62 | * @return boolean The result of the clear operation. |
||
63 | * |
||
64 | * @since 1.0 |
||
65 | */ |
||
66 | 2 | public function clear() |
|
72 | |||
73 | /** |
||
74 | * Method to get a storage entry value from a key. |
||
75 | * |
||
76 | * @param string $key The storage entry identifier. |
||
77 | * |
||
78 | * @return CacheItemInterface |
||
79 | * |
||
80 | * @since 1.0 |
||
81 | */ |
||
82 | 2 | View Code Duplication | public function getItem($key) |
96 | |||
97 | /** |
||
98 | * Method to remove a storage entry for a key. |
||
99 | * |
||
100 | * @param string $key The storage entry identifier. |
||
101 | * |
||
102 | * @return boolean |
||
103 | * |
||
104 | * @since 1.0 |
||
105 | */ |
||
106 | 1 | public function deleteItem($key) |
|
124 | |||
125 | /** |
||
126 | * Persists a cache item immediately. |
||
127 | * |
||
128 | * @param CacheItemInterface $item The cache item to save. |
||
129 | * |
||
130 | * @return static The invoked object. |
||
131 | */ |
||
132 | 2 | public function save(CacheItemInterface $item) |
|
133 | { |
||
134 | 2 | $this->connect(); |
|
135 | |||
136 | 2 | if ($item instanceof HasExpirationDateInterface) |
|
137 | 2 | { |
|
138 | 1 | $ttl = $this->convertItemExpiryToSeconds($item); |
|
139 | 1 | } |
|
140 | else |
||
141 | { |
||
142 | 1 | $ttl = 0; |
|
143 | } |
||
144 | |||
145 | 2 | $this->driver->set($item->getKey(), $item->get(), $ttl); |
|
146 | |||
147 | 2 | return (bool) ($this->driver->getResultCode() == \Memcached::RES_SUCCESS); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Method to determine whether a storage entry has been set for a key. |
||
152 | * |
||
153 | * @param string $key The storage entry identifier. |
||
154 | * |
||
155 | * @return boolean |
||
156 | * |
||
157 | * @since 1.0 |
||
158 | */ |
||
159 | 1 | public function hasItem($key) |
|
167 | |||
168 | /** |
||
169 | * Test to see if the CacheItemPoolInterface is available |
||
170 | * |
||
171 | * @return boolean True on success, false otherwise |
||
172 | * |
||
173 | * @since __DEPLOY_VERSION__ |
||
174 | */ |
||
175 | 1 | public static function isSupported() |
|
176 | { |
||
177 | /* |
||
178 | * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded. |
||
179 | * If the class is there, we can assume it works. |
||
180 | */ |
||
181 | 1 | return (class_exists('Memcached')); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Connect to the Memcached servers if the connection does not already exist. |
||
186 | * |
||
187 | * @return void |
||
188 | * |
||
189 | * @since 1.0 |
||
190 | */ |
||
191 | 4 | private function connect() |
|
225 | } |
||
226 |
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.