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 |
||
| 28 | class Quota extends Wrapper { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int $quota |
||
| 32 | */ |
||
| 33 | protected $quota; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string $sizeRoot |
||
| 37 | */ |
||
| 38 | protected $sizeRoot; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param array $parameters |
||
| 42 | */ |
||
| 43 | 96 | public function __construct($parameters) { |
|
| 44 | 96 | $this->storage = $parameters['storage']; |
|
| 45 | 96 | $this->quota = $parameters['quota']; |
|
| 46 | 96 | $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 47 | 96 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return int quota value |
||
| 51 | */ |
||
| 52 | 3 | public function getQuota() { |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $path |
||
| 58 | * @param \OC\Files\Storage\Storage $storage |
||
| 59 | */ |
||
| 60 | 78 | protected function getSize($path, $storage = null) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Get free space as limited by the quota |
||
| 76 | * |
||
| 77 | * @param string $path |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | 78 | public function free_space($path) { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * see http://php.net/manual/en/function.file_put_contents.php |
||
| 103 | * |
||
| 104 | * @param string $path |
||
| 105 | * @param string $data |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 62 | public function file_put_contents($path, $data) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * see http://php.net/manual/en/function.copy.php |
||
| 119 | * |
||
| 120 | * @param string $source |
||
| 121 | * @param string $target |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 21 | View Code Duplication | public function copy($source, $target) { |
| 132 | |||
| 133 | /** |
||
| 134 | * see http://php.net/manual/en/function.fopen.php |
||
| 135 | * |
||
| 136 | * @param string $path |
||
| 137 | * @param string $mode |
||
| 138 | * @return resource |
||
| 139 | */ |
||
| 140 | 8 | public function fopen($path, $mode) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Checks whether the given path is a part file |
||
| 158 | 8 | * |
|
| 159 | 8 | * @param string $path Path that may identify a .part file |
|
| 160 | 8 | * @return string File path without .part extension |
|
| 161 | 8 | * @note this is needed for reusing keys |
|
| 162 | */ |
||
| 163 | private function isPartFile($path) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param \OCP\Files\Storage $sourceStorage |
||
| 171 | * @param string $sourceInternalPath |
||
| 172 | * @param string $targetInternalPath |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param \OCP\Files\Storage $sourceStorage |
||
| 186 | * @param string $sourceInternalPath |
||
| 187 | * @param string $targetInternalPath |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 198 | } |
||
| 199 |