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 | 2 | 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 | 1 | 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 | 2 | 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) |
|
| 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 | 1 | public function save(CacheItemInterface $item) |
|
| 171 | { |
||
| 172 | 1 | $fileName = $this->fetchStreamUri($item->getKey()); |
|
| 173 | 1 | $filePath = pathinfo($fileName, PATHINFO_DIRNAME); |
|
| 174 | |||
| 175 | 1 | if (!is_dir($filePath)) |
|
| 176 | 1 | { |
|
| 177 | 1 | mkdir($filePath, 0770, true); |
|
| 178 | 1 | } |
|
| 179 | |||
| 180 | 1 | if ($item instanceof HasExpirationDateInterface) |
|
| 181 | 1 | { |
|
| 182 | $contents = serialize(array($item->get(), time() + $this->convertItemExpiryToSeconds($item))); |
||
| 183 | } |
||
| 184 | else |
||
| 185 | { |
||
| 186 | 1 | $contents = serialize(array($item->get(), null)); |
|
| 187 | } |
||
| 188 | |||
| 189 | 1 | $success = (bool) file_put_contents( |
|
| 190 | 1 | $fileName, |
|
| 191 | 1 | $contents, |
|
| 192 | 1 | ($this->options['file.locking'] ? LOCK_EX : null) |
|
| 193 | 1 | ); |
|
| 194 | |||
| 195 | 1 | return $success; |
|
| 196 | } |
||
| 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 | 1 | 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 | 3 | private function checkFilePath($filePath) |
|
| 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 | 1 | private function fetchStreamUri($key) |
|
| 270 | } |
||
| 271 |
If you suppress an error, we recommend checking for the error condition explicitly: