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
Push — crud-uploads ( 044930 )
by Pedro
11:38
created

MultipleFiles::saveMultipleFiles()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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

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 save(Model $entry, $value = null)
18
    {
19
        return $this->isRepeatable && ! $this->isRelationship ? $this->saveRepeatableMutipleFiles($entry, $value) : $this->saveMultipleFiles($entry, $value);
20
    }
21
22
    private function saveMultipleFiles($entry, $value = null)
23
    {
24
        $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

24
        $filesToDelete = CRUD::/** @scrutinizer ignore-call */ getRequest()->get('clear_'.$this->name);
Loading history...
25
26
        $value = $value ?? CRUD::getRequest()->file($this->name);
27
28
        $previousFiles = $entry->getOriginal($this->name) ?? [];
29
        
30
        if (! is_array($previousFiles) && is_string($previousFiles)) {
31
            $previousFiles = json_decode($previousFiles, true);
32
        }
33
34
        if ($filesToDelete) {
35
            foreach ($previousFiles as $previousFile) {
36
                if (in_array($previousFile, $filesToDelete)) {
37
                    Storage::disk($this->disk)->delete($previousFile);
38
39
                    $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

39
                    $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...
40
                        return $value != $previousFile;
41
                    });
42
                }
43
            }
44
        }
45
46
        foreach ($value ?? [] as $file) {
47
            if ($file && is_file($file)) {
48
                $fileName = $this->getFileNameWithExtension($file);
49
50
                $file->storeAs($this->path, $fileName, $this->disk);
51
52
                $previousFiles[] = $this->path.$fileName;
53
            }
54
        }
55
56
        return isset($entry->getCasts()[$this->name]) ? $previousFiles : json_encode($previousFiles);
57
    }
58
59
    private function saveRepeatableMutipleFiles($entry, $files)
60
    {
61
        $previousFiles = $this->getPreviousRepeatableValues($entry);
62
63
        $fileOrder = $this->getFileOrderFromRequest();
64
65
        foreach ($files as $row => $files) {
0 ignored issues
show
introduced by
$files is overwriting one of the parameters of this function.
Loading history...
66
            foreach ($files ?? [] as $file) {
67
                if ($file && is_file($file)) {
68
                    $fileName = $this->getFileNameWithExtension($file);
69
70
                    $file->storeAs($this->path, $fileName, $this->disk);
71
                    $fileOrder[$row][] = $this->path.$fileName;
72
                }
73
            }
74
        }
75
76
        foreach ($previousFiles as $previousRow => $files) {
0 ignored issues
show
introduced by
$files is overwriting one of the parameters of this function.
Loading history...
77
            foreach ($files ?? [] as $key => $file) {
78
                $key = array_search($file, $fileOrder, true);
79
                if ($key === false) {
80
                    Storage::disk($this->disk)->delete($file);
81
                }
82
            }
83
        }
84
85
        return $fileOrder;
86
    }
87
}
88