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

MultipleFiles::getFilesToDeleteFromRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        return isset($entry->getCasts()[$this->getName()]) ? $previousFiles : json_encode($previousFiles);
61
    }
62
63
    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

63
    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...
64
    {
65
        $fileOrder = $this->getFileOrderFromRequest();
66
67
        foreach ($files as $row => $files) {
0 ignored issues
show
introduced by
$files is overwriting one of the parameters of this function.
Loading history...
68
            foreach ($files ?? [] as $file) {
69
                if ($file && is_file($file)) {
70
                    $fileName = $this->getFileName($file);
71
                    $file->storeAs($this->getPath(), $fileName, $this->getDisk());
72
                    $fileOrder[$row][] = $this->getPath().$fileName;
73
                }
74
            }
75
        }
76
77
        foreach ($previousRepeatableValues as $previousRow => $previousFiles) {
78
            foreach ($previousFiles ?? [] as $key => $file) {
79
                $key = array_search($file, $fileOrder, true);
80
                if ($key === false) {
81
                    Storage::disk($this->getDisk())->delete($file);
82
                }
83
            }
84
        }
85
86
        return $fileOrder;
87
    }
88
89
    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

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