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 |
||
26 | class File extends Cache |
||
27 | { |
||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param mixed $options An options array, or an object that implements \ArrayAccess |
||
32 | * |
||
33 | * @since 1.0 |
||
34 | * @throws \RuntimeException |
||
35 | */ |
||
36 | 10 | public function __construct($options = array()) |
|
52 | |||
53 | /** |
||
54 | * This will wipe out the entire cache's keys.... |
||
55 | * |
||
56 | * @return boolean The result of the clear operation. |
||
57 | * |
||
58 | * @since 1.0 |
||
59 | */ |
||
60 | 10 | public function clear() |
|
83 | |||
84 | /** |
||
85 | * Method to get a storage entry value from a key. |
||
86 | * |
||
87 | * @param string $key The storage entry identifier. |
||
88 | * |
||
89 | * @return CacheItemInterface |
||
90 | * |
||
91 | * @since 1.0 |
||
92 | * @throws \RuntimeException |
||
93 | */ |
||
94 | 5 | public function getItem($key) |
|
142 | |||
143 | /** |
||
144 | * Method to remove a storage entry for a key. |
||
145 | * |
||
146 | * @param string $key The storage entry identifier. |
||
147 | * |
||
148 | * @return boolean True on success |
||
149 | * |
||
150 | * @since 1.0 |
||
151 | */ |
||
152 | 3 | public function deleteItem($key) |
|
153 | { |
||
154 | 3 | if ($this->hasItem($key)) |
|
155 | 3 | { |
|
156 | 3 | return (bool) @unlink($this->fetchStreamUri($key)); |
|
157 | } |
||
158 | |||
159 | // If the item doesn't exist, no error |
||
160 | 2 | return true; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Persists a cache item immediately. |
||
165 | * |
||
166 | * @param CacheItemInterface $item The cache item to save. |
||
167 | * |
||
168 | * @return bool True if the item was successfully persisted. False if there was an error. |
||
169 | */ |
||
170 | 8 | public function save(CacheItemInterface $item) |
|
197 | |||
198 | /** |
||
199 | * Method to determine whether a storage entry has been set for a key. |
||
200 | * |
||
201 | * @param string $key The storage entry identifier. |
||
202 | * |
||
203 | * @return boolean |
||
204 | * |
||
205 | * @since 1.0 |
||
206 | */ |
||
207 | 8 | public function hasItem($key) |
|
211 | |||
212 | /** |
||
213 | * Test to see if the CacheItemPoolInterface is available |
||
214 | * |
||
215 | * @return boolean True on success, false otherwise |
||
216 | * |
||
217 | * @since __DEPLOY_VERSION__ |
||
218 | */ |
||
219 | public static function isSupported() |
||
223 | |||
224 | /** |
||
225 | * Check that the file path is a directory and writable. |
||
226 | * |
||
227 | * @param string $filePath A file path. |
||
228 | * |
||
229 | * @return boolean The method will always return true, if it returns. |
||
230 | * |
||
231 | * @since 1.0 |
||
232 | * @throws \RuntimeException if the file path is invalid. |
||
233 | */ |
||
234 | 10 | private function checkFilePath($filePath) |
|
235 | { |
||
236 | 10 | if (!is_dir($filePath)) |
|
237 | 10 | { |
|
238 | throw new RuntimeException(sprintf('The base cache path `%s` does not exist.', $filePath)); |
||
239 | } |
||
240 | 10 | elseif (!is_writable($filePath)) |
|
241 | { |
||
242 | throw new RuntimeException(sprintf('The base cache path `%s` is not writable.', $filePath)); |
||
243 | } |
||
244 | |||
245 | 10 | return true; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get the full stream URI for the cache entry. |
||
250 | * |
||
251 | * @param string $key The storage entry identifier. |
||
252 | * |
||
253 | * @return string The full stream URI for the cache entry. |
||
254 | * |
||
255 | * @since 1.0 |
||
256 | * @throws \RuntimeException if the cache path is invalid. |
||
257 | */ |
||
258 | 8 | private function fetchStreamUri($key) |
|
270 | } |
||
271 |
If you suppress an error, we recommend checking for the error condition explicitly: