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 |
||
| 18 | class UploadManager extends Component |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Throw exception when trying to overwrite existing file. |
||
| 22 | */ |
||
| 23 | const STRATEGY_KEEP = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Overwrite existing file silently. |
||
| 27 | */ |
||
| 28 | const STRATEGY_OVERWRITE = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Rename new file if file with same name exists. |
||
| 32 | */ |
||
| 33 | const STRATEGY_RENAME = 2; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Path to upload folder. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $uploadDir = '@webroot/upload'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * URL to upload folder. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $uploadUrl = '@web/upload'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generate partition name based on file name. |
||
| 51 | * |
||
| 52 | * @param string $name |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 20 | protected function getPartitionName($name) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Add partition folder to given path. |
||
| 63 | * |
||
| 64 | * @param string $path |
||
| 65 | * @param string $name |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 20 | protected function getPartitionedPath($path, $name) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get prefixed path. |
||
| 79 | * |
||
| 80 | * @param string $path |
||
| 81 | * @param string $prefix |
||
| 82 | * |
||
| 83 | * @return string |
||
|
|
|||
| 84 | */ |
||
| 85 | 36 | protected function getPrefixedPath($path, $prefix) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Add index to file name. |
||
| 98 | * |
||
| 99 | * @param string $name |
||
| 100 | * @param int $index |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | 4 | protected function addIndexToName($name, $index) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Pick up file name according to overwrite strategy and create path. |
||
| 117 | * |
||
| 118 | * @throws InvalidParamException when file cannot be created. |
||
| 119 | * |
||
| 120 | * @param string $path |
||
| 121 | * @param string $name |
||
| 122 | * @param int $overwriteStrategy |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 16 | protected function createFilePath($path, $name, $overwriteStrategy) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get relative URL for relative path. |
||
| 165 | * |
||
| 166 | * @param string $path |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 4 | public function getUrl($path) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Get absolute path for relative path. |
||
| 177 | * |
||
| 178 | * @param string $path |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | 32 | public function getAbsolutePath($path) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Create given folder tree in upload folder. |
||
| 189 | * |
||
| 190 | * Returns absolute path of given path. |
||
| 191 | * |
||
| 192 | * @throws InvalidParamException when file cannot be created. |
||
| 193 | * |
||
| 194 | * @param string $path |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 24 | public function createPath($path) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Create folder tree appended with partition folder in upload folder. |
||
| 213 | * |
||
| 214 | * Partition folder name depends on given file name. |
||
| 215 | * |
||
| 216 | * Returns absolute path of given path. |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * @param string $name |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 4 | public function createPartitionedPath($path, $name) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Whether file with given relative path exists. |
||
| 232 | * |
||
| 233 | * @param string $filePath |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 4 | public function exists($filePath) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Save data stored in $content as file to $path/$name in upload folder. |
||
| 246 | * |
||
| 247 | * Returns relative path with partition folder. |
||
| 248 | * |
||
| 249 | * @param string $path |
||
| 250 | * @param string $name |
||
| 251 | * @param string $content |
||
| 252 | * @param int[optional] $overwriteStrategy |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 4 | View Code Duplication | public function saveContent($path, $name, $content, $overwriteStrategy = self::STRATEGY_KEEP) |
| 266 | |||
| 267 | /** |
||
| 268 | * Save $upload file to $path in upload folder. |
||
| 269 | * |
||
| 270 | * Returns relative path with partition folder. |
||
| 271 | * |
||
| 272 | * @param string $path |
||
| 273 | * @param UploadedFile $upload |
||
| 274 | * @param int[optional] $overwriteStrategy |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 4 | View Code Duplication | public function saveUpload($path, UploadedFile $upload, $overwriteStrategy = self::STRATEGY_KEEP) |
| 287 | |||
| 288 | /** |
||
| 289 | * Move or copy file. |
||
| 290 | * |
||
| 291 | * @param string $path |
||
| 292 | * @param string $absoluteFilePath |
||
| 293 | * @param int[optional] $overwriteStrategy |
||
| 294 | * @param string $function |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | 8 | View Code Duplication | protected function saveFileInternal($path, $absoluteFilePath, $overwriteStrategy, $function) |
| 307 | |||
| 308 | /** |
||
| 309 | * Save $absoluteFilePath file to $path in upload folder. |
||
| 310 | * |
||
| 311 | * Returns relative path with partition folder. |
||
| 312 | * |
||
| 313 | * @param string $path |
||
| 314 | * @param string $absoluteFilePath |
||
| 315 | * @param int[optional] $overwriteStrategy |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | 8 | public function saveFile($path, $absoluteFilePath, $overwriteStrategy = self::STRATEGY_KEEP) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Move $absoluteFilePath file to $path in upload folder. |
||
| 326 | * |
||
| 327 | * Returns relative path with partition folder. |
||
| 328 | * |
||
| 329 | * @param string $path |
||
| 330 | * @param string $absoluteFilePath |
||
| 331 | * @param int[optional] $overwriteStrategy |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 4 | public function moveFile($path, $absoluteFilePath, $overwriteStrategy = self::STRATEGY_KEEP) |
|
| 339 | } |
||
| 340 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.