| Conditions | 17 |
| Paths | 107 |
| Total Lines | 70 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 13 | public function getRename() |
||
| 14 | { |
||
| 15 | $old_name = $this->helper->input('file'); |
||
| 16 | $new_name = $this->helper->input('new_name'); |
||
| 17 | |||
| 18 | $old_file = $this->lfm->pretty($old_name); |
||
| 19 | |||
| 20 | $is_directory = $old_file->isDirectory(); |
||
| 21 | |||
| 22 | if (empty($new_name)) { |
||
| 23 | if ($is_directory) { |
||
| 24 | return parent::error('folder-name'); |
||
| 25 | } else { |
||
| 26 | return parent::error('file-name'); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | if ($is_directory && config('lfm.alphanumeric_directory')) { |
||
| 31 | if (config('lfm.convert_to_alphanumeric')) { |
||
| 32 | $new_name = Str::slug($new_name); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (preg_match('/[^\w\-_]/i', $new_name)) { |
||
| 36 | return parent::error('folder-alnum'); |
||
| 37 | } |
||
| 38 | }else if(!$is_directory && config('lfm.alphanumeric_filename')){ |
||
| 39 | // Remove extension for checks to alphanum characters |
||
| 40 | $extension = $old_file->extension(); |
||
| 41 | if ($extension) { |
||
| 42 | $new_name = str_replace('.' . $extension, '', $new_name); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (config('lfm.convert_to_alphanumeric')) { |
||
| 46 | $new_name = Str::slug($new_name); |
||
| 47 | } |
||
| 48 | |||
| 49 | if (preg_match('/[^\w\-_]/i', $new_name)) { |
||
| 50 | return parent::error('file-alnum'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $new_name .= ($extension) ? '.' . $extension : null; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($this->lfm->setName($new_name)->exists()) { |
||
| 57 | return parent::error('rename'); |
||
| 58 | } |
||
| 59 | |||
| 60 | $new_file = $this->lfm->setName($new_name)->path('absolute'); |
||
| 61 | |||
| 62 | if ($is_directory) { |
||
| 63 | event(new FolderIsRenaming($old_file->path(), $new_file)); |
||
| 64 | } else { |
||
| 65 | event(new ImageIsRenaming($old_file->path(), $new_file)); |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($old_file->hasThumb()) { |
||
| 69 | $this->lfm->setName($old_name)->thumb() |
||
| 70 | ->move($this->lfm->setName($new_name)->thumb()); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->lfm->setName($old_name) |
||
| 74 | ->move($this->lfm->setName($new_name)); |
||
| 75 | |||
| 76 | if ($is_directory) { |
||
| 77 | event(new FolderWasRenamed($old_file->path(), $new_file)); |
||
| 78 | } else { |
||
| 79 | event(new ImageWasRenamed($old_file->path(), $new_file)); |
||
| 80 | } |
||
| 81 | |||
| 82 | return parent::$success_response; |
||
| 83 | } |
||
| 85 |