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 (#5478)
by Cristian
38:30 queued 23:30
created

MultipleFiles::uploadFiles()   F

Complexity

Conditions 17
Paths 288

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 17
eloc 26
c 3
b 1
f 0
nc 288
nop 2
dl 0
loc 49
rs 3.2833

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\Uploaders;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\Storage;
10
11
class MultipleFiles extends Uploader
12
{
13
    public static function for(array $field, $configuration): UploaderInterface
14
    {
15
        return (new self($field, $configuration))->multiple();
16
    }
17
18
    public function uploadFiles(Model $entry, $value = null)
19
    {
20
        if ($value && isset($value[0]) && is_null($value[0])) {
21
            $value = false;
22
        }
23
24
        $filesToDelete = $this->getFilesToDeleteFromRequest();
25
        $value = $value ?? collect(CRUD::getRequest()->file($this->getNameForRequest()))->flatten()->toArray();
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

25
        $value = $value ?? collect(CRUD::/** @scrutinizer ignore-call */ getRequest()->file($this->getNameForRequest()))->flatten()->toArray();
Loading history...
26
        $previousFiles = $this->getPreviousFiles($entry) ?? [];
27
28
        if (is_array($previousFiles) && empty($previousFiles[0] ?? [])) {
29
            $previousFiles = [];
30
        }
31
32
        if (! is_array($previousFiles) && is_string($previousFiles)) {
33
            $previousFiles = json_decode($previousFiles, true);
34
        }
35
36
        if ($filesToDelete) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filesToDelete of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
            foreach ($previousFiles as $previousFile) {
38
                if (in_array($previousFile, $filesToDelete)) {
39
                    Storage::disk($this->getDisk())->delete($previousFile);
40
41
                    $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

41
                    $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...
42
                        return $value != $previousFile;
43
                    });
44
                }
45
            }
46
        }
47
48
        if (! is_array($value)) {
49
            $value = [];
50
        }
51
52
        foreach ($value as $file) {
53
            if ($file && is_file($file)) {
54
                $fileName = $this->getFileName($file);
55
                $file->storeAs($this->getPath(), $fileName, $this->getDisk());
56
                $previousFiles[] = $this->getPath().$fileName;
57
            }
58
        }
59
60
        $previousFiles = array_values($previousFiles);
61
62
        if (empty($previousFiles)) {
63
            return null;
64
        }
65
66
        return isset($entry->getCasts()[$this->getName()]) ? $previousFiles : json_encode($previousFiles);
67
    }
68
69
    public function uploadRepeatableFiles($files, $previousRepeatableValues, $entry = null)
0 ignored issues
show
Unused Code introduced by
The parameter $entry 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

69
    public function uploadRepeatableFiles($files, $previousRepeatableValues, /** @scrutinizer ignore-unused */ $entry = null)

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...
70
    {
71
        $fileOrder = $this->getFileOrderFromRequest();
72
73
        foreach ($files as $row => $files) {
0 ignored issues
show
introduced by
$files is overwriting one of the parameters of this function.
Loading history...
74
            foreach ($files ?? [] as $file) {
75
                if ($file && is_file($file)) {
76
                    $fileName = $this->getFileName($file);
77
                    $file->storeAs($this->getPath(), $fileName, $this->getDisk());
78
                    $fileOrder[$row][] = $this->getPath().$fileName;
79
                }
80
            }
81
        }
82
83
        foreach ($previousRepeatableValues as $previousRow => $previousFiles) {
84
            foreach ($previousFiles ?? [] as $key => $file) {
85
                $key = array_search($file, $fileOrder[$previousRow], true);
86
                if ($key === false) {
87
                    Storage::disk($this->getDisk())->delete($file);
88
                }
89
            }
90
        }
91
92
        return $fileOrder;
93
    }
94
95
    protected function hasDeletedFiles($value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

95
    protected function hasDeletedFiles(/** @scrutinizer ignore-unused */ $value): bool

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...
96
    {
97
        return empty($this->getFilesToDeleteFromRequest()) ? false : true;
98
    }
99
100
    protected function getEntryAttributeValue(Model $entry)
101
    {
102
        $value = $entry->{$this->getAttributeName()};
103
104
        return isset($entry->getCasts()[$this->getName()]) ? $value : json_encode($value);
105
    }
106
107
    private function getFilesToDeleteFromRequest(): array
108
    {
109
        return collect(CRUD::getRequest()->get('clear_'.$this->getNameForRequest()))->flatten()->toArray();
110
    }
111
}
112