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 |
||
| 36 | class CacheJail extends CacheWrapper { |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $root; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param \OCP\Files\Cache\ICache $cache |
||
| 44 | * @param string $root |
||
| 45 | */ |
||
| 46 | public function __construct($cache, $root) { |
||
| 50 | |||
| 51 | protected function getSourcePath($path) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $path |
||
| 61 | * @return null|string the jailed path or null if the path is outside the jail |
||
| 62 | */ |
||
| 63 | protected function getJailedPath($path) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param ICacheEntry|array $entry |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | protected function formatCacheEntry($entry) { |
||
| 87 | |||
| 88 | protected function filterCacheEntry($entry) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * get the stored metadata of a file or folder |
||
| 95 | * |
||
| 96 | * @param string /int $file |
||
| 97 | * @return ICacheEntry|false |
||
| 98 | */ |
||
| 99 | public function get($file) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * insert meta data for a new file or folder |
||
| 108 | * |
||
| 109 | * @param string $file |
||
| 110 | * @param array $data |
||
| 111 | * |
||
| 112 | * @return int file id |
||
| 113 | * @throws \RuntimeException |
||
| 114 | */ |
||
| 115 | public function insert($file, array $data) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * update the metadata in the cache |
||
| 121 | * |
||
| 122 | * @param int $id |
||
| 123 | * @param array $data |
||
| 124 | */ |
||
| 125 | public function update($id, array $data) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * get the file id for a file |
||
| 131 | * |
||
| 132 | * @param string $file |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public function getId($file) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * get the id of the parent folder of a file |
||
| 141 | * |
||
| 142 | * @param string $file |
||
| 143 | * @return int |
||
| 144 | */ |
||
| 145 | public function getParentId($file) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * check if a file is available in the cache |
||
| 151 | * |
||
| 152 | * @param string $file |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function inCache($file) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * remove a file or folder from the cache |
||
| 161 | * |
||
| 162 | * @param string $file |
||
| 163 | */ |
||
| 164 | public function remove($file) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Move a file or folder in the cache |
||
| 170 | * |
||
| 171 | * @param string $source |
||
| 172 | * @param string $target |
||
| 173 | */ |
||
| 174 | public function move($source, $target) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the storage id and path needed for a move |
||
| 180 | * |
||
| 181 | * @param string $path |
||
| 182 | * @return array [$storageId, $internalPath] |
||
| 183 | */ |
||
| 184 | protected function getMoveInfo($path) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * remove all entries for files that are stored on the storage from the cache |
||
| 190 | */ |
||
| 191 | public function clear() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $file |
||
| 197 | * |
||
| 198 | * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
||
| 199 | */ |
||
| 200 | public function getStatus($file) { |
||
| 203 | |||
| 204 | private function formatSearchResults($results) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * search for files matching $pattern |
||
| 212 | * |
||
| 213 | * @param string $pattern |
||
| 214 | * @return array an array of file data |
||
| 215 | */ |
||
| 216 | public function search($pattern) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * search for files by mimetype |
||
| 223 | * |
||
| 224 | * @param string $mimetype |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function searchByMime($mimetype) { |
||
| 231 | |||
| 232 | public function searchQuery(ISearchQuery $query) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * search for files by mimetype |
||
| 239 | * |
||
| 240 | * @param string|int $tag name or tag id |
||
| 241 | * @param string $userId owner of the tags |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function searchByTag($tag, $userId) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * update the folder size and the size of all parent folders |
||
| 251 | * |
||
| 252 | * @param string|boolean $path |
||
| 253 | * @param array $data (optional) meta data of the folder |
||
| 254 | */ |
||
| 255 | public function correctFolderSize($path, $data = null) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * get the size of a folder and set it in the cache |
||
| 263 | * |
||
| 264 | * @param string $path |
||
| 265 | * @param array $entry (optional) meta data of the folder |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function calculateFolderSize($path, $entry = null) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * get all file ids on the files on the storage |
||
| 279 | * |
||
| 280 | * @return int[] |
||
| 281 | */ |
||
| 282 | public function getAll() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * find a folder in the cache which has not been fully scanned |
||
| 289 | * |
||
| 290 | * If multiply incomplete folders are in the cache, the one with the highest id will be returned, |
||
| 291 | * use the one with the highest id gives the best result with the background scanner, since that is most |
||
| 292 | * likely the folder where we stopped scanning previously |
||
| 293 | * |
||
| 294 | * @return string|bool the path of the folder or false when no folder matched |
||
| 295 | */ |
||
| 296 | public function getIncomplete() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * get the path of a file on this storage by it's id |
||
| 303 | * |
||
| 304 | * @param int $id |
||
| 305 | * @return string|null |
||
| 306 | */ |
||
| 307 | public function getPathById($id) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Move a file or folder in the cache |
||
| 314 | * |
||
| 315 | * Note that this should make sure the entries are removed from the source cache |
||
| 316 | * |
||
| 317 | * @param \OCP\Files\Cache\ICache $sourceCache |
||
| 318 | * @param string $sourcePath |
||
| 319 | * @param string $targetPath |
||
| 320 | */ |
||
| 321 | public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { |
||
| 327 | } |
||
| 328 |