Test Failed
Pull Request — master (#1193)
by
unknown
08:05
created

ItemsController::getKeywordFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Facades\Storage;
6
use UniSharp\LaravelFilemanager\Events\FileIsMoving;
7
use UniSharp\LaravelFilemanager\Events\FileWasMoving;
8
use UniSharp\LaravelFilemanager\Events\FolderIsMoving;
9
use UniSharp\LaravelFilemanager\Events\FolderWasMoving;
10
11
class ItemsController extends LfmController
12
{
13
    /**
14
     * @return mixed
15
     */
16
    private static function getKeywordFromRequest()
0 ignored issues
show
Unused Code introduced by
The method getKeywordFromRequest() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
17
    {
18
        return request()->get('keyword', "");
0 ignored issues
show
Bug introduced by
The function request 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

18
        return /** @scrutinizer ignore-call */ request()->get('keyword', "");
Loading history...
19
    }
20
21
22
    /**
23
     * @return int
24
     */
25
    private static function getCurrentPageFromRequest(): int
26
    {
27
        $currentPage = (int) request()->get('page', 1);
0 ignored issues
show
Bug introduced by
The function request 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

27
        $currentPage = (int) /** @scrutinizer ignore-call */ request()->get('page', 1);
Loading history...
28
29
        return max($currentPage, 1);
30
    }
31
32
33
    /**
34
     * Get the images to load for a selected folder.
35
     *
36
     * @return mixed
37
     */
38
    public function getItems()
39
    {
40
        $currentPage = self::getCurrentPageFromRequest();
41
42
        $perPage = $this->helper->getPaginationPerPage();
0 ignored issues
show
Bug introduced by
The method getPaginationPerPage() 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

42
        /** @scrutinizer ignore-call */ 
43
        $perPage = $this->helper->getPaginationPerPage();

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 UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
43
44
        if (config('lfm.default_sort') == 'time') {
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

44
        if (/** @scrutinizer ignore-call */ config('lfm.default_sort') == 'time') {
Loading history...
45
            $items = array_merge($this->lfm->folders(), array_reverse($this->lfm->files()));
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method folders() 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

45
            $items = array_merge($this->lfm->/** @scrutinizer ignore-call */ folders(), array_reverse($this->lfm->files()));

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...
46
        } else {
47
            $items = array_merge($this->lfm->folders(), $this->lfm->files());
48
        }
49
50
        $items = array_map(function ($item) {
51
            return $item->fill()->attributes;
52
        }, $items);
53
54
        $keyword = request()->get('keyword', "");
0 ignored issues
show
Bug introduced by
The function request 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

54
        $keyword = /** @scrutinizer ignore-call */ request()->get('keyword', "");
Loading history...
55
56
        if (!empty($keyword)) {
57
            $items = array_values(array_filter($items, function ($item) use ($keyword) {
58
                if ($this->like_match("%" . $keyword . "%", $item['name'])) {
59
                    return TRUE;
60
                } else {
61
                    return FALSE;
62
                }
63
            }));
64
        }
65
66
        return [
67
            'items' => array_slice($items, ($currentPage - 1) * $perPage, $perPage),
68
            'paginator' => [
69
                'current_page' => $currentPage,
70
                'total' => count($items),
71
                'per_page' => $perPage,
72
            ],
73
            'display' => $this->helper->getDisplayMode(),
74
            'working_dir' => $this->lfm->path('working_dir'),
75
        ];
76
    }
77
78
79
    public function like_match($pattern, $subject)
80
    {
81
        $pattern = str_replace('%', '.*', preg_quote($pattern, '/'));
82
83
        return (bool) preg_match("/^{$pattern}$/i", $subject);
84
    }
85
86
87
    public function move()
88
    {
89
        $items = request('items');
0 ignored issues
show
Bug introduced by
The function request 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

89
        $items = /** @scrutinizer ignore-call */ request('items');
Loading history...
90
        $folder_types = array_filter(['user', 'share'], function ($type) {
91
            return $this->helper->allowFolderType($type);
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
92
        });
93
94
        return view('laravel-filemanager::move')
0 ignored issues
show
Bug introduced by
The function view 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

94
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::move')
Loading history...
95
            ->with([
96
                'root_folders' => array_map(function ($type) use ($folder_types) {
97
                    $path = $this->lfm->dir($this->helper->getRootFolder($type));
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
99
                    return (object) [
100
                        'name' => trans('laravel-filemanager::lfm.title-' . $type),
0 ignored issues
show
Bug introduced by
Are you sure the usage of trans('laravel-filemanager::lfm.title-' . $type) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to trans() has too many arguments starting with 'laravel-filemanager::lfm.title-' . $type. ( Ignorable by Annotation )

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

100
                        'name' => /** @scrutinizer ignore-call */ trans('laravel-filemanager::lfm.title-' . $type),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
                        'url' => $path->path('working_dir'),
102
                        'children' => $path->folders(),
103
                        'has_next' => !($type == end($folder_types)),
104
                    ];
105
                }, $folder_types),
106
            ])
107
            ->with('items', $items);
108
    }
109
110
111
    public function domove()
112
    {
113
        $target = $this->helper->input('goToFolder');
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
        $items = $this->helper->input('items');
115
116
        foreach ($items as $item) {
117
            $old_file = $this->lfm->pretty($item);
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
            $is_directory = $old_file->isDirectory();
119
120
            $file = $this->lfm->setName($item);
121
122
            if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
123
                abort(404);
0 ignored issues
show
Bug introduced by
The function abort 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

123
                /** @scrutinizer ignore-call */ 
124
                abort(404);
Loading history...
124
            }
125
126
            $old_path = $old_file->path();
127
128
            if ($old_file->hasThumb()) {
129
                $new_file = $this->lfm->setName($item)->thumb()->dir($target);
130
                if ($is_directory) {
131
                    event(new FolderIsMoving($old_file->path(), $new_file->path()));
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

131
                    /** @scrutinizer ignore-call */ 
132
                    event(new FolderIsMoving($old_file->path(), $new_file->path()));
Loading history...
132
                } else {
133
                    event(new FileIsMoving($old_file->path(), $new_file->path()));
134
                }
135
                $this->lfm->setName($item)->thumb()->move($new_file);
136
            }
137
            $new_file = $this->lfm->setName($item)->dir($target);
138
            $this->lfm->setName($item)->move($new_file);
139
            if ($is_directory) {
140
                event(new FolderWasMoving($old_path, $new_file->path()));
141
            } else {
142
                event(new FileWasMoving($old_path, $new_file->path()));
143
            }
144
        }
145
146
        return parent::$success_response;
147
    }
148
}
149