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 Setup Failed
Pull Request — main (#5725)
by Pedro
25:49 queued 10:59
created

MultipleFiles   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 55
c 2
b 1
f 0
dl 0
loc 115
rs 9.68
wmc 34

6 Methods

Rating   Name   Duplication   Size   Complexity  
A for() 0 3 1
A getFilesToDeleteFromRequest() 0 3 1
A getEntryAttributeValue() 0 5 2
A hasDeletedFiles() 0 3 2
F uploadFiles() 0 49 18
B uploadRepeatableFiles() 0 39 10
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($value)->flatten()->toArray();
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()]) || $this->isFake() ? $previousFiles : json_encode($previousFiles);
67
    }
68
69
    /** @codeCoverageIgnore */
70
    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

70
    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...
71
    {
72
        $fileOrder = $this->getFileOrderFromRequest();
73
74
        foreach ($files as $row => $files) {
0 ignored issues
show
introduced by
$files is overwriting one of the parameters of this function.
Loading history...
75
            foreach ($files ?? [] as $file) {
76
                if ($file && is_file($file)) {
77
                    $fileName = $this->getFileName($file);
78
                    $file->storeAs($this->getPath(), $fileName, $this->getDisk());
79
                    $fileOrder[$row][] = $this->getPath().$fileName;
80
                }
81
            }
82
        }
83
        // create a temporary variable that we can unset keys
84
        // everytime one is found. That way we avoid iterating
85
        // already handled keys (notice we do a deep array copy)
86
        $tempFileOrder = array_map(function ($item) {
87
            return $item;
88
        }, $fileOrder);
89
90
        foreach ($previousRepeatableValues as $previousRow => $previousFiles) {
91
            foreach ($previousFiles ?? [] as $key => $file) {
92
                $previousFileInArray = array_filter($tempFileOrder, function ($items, $key) use ($file, $tempFileOrder) {
93
                    $found = array_search($file, $items ?? [], true);
94
                    if ($found !== false) {
95
                        Arr::forget($tempFileOrder, $key.'.'.$found);
96
97
                        return true;
98
                    }
99
100
                    return false;
101
                }, ARRAY_FILTER_USE_BOTH);
102
                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...
103
                    Storage::disk($this->getDisk())->delete($file);
104
                }
105
            }
106
        }
107
108
        return $fileOrder;
109
    }
110
111
    public function hasDeletedFiles($value): bool
112
    {
113
        return empty($this->getFilesToDeleteFromRequest()) ? false : true;
114
    }
115
116
    public function getEntryAttributeValue(Model $entry)
117
    {
118
        $value = $entry->{$this->getAttributeName()};
119
120
        return isset($entry->getCasts()[$this->getName()]) ? $value : json_encode($value);
121
    }
122
123
    private function getFilesToDeleteFromRequest(): array
124
    {
125
        return collect(CRUD::getRequest()->get('clear_'.$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

125
        return collect(CRUD::/** @scrutinizer ignore-call */ getRequest()->get('clear_'.$this->getNameForRequest()))->flatten()->toArray();
Loading history...
126
    }
127
}
128