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 — crud-uploads (#5038)
by Cristian
12:37
created

ValidArray::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders\Validation;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade;
6
use Closure;
7
use Illuminate\Contracts\Validation\DataAwareRule;
8
use Illuminate\Contracts\Validation\ValidationRule;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Validation\ValidationRule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Illuminate\Contracts\Validation\ValidatorAwareRule;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Support\Facades\Validator;
12
use Illuminate\Validation\Rules\File;
13
14
class ValidArray implements ValidationRule, DataAwareRule, ValidatorAwareRule
15
{
16
    /**
17
     * @var \Illuminate\Contracts\Validation\Validator
18
     */
19
    protected $validator;
20
21
    protected array $data;
22
23
    public array $arrayRules = [];
24
25
    public array $itemRules = [];
26
27
    public ?Model $entry;
28
29
    public static function make(): self
30
    {
31
        $instance = new static();
32
        $entry = CrudPanelFacade::getCurrentEntry();
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

32
        /** @scrutinizer ignore-call */ 
33
        $entry = CrudPanelFacade::getCurrentEntry();
Loading history...
33
        $instance->entry = $entry !== false ? $entry : null;
34
35
        return $instance;
36
    }
37
38
    /**
39
     * Run the validation rule.
40
     *
41
     * @param  string  $attribute
42
     * @param  mixed  $value
43
     * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
44
     * @return void
45
     */
46
    public function validate(string $attribute, mixed $value, Closure $fail): void
47
    {
48
        if (! is_array($value)) {
49
            try {
50
                $value = json_decode($value, true);
51
            } catch (\Exception $e) {
52
                $fail('Unable to determine the value type.');
53
54
                return;
55
            }
56
        }
57
        $this->validateArrayData($attribute, $value, $fail);
0 ignored issues
show
Bug introduced by
$fail of type Closure is incompatible with the type array|null expected by parameter $data of Backpack\CRUD\app\Librar...ay::validateArrayData(). ( Ignorable by Annotation )

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

57
        $this->validateArrayData($attribute, $value, /** @scrutinizer ignore-type */ $fail);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $fail of Backpack\CRUD\app\Librar...ay::validateArrayData() does only seem to accept Closure, 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

57
        $this->validateArrayData($attribute, /** @scrutinizer ignore-type */ $value, $fail);
Loading history...
58
        $this->validateItemsAsArray($attribute, $value, $fail);
59
    }
60
61
    /**
62
     * Set the performing validator.
63
     *
64
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
65
     * @return $this
66
     */
67
    public function setValidator($validator)
68
    {
69
        $this->validator = $validator;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Set the data under validation.
76
     *
77
     * @param  array  $data
78
     * @return $this
79
     */
80
    public function setData($data)
81
    {
82
        $this->data = $data;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Set the rules that apply to the "array" aka the field, if it's required, min, max etc.
89
     */
90
    public function arrayRules(string|array|File $rules): self
91
    {
92
        if (is_string($rules)) {
0 ignored issues
show
introduced by
The condition is_string($rules) is always false.
Loading history...
93
            $rules = explode('|', $rules);
94
        }
95
96
        if (! in_array('array', $rules)) {
97
            $rules[] = 'array';
98
        }
99
100
        $this->arrayRules = $rules;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set the rules that apply to the items beeing sent in array.
107
     */
108
    public function itemRules(string|array|File $rules): self
109
    {
110
        if (is_string($rules)) {
0 ignored issues
show
introduced by
The condition is_string($rules) is always false.
Loading history...
111
            $rules = explode('|', $rules);
112
        }
113
        if (! is_array($rules)) {
0 ignored issues
show
introduced by
The condition is_array($rules) is always true.
Loading history...
114
            $rules = [$rules];
115
        }
116
        $this->itemRules = $rules;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Performs the validation on the array of items, item by item, using the item validation array.
123
     *
124
     * @param  string  $attribute
125
     * @param  array  $files
126
     * @param  Closure  $fail
127
     * @return void
128
     */
129
    protected function validateItems($attribute, $items, $fail)
130
    {
131
        foreach ($items as $item) {
132
            $validator = Validator::make([$attribute => $item], [
133
                $attribute => $this->itemRules,
134
            ], $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...
135
136
            if ($validator->fails()) {
137
                foreach ($validator->errors()->messages()[$attribute] as $message) {
138
                    $fail($message)->translate();
139
                }
140
            }
141
        }
142
    }
143
144
    /**
145
     * Performs the validation on the array of items, using the item validation array.
146
     *
147
     * @param  string  $attribute
148
     * @param  array  $files
149
     * @param  Closure  $fail
150
     * @return void
151
     */
152
    protected function validateItemsAsArray($attribute, $items, $fail)
153
    {
154
        $validator = Validator::make([$attribute => $items], [
155
            $attribute.'.*' => $this->itemRules,
156
        ], $this->validator->customMessages, $this->validator->customAttributes);
0 ignored issues
show
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...
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...
157
158
        if ($validator->fails()) {
159
            foreach ($validator->errors()->messages()[$attribute] as $message) {
160
                $fail($message)->translate();
161
            }
162
        }
163
    }
164
165
    /**
166
     * Validate the given data or the array of data from the validator againts the array rules.
167
     *
168
     * @param  string  $attribute
169
     * @param  Closure  $fail
170
     * @param  null|array  $data
171
     * @param  null|array  $rules
172
     * @return void
173
     */
174
    protected function validateArrayData($attribute, $fail, $data = null, $rules = null)
175
    {
176
        $data = $data ?? $this->data;
177
        $rules = $rules ?? $this->arrayRules;
178
179
        $validator = Validator::make($data, [
180
            $attribute => $rules,
181
        ], $this->validator->customMessages, $this->validator->customAttributes);
0 ignored issues
show
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...
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...
182
183
        if ($validator->fails()) {
184
            foreach ($validator->errors()->messages()[$attribute] as $message) {
185
                $fail($message)->translate();
186
            }
187
        }
188
    }
189
}
190