| Conditions | 13 |
| Paths | 20 |
| Total Lines | 65 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 158 | private function uploadFile(UploadedFile $file, string $uploadDirectory, string $uploadFilenameBehavior): array |
||
| 159 | { |
||
| 160 | $isFileExists = file_exists(public_path($uploadDirectory).DIRECTORY_SEPARATOR.$file->getClientOriginalName()); |
||
| 161 | $uploadFileName = $file->getClientOriginalName(); |
||
| 162 | |||
| 163 | $filenameWithoutExtensions = substr($file->getClientOriginalName(), 0, strrpos($file->getClientOriginalName(), '.')); |
||
| 164 | |||
| 165 | //Варианты |
||
| 166 | switch ($uploadFilenameBehavior) { |
||
| 167 | case 'UPLOAD_ORIGINAL_ALERT': |
||
| 168 | if ($isFileExists) { |
||
| 169 | $result['uploaded'] = 0; |
||
| 170 | $result['error']['message'] = 'Файл с таким именем уже существует. Измените имя файла и попробуйте еще раз'; |
||
| 171 | $uploadFileName = false; |
||
| 172 | } |
||
| 173 | break; |
||
| 174 | case 'UPLOAD_ORIGINAL_ADD_HASH': |
||
| 175 | if ($isFileExists) { |
||
| 176 | $uploadFileName = $filenameWithoutExtensions |
||
| 177 | .'_'.md5(time().$filenameWithoutExtensions) |
||
| 178 | .'.'.$file->getClientOriginalExtension(); |
||
| 179 | $result['error']['message'] = "Файл с таким именем уже существовал. Загружаемый файл был переименован в '{$uploadFileName}'"; |
||
| 180 | } |
||
| 181 | break; |
||
| 182 | case 'UPLOAD_ORIGINAL_ADD_INCREMENT': |
||
| 183 | if ($isFileExists) { |
||
| 184 | $index = 1; |
||
| 185 | $uploadFileName = $filenameWithoutExtensions.'_'.$index.'.'.$file->getClientOriginalExtension(); |
||
| 186 | while ( |
||
| 187 | file_exists(public_path($uploadDirectory).DIRECTORY_SEPARATOR.$uploadFileName) |
||
| 188 | and |
||
| 189 | $index < $this->uploadFilenameIncrementMax |
||
| 190 | ) { |
||
| 191 | $index++; |
||
| 192 | $uploadFileName = $filenameWithoutExtensions.'_'.$index.'.'.$file->getClientOriginalExtension(); |
||
| 193 | } |
||
| 194 | $result['error']['message'] = "Файл с таким именем уже существовал. Загружаемый файл был переименован в '{$uploadFileName}'"; |
||
| 195 | if ($index == $this->uploadFilenameIncrementMax) { |
||
| 196 | $uploadFileName = false; |
||
| 197 | $result['uploaded'] = 0; |
||
| 198 | $result['error']['message'] = 'Файл с таким именем уже существовал. Имя подобрать не удалось. Переименуйте файл и попробуйте еще раз'; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | break; |
||
| 202 | case 'UPLOAD_ORIGINAL_REWRITE': |
||
| 203 | if ($isFileExists) { |
||
| 204 | $result['error']['message'] = 'Файл с таким именем уже существовал и был перезаписан'; |
||
| 205 | } |
||
| 206 | break; |
||
| 207 | default: |
||
| 208 | //UPLOAD_HASH |
||
| 209 | $uploadFileName = md5(time().$filenameWithoutExtensions).'.'.$file->getClientOriginalExtension(); |
||
| 210 | $result['error']['message'] = "Файл был переименован в '{$uploadFileName}'"; |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($uploadFileName) { |
||
| 215 | $file->move(public_path($uploadDirectory), $uploadFileName); |
||
| 216 | |||
| 217 | $result['uploaded'] = 1; |
||
| 218 | $result['url'] = asset($uploadDirectory.'/'.$uploadFileName); |
||
| 219 | $result['fileName'] = $uploadFileName; |
||
| 220 | } |
||
| 221 | |||
| 222 | return $result; |
||
| 223 | } |
||
| 225 |