Completed
Pull Request — master (#831)
by
unknown
03:34
created

RenameController::getRename()   B

Complexity

Conditions 11
Paths 28

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 32
nc 28
nop 0
dl 0
loc 54
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Xuandung38\LaravelFilemanager\Controllers;
4
5
use Xuandung38\LaravelFilemanager\Events\ImageIsRenaming;
6
use Xuandung38\LaravelFilemanager\Events\ImageWasRenamed;
7
use Xuandung38\LaravelFilemanager\Events\FolderIsRenaming;
8
use Xuandung38\LaravelFilemanager\Events\FolderWasRenamed;
9
10
class RenameController extends LfmController
11
{
12
    public function getRename()
13
    {
14
        $old_name = $this->helper->input('file');
0 ignored issues
show
Bug introduced by
The method input() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        /** @scrutinizer ignore-call */ 
15
        $old_name = $this->helper->input('file');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property helper does not exist on Xuandung38\LaravelFilema...ollers\RenameController. Since you implemented __get, consider adding a @property annotation.
Loading history...
15
        $new_name = $this->helper->input('new_name');
16
17
        $old_file = $this->lfm->pretty($old_name);
0 ignored issues
show
Bug introduced by
The method pretty() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        /** @scrutinizer ignore-call */ 
18
        $old_file = $this->lfm->pretty($old_name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property lfm does not exist on Xuandung38\LaravelFilema...ollers\RenameController. Since you implemented __get, consider adding a @property annotation.
Loading history...
18
19
        $is_directory = $old_file->isDirectory();
20
21
        if (empty($new_name)) {
22
            if ($is_directory) {
23
                return parent::error('folder-name');
24
            } else {
25
                return parent::error('file-name');
26
            }
27
        }
28
29
        if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        if (/** @scrutinizer ignore-call */ config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
Loading history...
30
            return parent::error('folder-alnum');
31
        // return parent::error('file-alnum');
32
        } elseif ($this->lfm->setName($new_name)->exists()) {
33
            return parent::error('rename');
34
        }
35
36
        if (! $is_directory) {
37
            $extension = $old_file->extension();
38
            if ($extension) {
39
                $new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
40
            }
41
        }
42
43
        $new_file = $this->lfm->setName($new_name)->path('absolute');
44
45
        if ($is_directory) {
46
            event(new FolderIsRenaming($old_file->path(), $new_file));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            /** @scrutinizer ignore-call */ 
47
            event(new FolderIsRenaming($old_file->path(), $new_file));
Loading history...
47
        } else {
48
            event(new ImageIsRenaming($old_file->path(), $new_file));
49
        }
50
51
        if ($old_file->hasThumb()) {
52
            $this->lfm->setName($old_name)->thumb()
53
                ->move($this->lfm->setName($new_name)->thumb());
54
        }
55
56
        $this->lfm->setName($old_name)
57
            ->move($this->lfm->setName($new_name));
58
59
        if ($is_directory) {
60
            event(new FolderWasRenamed($old_file->path(), $new_file));
61
        } else {
62
            event(new ImageWasRenamed($old_file->path(), $new_file));
63
        }
64
65
        return parent::$success_response;
66
    }
67
}
68