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-upload-validators-in-repea... ( 0d6f3d )
by Pedro
14:44
created

ValidUploadMultiple::validateUploadInSubfield()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
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\Str;
9
use Illuminate\Support\Facades\Validator;
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
            return;
28
        }
29
30
        if (! $value = self::ensureValidValue($value)) {
31
            $fail('Unable to determine the value type.');
32
33
            return;
34
        }
35
36
        // `upload_multiple` sends [[0 => null]] when user doesn't upload anything
37
        // assume that nothing changed on field so nothing is sent on the request.
38
        if (count($value) === 1 && empty($value[0])) {
39
            if ($entry) {
40
                unset($this->data[$attribute]);
41
            } else {
42
                $this->data[$attribute] = [];
43
            }
44
            $value = [];
45
        }
46
47
        $previousValues = $entry?->{$attribute} ?? [];
48
        if (is_string($previousValues)) {
49
            $previousValues = json_decode($previousValues, true) ?? [];
50
        }
51
52
        $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

52
        $value = array_merge($previousValues, /** @scrutinizer ignore-type */ $value);
Loading history...
53
54
        if ($entry) {
55
            $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

55
            $filesDeleted = CrudPanelFacade::/** @scrutinizer ignore-call */ getRequest()->input('clear_'.$attribute) ?? [];
Loading history...
56
57
            $data = $this->data;
58
            $data[$attribute] = array_diff($value, $filesDeleted);
59
60
            $this->validateArrayData($attribute, $fail, $data);
61
62
            $this->validateItems($attribute, $value, $fail);
63
64
            return;
65
        }
66
67
        $this->validateArrayData($attribute, $fail);
68
69
        $this->validateItems($attribute, $value, $fail);
70
    }
71
72
    protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null)
73
    {
74
        $mainField = Str::before($attribute, '.');
75
        $subfield = Str::afterLast($attribute, '.');
76
        $row = (int)Str::before(Str::after($attribute, '.'), '.');
77
78
        $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...
79
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
    protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void
92
    {
93
        $validator = Validator::make([$attribute => $value], [
94
            $attribute => $rules,
95
        ], $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...
96
97
        if ($validator->fails()) {
98
            foreach ($validator->errors()->messages()[$attribute] as $message) {
99
                $fail($message)->translate();
100
            }
101
        }
102
    }
103
}
104