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:
Complex classes like Local often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Local, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Local implements Adapter, |
||
|
|
|||
| 17 | StreamFactory, |
||
| 18 | ChecksumCalculator, |
||
| 19 | SizeCalculator, |
||
| 20 | MimeTypeProvider |
||
| 21 | { |
||
| 22 | protected $directory; |
||
| 23 | private $create; |
||
| 24 | private $mode; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $directory Directory where the filesystem is located |
||
| 28 | * @param bool $create Whether to create the directory if it does not |
||
| 29 | * exist (default FALSE) |
||
| 30 | * @param int $mode Mode for mkdir |
||
| 31 | * |
||
| 32 | * @throws \RuntimeException if the specified directory does not exist and |
||
| 33 | * could not be created |
||
| 34 | */ |
||
| 35 | public function __construct($directory, $create = false, $mode = 0777) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | * |
||
| 50 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 51 | * @throws \InvalidArgumentException if the directory already exists |
||
| 52 | * @throws \RuntimeException if the directory could not be created |
||
| 53 | */ |
||
| 54 | public function read($key) |
||
| 55 | { |
||
| 56 | if ($this->isDirectory($key)) { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | return file_get_contents($this->computePath($key)); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | * |
||
| 66 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 67 | * @throws \InvalidArgumentException if the directory already exists |
||
| 68 | * @throws \RuntimeException if the directory could not be created |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function write($key, $content) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | * |
||
| 81 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 82 | * @throws \InvalidArgumentException if the directory already exists |
||
| 83 | * @throws \RuntimeException if the directory could not be created |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function rename($sourceKey, $targetKey) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | public function exists($key) |
||
| 97 | { |
||
| 98 | return file_exists($this->computePath($key)); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | * |
||
| 104 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 105 | * @throws \InvalidArgumentException if the directory already exists |
||
| 106 | * @throws \RuntimeException if the directory could not be created |
||
| 107 | */ |
||
| 108 | public function keys() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | * |
||
| 136 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 137 | * @throws \InvalidArgumentException if the directory already exists |
||
| 138 | * @throws \RuntimeException if the directory could not be created |
||
| 139 | */ |
||
| 140 | public function mtime($key) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | * |
||
| 148 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 149 | * @throws \InvalidArgumentException if the directory already exists |
||
| 150 | * @throws \RuntimeException if the directory could not be created |
||
| 151 | */ |
||
| 152 | public function delete($key) |
||
| 153 | { |
||
| 154 | $file = $this->computePath($key); |
||
| 155 | |||
| 156 | if (!file_exists($file)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | return $this->doDelete($file); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $key |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | * |
||
| 168 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 169 | * @throws \InvalidArgumentException if the directory already exists |
||
| 170 | * @throws \RuntimeException if the directory could not be created |
||
| 171 | */ |
||
| 172 | public function isDirectory($key) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | * |
||
| 180 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 181 | * @throws \InvalidArgumentException if the directory already exists |
||
| 182 | * @throws \RuntimeException if the directory could not be created |
||
| 183 | */ |
||
| 184 | public function createStream($key) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | * |
||
| 192 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 193 | * @throws \InvalidArgumentException if the directory already exists |
||
| 194 | * @throws \RuntimeException if the directory could not be created |
||
| 195 | */ |
||
| 196 | public function checksum($key) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | * |
||
| 204 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 205 | * @throws \InvalidArgumentException if the directory already exists |
||
| 206 | * @throws \RuntimeException if the directory could not be created |
||
| 207 | */ |
||
| 208 | public function size($key) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | * |
||
| 216 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 217 | * @throws \InvalidArgumentException if the directory already exists |
||
| 218 | * @throws \RuntimeException if the directory could not be created |
||
| 219 | */ |
||
| 220 | public function mimeType($key) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Computes the key from the specified path. |
||
| 229 | * |
||
| 230 | * @param $path |
||
| 231 | * @return string |
||
| 232 | * |
||
| 233 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 234 | * @throws \InvalidArgumentException if the directory already exists |
||
| 235 | * @throws \RuntimeException if the directory could not be created |
||
| 236 | */ |
||
| 237 | public function computeKey($path) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Computes the path from the specified key. |
||
| 246 | * |
||
| 247 | * @param string $key The key which for to compute the path |
||
| 248 | * |
||
| 249 | * @return string A path |
||
| 250 | * |
||
| 251 | * @throws \InvalidArgumentException If the directory already exists |
||
| 252 | * @throws \OutOfBoundsException If the computed path is out of the directory |
||
| 253 | * @throws \RuntimeException If directory does not exists and cannot be created |
||
| 254 | */ |
||
| 255 | protected function computePath($key) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Normalizes the given path. |
||
| 264 | * |
||
| 265 | * @param string $path |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | * @throws \OutOfBoundsException If the computed path is out of the |
||
| 269 | * directory |
||
| 270 | */ |
||
| 271 | protected function normalizePath($path) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Ensures the specified directory exists, creates it if it does not. |
||
| 284 | * |
||
| 285 | * @param string $directory Path of the directory to test |
||
| 286 | * @param bool $create Whether to create the directory if it does |
||
| 287 | * not exist |
||
| 288 | * |
||
| 289 | * @throws \InvalidArgumentException if the directory already exists |
||
| 290 | * @throws \RuntimeException if the directory does not exists and could not |
||
| 291 | * be created |
||
| 292 | */ |
||
| 293 | protected function ensureDirectoryExists($directory, $create = false) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Creates the specified directory and its parents. |
||
| 306 | * |
||
| 307 | * @param string $directory Path of the directory to create |
||
| 308 | * |
||
| 309 | * @throws \InvalidArgumentException if the directory already exists |
||
| 310 | * @throws \RuntimeException if the directory could not be created |
||
| 311 | */ |
||
| 312 | protected function createDirectory($directory) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Removes files or directories. |
||
| 321 | * |
||
| 322 | * @see https://github.com/symfony/filesystem/blob/master/Filesystem.php#L159 |
||
| 323 | * |
||
| 324 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | private function doDelete($files) |
||
| 355 | } |
||
| 356 |