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 — fix-uploaders-inside-repeatabl... ( 7e9dc4 )
by Pedro
15:25
created

MultipleFiles   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 23

3 Methods

Rating   Name   Duplication   Size   Complexity  
A for() 0 3 1
C uploadFiles() 0 39 14
B uploadRepeatableFiles() 0 24 8
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 = collect(CRUD::getRequest()->get('clear_'.$this->getRepeatableContainerName() ?? $this->getName()))->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

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

37
                    $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...
38
                        return $value != $previousFile;
39
                    });
40
                }
41
            }
42
        }
43
44
        if(!is_array($value)) {
45
            $value = [];
46
        }
47
48
        foreach ($value as $file) {
49
            if ($file && is_file($file)) {
50
                $fileName = $this->getFileName($file);
51
                $file->storeAs($this->getPath(), $fileName, $this->getDisk());
52
                $previousFiles[] = $this->getPath().$fileName;
53
            }
54
        }
55
56
        return isset($entry->getCasts()[$this->getName()]) ? $previousFiles : json_encode($previousFiles);
57
    }
58
59
    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

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