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 (#5427)
by Cristian
29:45 queued 14:40
created

ValidUploadMultiple::validateUploadInSubfield()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
c 1
b 1
f 0
nc 3
nop 4
dl 0
loc 15
rs 9.6111
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Validation\Rules;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade;
6
use Backpack\CRUD\app\Library\Uploaders\Uploader;
7
use Closure;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
11
class ValidUploadMultiple extends ValidFileArray
12
{
13
    /**
14
     * Run the validation rule.
15
     *
16
     * @param  string  $attribute
17
     * @param  mixed  $value
18
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
19
     * @return void
20
     */
21
    public function validate(string $attribute, mixed $value, Closure $fail): void
22
    {
23
        $entry = CrudPanelFacade::getCurrentEntry() ?: null;
0 ignored issues
show
Bug introduced by
The method getCurrentEntry() 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

23
        $entry = CrudPanelFacade::/** @scrutinizer ignore-call */ getCurrentEntry() ?: null;
Loading history...
24
25
        if (! $value = self::ensureValidValue($value)) {
26
            $fail('Unable to determine the value type.');
27
28
            return;
29
        }
30
31
        // `upload_multiple` sends [[0 => null]] when user doesn't upload anything
32
        // assume that nothing changed on field so nothing is sent on the request.
33
        if (count($value) === 1 && empty($value[0])) {
34
            if ($entry) {
35
                Arr::forget($this->data, $attribute);
36
            } else {
37
                $this->data[$attribute] = [];
38
            }
39
            $value = [];
40
        }
41
42
        if (Str::contains($attribute, '.')) {
43
            $this->validateUploadInSubfield($attribute, $value, $fail, $entry);
44
45
            return;
46
        }
47
48
        $previousValues = $entry?->{$attribute} ?? [];
49
        if (is_string($previousValues)) {
50
            $previousValues = json_decode($previousValues, true) ?? [];
51
        }
52
53
        $value = array_merge($previousValues, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $value = array_merge($previousValues, /** @scrutinizer ignore-type */ $value);
Loading history...
54
55
        if ($entry) {
56
            $filesDeleted = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? [];
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

56
            $filesDeleted = CrudPanelFacade::/** @scrutinizer ignore-call */ getRequest()->input('clear_'.$attribute) ?? [];
Loading history...
57
58
            $data = $this->data;
59
            $data[$attribute] = array_diff($value, $filesDeleted);
60
61
            $this->validateArrayData($attribute, $fail, $data);
62
63
            $this->validateItems($attribute, $value, $fail);
64
65
            return;
66
        }
67
68
        $this->validateArrayData($attribute, $fail);
69
70
        $this->validateItems($attribute, $value, $fail);
71
    }
72
73
    protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null)
74
    {
75
        $mainField = Str::before($attribute, '.');
76
        $subfield = Str::afterLast($attribute, '.');
77
        $row = (int) Str::before(Str::after($attribute, '.'), '.');
78
        $dataFiles = explode(',', $this->data['_order_'.$mainField][$row][$subfield] ?? '');
0 ignored issues
show
Unused Code introduced by
The assignment to $dataFiles is dead and can be removed.
Loading history...
79
        $values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.
Loading history...
80
        if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) {
81
            return;
82
        }
83
84
        $this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
85
86
        if (! empty($value) && ! empty($this->getFileRules())) {
87
            $this->createValidator($subfield.'.*', $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
88
        }
89
    }
90
}
91