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 — crud-uploads ( f5e93a...469ece )
by Pedro
11:36
created

MultipleFiles::uploadRepeatableFile()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 16
nop 2
dl 0
loc 26
rs 8.4444
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 Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Storage;
9
10
class MultipleFiles extends Uploader
11
{
12
    public static function for(array $field, $configuration)
13
    {
14
        return (new self($field, $configuration))->multiple();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new self($field, ...figuration)->multiple() returns the type Backpack\CRUD\app\Library\Uploaders\MultipleFiles which is incompatible with the return type mandated by Backpack\CRUD\app\Librar...ploaderInterface::for() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
15
    }
16
17
    public function uploadFile(Model $entry, $value = null)
18
    {
19
        $filesToDelete = CRUD::getRequest()->get('clear_'.$this->getName());
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

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

32
                    $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...
33
                        return $value != $previousFile;
34
                    });
35
                }
36
            }
37
        }
38
39
        foreach ($value ?? [] as $file) {
40
            if ($file && is_file($file)) {
41
                $fileName = $this->getFileName($file);
42
43
                $file->storeAs($this->getPath(), $fileName, $this->getDisk());
44
45
                $previousFiles[] = $this->getPath().$fileName;
46
            }
47
        }
48
49
        return isset($entry->getCasts()[$this->getName()]) ? $previousFiles : json_encode($previousFiles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $prev..._encode($previousFiles) also could return the type array|string which is incompatible with the return type mandated by Backpack\CRUD\app\Librar...Interface::uploadFile() of void.
Loading history...
50
    }
51
52
    public function uploadRepeatableFile(Model $entry, $files = null)
53
    {
54
        $previousFiles = $this->getPreviousRepeatableValues($entry);
55
        $fileOrder = $this->getFileOrderFromRequest();
56
57
        foreach ($files as $row => $files) {
58
            foreach ($files ?? [] as $file) {
59
                if ($file && is_file($file)) {
60
                    $fileName = $this->getFileName($file);
61
62
                    $file->storeAs($this->getPath(), $fileName, $this->getDisk());
63
                    $fileOrder[$row][] = $this->getPath().$fileName;
64
                }
65
            }
66
        }
67
68
        foreach ($previousFiles as $previousRow => $files) {
69
            foreach ($files ?? [] as $key => $file) {
70
                $key = array_search($file, $fileOrder, true);
71
                if ($key === false) {
72
                    Storage::disk($this->getDisk())->delete($file);
73
                }
74
            }
75
        }
76
77
        return $fileOrder;
78
    }
79
}
80