Test Failed
Pull Request — master (#1250)
by
unknown
09:52
created

LfmItem   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 69
c 2
b 1
f 1
dl 0
loc 202
rs 8.96
wmc 43

20 Methods

Rating   Name   Duplication   Size   Complexity  
A size() 0 3 2
A extension() 0 3 1
A url() 0 7 2
A isDirectory() 0 3 1
A mimeType() 0 7 2
A __get() 0 8 2
A __construct() 0 7 2
A path() 0 3 1
A fill() 0 7 2
A name() 0 3 1
A isFile() 0 3 1
A isImage() 0 3 2
A time() 0 10 6
A shouldCreateThumb() 0 15 4
A get() 0 3 1
A type() 0 11 3
A hasThumb() 0 11 3
A thumbUrl() 0 11 3
A icon() 0 11 3
A humanFilesize() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like LfmItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LfmItem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace UniSharp\LaravelFilemanager;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8
class LfmItem
9
{
10
    private $lfm;
11
    private $helper;
12
    private $isDirectory;
13
    private $mimeType = null;
14
15
    private $columns = [];
16
    public $attributes = [];
17
18
    public function __construct(LfmPath $lfm, Lfm $helper, $isDirectory = false)
19
    {
20
        $this->lfm = $lfm->thumb(false);
21
        $this->helper = $helper;
22
        $this->isDirectory = $isDirectory;
23
        $this->columns = $helper->config('item_columns') ?:
24
            ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url'];
25
    }
26
27
    public function __get($var_name)
28
    {
29
        if (!array_key_exists($var_name, $this->attributes)) {
30
            $function_name = Str::camel($var_name);
31
            $this->attributes[$var_name] = $this->$function_name();
32
        }
33
34
        return $this->attributes[$var_name];
35
    }
36
37
    public function fill()
38
    {
39
        foreach ($this->columns as $column) {
40
            $this->__get($column);
41
        }
42
43
        return $this;
44
    }
45
46
    public function name()
47
    {
48
        return $this->lfm->getName();
49
    }
50
51
    public function path($type = 'absolute')
52
    {
53
        return $this->lfm->path($type);
54
    }
55
56
    public function isDirectory()
57
    {
58
        return $this->isDirectory;
59
    }
60
61
    public function isFile()
62
    {
63
        return ! $this->isDirectory();
64
    }
65
66
    /**
67
     * Check a file is image or not.
68
     *
69
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
70
     * @return bool
71
     */
72
    public function isImage()
73
    {
74
        return $this->isFile() && Str::startsWith($this->mimeType(), 'image');
75
    }
76
77
    /**
78
     * Get mime type of a file.
79
     *
80
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
81
     * @return string
82
     */
83
    public function mimeType()
84
    {
85
        if (is_null($this->mimeType)) {
86
            $this->mimeType = $this->lfm->mimeType();
0 ignored issues
show
Bug introduced by
The method mimeType() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

86
            /** @scrutinizer ignore-call */ 
87
            $this->mimeType = $this->lfm->mimeType();
Loading history...
87
        }
88
89
        return $this->mimeType;
90
    }
91
92
    public function extension()
93
    {
94
        return $this->lfm->extension();
0 ignored issues
show
Bug introduced by
The method extension() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( 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 $this->lfm->/** @scrutinizer ignore-call */ extension();
Loading history...
95
    }
96
97
    public function url()
98
    {
99
        if ($this->isDirectory()) {
100
            return $this->lfm->path('working_dir');
101
        }
102
103
        return $this->lfm->url();
104
    }
105
106
    public function size()
107
    {
108
        return $this->isFile() ? $this->humanFilesize($this->lfm->size()) : '';
0 ignored issues
show
Bug introduced by
The method size() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

108
        return $this->isFile() ? $this->humanFilesize($this->lfm->/** @scrutinizer ignore-call */ size()) : '';
Loading history...
109
    }
110
111
    public function time()
112
    {
113
        if (function_exists('config')) {
114
            $disk = $this->helper->config('disk');
115
            $driver = $disk ? config("filesystems.disks.$disk.driver") : null;
116
            if (($driver == 'bunny' || $driver == 'bunnycdn') && $this->isDirectory()) {
117
                return null;
118
            }
119
        }
120
        return $this->lfm->lastModified();
0 ignored issues
show
Bug introduced by
The method lastModified() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

120
        return $this->lfm->/** @scrutinizer ignore-call */ lastModified();
Loading history...
121
    }
122
123
    public function thumbUrl()
124
    {
125
        if ($this->isDirectory()) {
126
            return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png');
0 ignored issues
show
Bug introduced by
The function asset 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

126
            return /** @scrutinizer ignore-call */ asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png');
Loading history...
127
        }
128
129
        if ($this->isImage()) {
130
            return $this->lfm->thumb($this->hasThumb())->url(true);
0 ignored issues
show
Unused Code introduced by
The call to UniSharp\LaravelFilemanager\LfmPath::url() has too many arguments starting with true. ( Ignorable by Annotation )

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

130
            return $this->lfm->thumb($this->hasThumb())->/** @scrutinizer ignore-call */ url(true);

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...
131
        }
132
133
        return null;
134
    }
135
136
    public function icon()
137
    {
138
        if ($this->isDirectory()) {
139
            return 'fa-folder-o';
140
        }
141
142
        if ($this->isImage()) {
143
            return 'fa-image';
144
        }
145
146
        return $this->extension();
147
    }
148
149
    public function type()
150
    {
151
        if ($this->isDirectory()) {
152
            return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder');
0 ignored issues
show
Bug introduced by
The function trans 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

152
            return /** @scrutinizer ignore-call */ trans(Lfm::PACKAGE_NAME . '::lfm.type-folder');
Loading history...
153
        }
154
155
        if ($this->isImage()) {
156
            return $this->mimeType();
157
        }
158
159
        return $this->helper->getFileType($this->extension());
160
    }
161
162
    public function hasThumb()
163
    {
164
        if (!$this->isImage()) {
165
            return false;
166
        }
167
168
        if (!$this->lfm->thumb()->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

168
        if (!$this->lfm->thumb()->/** @scrutinizer ignore-call */ exists()) {
Loading history...
169
            return false;
170
        }
171
172
        return true;
173
    }
174
175
    public function shouldCreateThumb()
176
    {
177
        if (!$this->helper->config('should_create_thumbnails')) {
178
            return false;
179
        }
180
181
        if (!$this->isImage()) {
182
            return false;
183
        }
184
185
        if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) {
186
            return false;
187
        }
188
189
        return true;
190
    }
191
192
    public function get()
193
    {
194
        return $this->lfm->get();
0 ignored issues
show
Bug introduced by
The method get() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

194
        return $this->lfm->/** @scrutinizer ignore-call */ get();
Loading history...
195
    }
196
197
    /**
198
     * Make file size readable.
199
     *
200
     * @param  int  $bytes     File size in bytes.
201
     * @param  int  $decimals  Decimals.
202
     * @return string
203
     */
204
    public function humanFilesize($bytes, $decimals = 2)
205
    {
206
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
207
        $factor = floor((strlen($bytes) - 1) / 3);
208
209
        return sprintf("%.{$decimals}f %s", $bytes / pow(1024, $factor), @$size[$factor]);
210
    }
211
}
212