Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#4988)
by Pedro
40:53 queued 25:51
created

MultipleFiles::uploadFile()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 24
nop 2
dl 0
loc 35
rs 7.6666
c 0
b 0
f 0

How to fix   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 Backpack\CRUD\app\Library\CrudPanel\Uploads\Uploaders;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Storage;
9
10
class MultipleFiles extends Uploader
11
{
12
    public static function for(array $field, $configuration)
13
    {
14
        return (new self($field, $configuration))->multiple();
15
    }
16
17
    public function uploadFile(Model $entry, $value = null)
18
    {
19
        $filesToDelete = CRUD::getRequest()->get('clear_'.$this->name);
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, 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

19
        $filesToDelete = CRUD::/** @scrutinizer ignore-call */ getRequest()->get('clear_'.$this->name);
Loading history...
20
21
        $value = $value ?? CRUD::getRequest()->file($this->name);
22
23
        $previousFiles = $entry->getOriginal($this->name) ?? [];
24
25
        if (! is_array($previousFiles) && is_string($previousFiles)) {
26
            $previousFiles = json_decode($previousFiles, true);
27
        }
28
29
        if ($filesToDelete) {
30
            foreach ($previousFiles as $previousFile) {
31
                if (in_array($previousFile, $filesToDelete)) {
32
                    Storage::disk($this->disk)->delete($previousFile);
33
34
                    $previousFiles = Arr::where($previousFiles, function ($value, $key) use ($previousFile) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

34
                    $previousFiles = Arr::where($previousFiles, function ($value, /** @scrutinizer ignore-unused */ $key) use ($previousFile) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
                        return $value != $previousFile;
36
                    });
37
                }
38
            }
39
        }
40
41
        foreach ($value ?? [] as $file) {
42
            if ($file && is_file($file)) {
43
                $fileName = $this->getFileNameWithExtension($file);
44
45
                $file->storeAs($this->path, $fileName, $this->disk);
46
47
                $previousFiles[] = $this->path.$fileName;
48
            }
49
        }
50
51
        return isset($entry->getCasts()[$this->name]) ? $previousFiles : json_encode($previousFiles);
52
    }
53
54
    public function saveRepeatableFile(Model $entry, $files = null)
55
    {
56
        $previousFiles = $this->getPreviousRepeatableValues($entry);
57
58
        $fileOrder = $this->getFileOrderFromRequest();
59
60
        foreach ($files as $row => $files) {
61
            foreach ($files ?? [] as $file) {
62
                if ($file && is_file($file)) {
63
                    $fileName = $this->getFileNameWithExtension($file);
64
65
                    $file->storeAs($this->path, $fileName, $this->disk);
66
                    $fileOrder[$row][] = $this->path.$fileName;
67
                }
68
            }
69
        }
70
71
        foreach ($previousFiles as $previousRow => $files) {
72
            foreach ($files ?? [] as $key => $file) {
73
                $key = array_search($file, $fileOrder, true);
74
                if ($key === false) {
75
                    Storage::disk($this->disk)->delete($file);
76
                }
77
            }
78
        }
79
80
        return $fileOrder;
81
    }
82
}
83