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 |
||
| 13 | class FileUpload extends File |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @param string $path |
||
| 17 | * @param string $name |
||
| 18 | * @param int $size |
||
| 19 | * @param string $mimeType |
||
| 20 | * @param null $error |
||
| 21 | */ |
||
| 22 | public function __construct(string $path, string $name, int $size = 0, string $mimeType = '', $error = null) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get file content. |
||
| 34 | * |
||
| 35 | * @return string|mixed |
||
| 36 | */ |
||
| 37 | public function read() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns the original file name. |
||
| 50 | * |
||
| 51 | * It is extracted from the request from which the file has been uploaded. |
||
| 52 | * Then it should not be considered as a safe value. |
||
| 53 | * |
||
| 54 | * @return string The original name |
||
| 55 | */ |
||
| 56 | public function nameUnsafe(): string |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns the file size. |
||
| 63 | * |
||
| 64 | * It is extracted from the request from which the file has been uploaded. |
||
| 65 | * Then it should not be considered as a safe value. |
||
| 66 | * |
||
| 67 | * @return int The file size |
||
| 68 | */ |
||
| 69 | public function sizeUnsafe(): int |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the file mime type. |
||
| 76 | * |
||
| 77 | * The client mime type is extracted from the request from which the file |
||
| 78 | * was uploaded, so it should not be considered as a safe value. |
||
| 79 | * |
||
| 80 | * @return string The mime type |
||
| 81 | */ |
||
| 82 | public function mimeTypeUnsafe(): string |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the upload error. |
||
| 89 | * |
||
| 90 | * If the upload was successful, the constant UPLOAD_ERR_OK is returned. |
||
| 91 | * Otherwise one of the other UPLOAD_ERR_XXX constants is returned. |
||
| 92 | * |
||
| 93 | * @return int The upload error |
||
| 94 | */ |
||
| 95 | public function error() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns an informative upload error message. |
||
| 102 | * |
||
| 103 | * @return string The error message regarding the specified error code |
||
| 104 | */ |
||
| 105 | public function errorMessage(): string |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the original file extension. |
||
| 126 | * |
||
| 127 | * It is extracted from the original file name that was uploaded. |
||
| 128 | * Then it should not be considered as a safe value. |
||
| 129 | * |
||
| 130 | * @return string The extension |
||
| 131 | */ |
||
| 132 | public function extensionUnsafe(): string |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns whether the file was uploaded successfully. |
||
| 139 | * |
||
| 140 | * @return bool True if the file has been uploaded with HTTP and no error occurred. |
||
| 141 | */ |
||
| 142 | public function isValid(): bool |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the maximum size of an uploaded file as configured in php.ini. |
||
| 149 | * |
||
| 150 | * @return int The maximum size of an uploaded file in bytes |
||
| 151 | */ |
||
| 152 | public function getMaxFileSize(): int |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Moves the file to a new location. |
||
| 189 | * |
||
| 190 | * @param string $directory Destination folder |
||
| 191 | * @param string $name New file name |
||
| 192 | * @return File A File object representing the new file |
||
| 193 | * @throws FileException |
||
| 194 | */ |
||
| 195 | public function move(string $directory, string $name = ''): File |
||
| 212 | |||
| 213 | private $name; |
||
| 214 | private $size; |
||
| 215 | private $mimeType; |
||
| 216 | private $error; |
||
| 217 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.