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 — upload-validation ( 3791cb...27202d )
by Pedro
15:14
created

ValidBackpackUpload::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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