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 |
||
| 27 | class File extends AbstractCacheItemPool |
||
| 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 | 12 | public function __construct($options = array()) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * This will wipe out the entire cache's keys.... |
||
| 56 | * |
||
| 57 | * @return boolean The result of the clear operation. |
||
| 58 | * |
||
| 59 | * @since 1.0 |
||
| 60 | */ |
||
| 61 | 12 | public function clear() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Method to get a storage entry value from a key. |
||
| 87 | * |
||
| 88 | * @param string $key The storage entry identifier. |
||
| 89 | * |
||
| 90 | * @return CacheItemInterface |
||
| 91 | * |
||
| 92 | * @since 1.0 |
||
| 93 | * @throws \RuntimeException |
||
| 94 | */ |
||
| 95 | 5 | public function getItem($key) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Method to remove a storage entry for a key. |
||
| 146 | * |
||
| 147 | * @param string $key The storage entry identifier. |
||
| 148 | * |
||
| 149 | * @return boolean True on success |
||
| 150 | * |
||
| 151 | * @since 1.0 |
||
| 152 | */ |
||
| 153 | 3 | public function deleteItem($key) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Persists a cache item immediately. |
||
| 166 | * |
||
| 167 | * @param CacheItemInterface $item The cache item to save. |
||
| 168 | * |
||
| 169 | * @return bool True if the item was successfully persisted. False if there was an error. |
||
| 170 | */ |
||
| 171 | 9 | public function save(CacheItemInterface $item) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Method to determine whether a storage entry has been set for a key. |
||
| 201 | * |
||
| 202 | * @param string $key The storage entry identifier. |
||
| 203 | * |
||
| 204 | * @return boolean |
||
| 205 | * |
||
| 206 | * @since 1.0 |
||
| 207 | */ |
||
| 208 | 9 | public function hasItem($key) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Test to see if the CacheItemPoolInterface is available |
||
| 215 | * |
||
| 216 | * @return boolean True on success, false otherwise |
||
| 217 | * |
||
| 218 | * @since __DEPLOY_VERSION__ |
||
| 219 | */ |
||
| 220 | public static function isSupported() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Check that the file path is a directory and writable. |
||
| 227 | * |
||
| 228 | * @param string $filePath A file path. |
||
| 229 | * |
||
| 230 | * @return boolean The method will always return true, if it returns. |
||
| 231 | * |
||
| 232 | * @since 1.0 |
||
| 233 | * @throws \RuntimeException if the file path is invalid. |
||
| 234 | */ |
||
| 235 | 12 | private function checkFilePath($filePath) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get the full stream URI for the cache entry. |
||
| 251 | * |
||
| 252 | * @param string $key The storage entry identifier. |
||
| 253 | * |
||
| 254 | * @return string The full stream URI for the cache entry. |
||
| 255 | * |
||
| 256 | * @since 1.0 |
||
| 257 | * @throws \RuntimeException if the cache path is invalid. |
||
| 258 | */ |
||
| 259 | 9 | private function fetchStreamUri($key) |
|
| 271 | } |
||
| 272 |
If you suppress an error, we recommend checking for the error condition explicitly: