Conditions | 12 |
Paths | 56 |
Total Lines | 60 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 | $file = $this->lfm->setName($old_name); |
||
19 | |||
20 | if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) { |
||
21 | abort(404); |
||
22 | } |
||
23 | |||
24 | $old_file = $this->lfm->pretty($old_name); |
||
25 | |||
26 | $is_directory = $old_file->isDirectory(); |
||
27 | |||
28 | if (empty($new_name)) { |
||
29 | if ($is_directory) { |
||
30 | return parent::error('folder-name'); |
||
31 | } else { |
||
32 | return parent::error('file-name'); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) { |
||
37 | return parent::error('folder-alnum'); |
||
38 | // return parent::error('file-alnum'); |
||
39 | } elseif ($this->lfm->setName($new_name)->exists()) { |
||
40 | return parent::error('rename'); |
||
41 | } |
||
42 | |||
43 | if (! $is_directory) { |
||
44 | $extension = $old_file->extension(); |
||
45 | if ($extension) { |
||
46 | $new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | $new_file = $this->lfm->setName($new_name)->path('absolute'); |
||
51 | |||
52 | if ($is_directory) { |
||
53 | event(new FolderIsRenaming($old_file->path(), $new_file)); |
||
54 | } else { |
||
55 | event(new ImageIsRenaming($old_file->path(), $new_file)); |
||
56 | } |
||
57 | |||
58 | if ($old_file->hasThumb()) { |
||
59 | $this->lfm->setName($old_name)->thumb() |
||
60 | ->move($this->lfm->setName($new_name)->thumb()); |
||
61 | } |
||
62 | |||
63 | $this->lfm->setName($old_name) |
||
64 | ->move($this->lfm->setName($new_name)); |
||
65 | |||
66 | if ($is_directory) { |
||
67 | event(new FolderWasRenamed($old_file->path(), $new_file)); |
||
68 | } else { |
||
69 | event(new ImageWasRenamed($old_file->path(), $new_file)); |
||
70 | } |
||
71 | |||
72 | return parent::$success_response; |
||
73 | } |
||
75 |