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
29:50 queued 14:51
created

ValidBackpackUpload::setValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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 ValidBackpackUpload 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 $fileRules = [];
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
    }
49
50
    /**
51
     * Set the performing validator.
52
     *
53
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
54
     * @return $this
55
     */
56
    public function setValidator($validator)
57
    {
58
        $this->validator = $validator;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Set the data under validation.
65
     *
66
     * @param  array  $data
67
     * @return $this
68
     */
69
    public function setData($data)
70
    {
71
        $this->data = $data;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set the rules that apply to the "array" aka the field, if it's required, min, max etc.
78
     */
79
    public function arrayRules(string|array|File $rules): self
80
    {
81
        if (is_string($rules)) {
0 ignored issues
show
introduced by
The condition is_string($rules) is always false.
Loading history...
82
            $rules = explode('|', $rules);
83
        }
84
85
        if (! in_array('array', $rules)) {
86
            $rules[] = 'array';
87
        }
88
89
        $this->arrayRules = $rules;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set the rules that apply to the files beeing uploaded.
96
     */
97
    public function fileRules(string|array|File $rules): self
98
    {
99
        if (is_string($rules)) {
0 ignored issues
show
introduced by
The condition is_string($rules) is always false.
Loading history...
100
            $rules = explode('|', $rules);
101
        }
102
        if (! is_array($rules)) {
0 ignored issues
show
introduced by
The condition is_array($rules) is always true.
Loading history...
103
            $rules = [$rules];
104
        }
105
        $this->fileRules = $rules;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Performs the validation on the array of files using the file validation array.
112
     *
113
     * @param  string  $attribute
114
     * @param  array  $files
115
     * @param  Closure  $fail
116
     * @return void
117
     */
118
    protected function validateFiles($attribute, $files, $fail)
119
    {
120
        foreach ($files as $file) {
121
            if (! is_file($file)) {
122
                continue;
123
            }
124
            $validator = Validator::make([$attribute => $file], [
125
                $attribute => $this->fileRules,
126
            ], $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...
127
128
            if ($validator->fails()) {
129
                foreach ($validator->errors()->messages()[$attribute] as $message) {
130
                    $fail($message);
131
                }
132
            }
133
        }
134
    }
135
136
    /**
137
     * Validate the given data or the array of data from the validator againts the array rules.
138
     *
139
     * @param  string  $attribute
140
     * @param  Closure  $fail
141
     * @param  null|array  $data
142
     * @param  null|array  $rules
143
     * @return void
144
     */
145
    protected function validateArrayData($attribute, $fail, $data = null, $rules = null)
146
    {
147
        $data = $data ?? $this->data;
148
        $rules = $rules ?? $this->arrayRules;
149
150
        $validator = Validator::make($data, [
151
            $attribute => $rules,
152
        ], $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...
153
154
        if ($validator->fails()) {
155
            foreach ($validator->errors()->messages()[$attribute] as $message) {
156
                $fail($message);
157
            }
158
        }
159
    }
160
}
161