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 |
||
| 11 | class File implements CacheInterface |
||
| 12 | { |
||
| 13 | use AdapterTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $cachePath; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * File constructor. |
||
| 22 | * |
||
| 23 | * @param $path |
||
| 24 | * @param $ttl |
||
| 25 | */ |
||
| 26 | public function __construct($path, $ttl = 3600) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $key |
||
| 38 | * @param null $default |
||
| 39 | * |
||
| 40 | * @return bool|mixed|null |
||
| 41 | */ |
||
| 42 | public function get($key, $default = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $key |
||
| 78 | * @param mixed $value |
||
| 79 | * @param null $ttl |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | * |
||
| 83 | * @throws InvalidArgumentException |
||
| 84 | */ |
||
| 85 | public function set($key, $value, $ttl = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $key |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function delete($key) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Remove all cache entries. |
||
| 128 | */ |
||
| 129 | public function clear() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $key |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | * |
||
| 149 | * @throws InvalidArgumentException |
||
| 150 | */ |
||
| 151 | public function has($key) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Clean up expired cache entries. |
||
| 160 | */ |
||
| 161 | private function cleanExpired() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Creates writable directory in given path. |
||
| 173 | * |
||
| 174 | * @param $directoryPath |
||
| 175 | * |
||
| 176 | * @throws InvalidArgumentException |
||
| 177 | */ |
||
| 178 | private function createDirectory($directoryPath) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * For a given cache key, obtain the absolute file path. |
||
| 196 | * |
||
| 197 | * @param string $key |
||
| 198 | * |
||
| 199 | * @return string absolute path to cache file |
||
| 200 | */ |
||
| 201 | private function getPath($key) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return \RecursiveIteratorIterator |
||
| 210 | */ |
||
| 211 | private function getCacheFolderIterator() |
||
| 218 | } |
||
| 219 |
If you suppress an error, we recommend checking for the error condition explicitly: