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 |
||
| 37 | class MountPoint implements IMountPoint { |
||
| 38 | /** |
||
| 39 | * @var \OC\Files\Storage\Storage $storage |
||
| 40 | */ |
||
| 41 | protected $storage = null; |
||
| 42 | protected $class; |
||
| 43 | protected $storageId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Configuration options for the storage backend |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $arguments = array(); |
||
| 51 | protected $mountPoint; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Mount specific options |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $mountOptions = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \OC\Files\Storage\StorageFactory $loader |
||
| 62 | */ |
||
| 63 | private $loader; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Specified whether the storage is invalid after failing to |
||
| 67 | * instantiate it. |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | private $invalidStorage = false; |
||
| 72 | |||
| 73 | /** @var int|null */ |
||
| 74 | protected $mountId; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string|\OC\Files\Storage\Storage $storage |
||
| 78 | * @param string $mountpoint |
||
| 79 | * @param array $arguments (optional) configuration for the storage backend |
||
| 80 | * @param \OCP\Files\Storage\IStorageFactory $loader |
||
| 81 | * @param array $mountOptions mount specific options |
||
| 82 | * @param int|null $mountId |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
||
| 86 | if (is_null($arguments)) { |
||
| 87 | $arguments = array(); |
||
| 88 | } |
||
| 89 | if (is_null($loader)) { |
||
| 90 | $this->loader = new StorageFactory(); |
||
| 91 | } else { |
||
| 92 | $this->loader = $loader; |
||
|
|
|||
| 93 | } |
||
| 94 | |||
| 95 | if (!is_null($mountOptions)) { |
||
| 96 | $this->mountOptions = $mountOptions; |
||
| 97 | } |
||
| 98 | |||
| 99 | $mountpoint = $this->formatPath($mountpoint); |
||
| 100 | $this->mountPoint = $mountpoint; |
||
| 101 | if ($storage instanceof Storage) { |
||
| 102 | $this->class = get_class($storage); |
||
| 103 | $this->storage = $this->loader->wrap($this, $storage); |
||
| 104 | } else { |
||
| 105 | // Update old classes to new namespace |
||
| 106 | if (strpos($storage, 'OC_Filestorage_') !== false) { |
||
| 107 | $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
||
| 108 | } |
||
| 109 | $this->class = $storage; |
||
| 110 | $this->arguments = $arguments; |
||
| 111 | } |
||
| 112 | $this->mountId = $mountId; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * get complete path to the mount point, relative to data/ |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getMountPoint() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the mount point path, relative to data/ |
||
| 126 | * |
||
| 127 | * @param string $mountPoint new mount point |
||
| 128 | */ |
||
| 129 | public function setMountPoint($mountPoint) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * create the storage that is mounted |
||
| 135 | * |
||
| 136 | * @return \OC\Files\Storage\Storage |
||
| 137 | */ |
||
| 138 | private function createStorage() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return \OC\Files\Storage\Storage |
||
| 165 | */ |
||
| 166 | public function getStorage() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getStorageId() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return int |
||
| 196 | */ |
||
| 197 | public function getNumericStorageId() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $path |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getInternalPath($path) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $path |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | View Code Duplication | private function formatPath($path) { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param callable $wrapper |
||
| 230 | */ |
||
| 231 | public function wrapStorage($wrapper) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get a mount option |
||
| 241 | * |
||
| 242 | * @param string $name Name of the mount option to get |
||
| 243 | * @param mixed $default Default value for the mount option |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | public function getOption($name, $default) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get all options for the mount |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function getOptions() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get the file id of the root of the storage |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public function getStorageRootId() { |
||
| 267 | |||
| 268 | public function getMountId() { |
||
| 271 | } |
||
| 272 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.