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
Pull Request — main (#5427)
by Pedro
26:34 queued 11:57
created

ValidUploadMultiple::validateUploadInSubfield()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 1
b 1
f 0
nc 3
nop 4
dl 0
loc 16
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\Facades\Validator;
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 (Str::contains($attribute, '.')) {
26
            $this->validateUploadInSubfield($attribute, $value, $fail, $entry);
27
28
            return;
29
        }
30
31
        if (! $value = self::ensureValidValue($value)) {
32
            $fail('Unable to determine the value type.');
33
34
            return;
35
        }
36
37
        // `upload_multiple` sends [[0 => null]] when user doesn't upload anything
38
        // assume that nothing changed on field so nothing is sent on the request.
39
        if (count($value) === 1 && empty($value[0])) {
40
            if ($entry) {
41
                unset($this->data[$attribute]);
42
            } else {
43
                $this->data[$attribute] = [];
44
            }
45
            $value = [];
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
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
81
        if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) {
82
            return;
83
        }
84
85
        $this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
86
87
        if (! empty($value) && ! empty($this->getFileRules())) {
88
            $this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
89
        }
90
    }
91
92
    protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void
93
    {
94
        $validator = Validator::make([$attribute => $value], [
95
            $attribute => $rules,
96
        ], $this->validator->customMessages, $this->validator->customAttributes);
0 ignored issues
show
Bug introduced by
Accessing customAttributes on the interface Illuminate\Contracts\Validation\Validator suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing customMessages on the interface Illuminate\Contracts\Validation\Validator suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
97
98
        if ($validator->fails()) {
99
            foreach ($validator->errors()->messages()[$attribute] as $message) {
100
                $fail($message)->translate();
101
            }
102
        }
103
    }
104
}
105