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

Test Failed
Pull Request — main (#5518)
by Pedro
34:25 queued 23:25
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
        // create a temporary variable that we can unset keys
83
        // everytime one is found. That way we avoid iterating
84
        // already handled keys (notice we do a deep array copy)
85
        $tempFileOrder = array_map(function ($item) {
86
            return $item;
87
        }, $fileOrder);
88
89
        foreach ($previousRepeatableValues as $previousRow => $previousFiles) {
90
            foreach ($previousFiles ?? [] as $key => $file) {
91
                $previousFileInArray = array_filter($tempFileOrder, function ($items, $key) use ($file, $tempFileOrder) {
92
                    $found = array_search($file, $items ?? [], true);
93
                    if ($found !== false) {
94
                        Arr::forget($tempFileOrder, $key.'.'.$found);
95
96
                        return true;
97
                    }
98
99
                    return false;
100
                }, ARRAY_FILTER_USE_BOTH);
101
                if ($file && ! $previousFileInArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $previousFileInArray 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...
102
                    Storage::disk($this->getDisk())->delete($file);
103
                }
104
            }
105
        }
106
107
        return $fileOrder;
108
    }
109
110
    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

110
    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...
111
    {
112
        return empty($this->getFilesToDeleteFromRequest()) ? false : true;
113
    }
114
115
    protected function getEntryAttributeValue(Model $entry)
116
    {
117
        $value = $entry->{$this->getAttributeName()};
118
119
        return isset($entry->getCasts()[$this->getName()]) ? $value : json_encode($value);
120
    }
121
122
    private function getFilesToDeleteFromRequest(): array
123
    {
124
        return collect(CRUD::getRequest()->get('clear_'.$this->getNameForRequest()))->flatten()->toArray();
125
    }
126
}
127