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

Test Failed
Push — add-method-to-get-ajax-uploade... ( 20693f...16244a )
by Pedro
11:40
created

BackpackCustomRule::ensureValueIsValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Validation\Rules;
4
5
use Closure;
6
use Illuminate\Contracts\Validation\DataAwareRule;
7
use Illuminate\Contracts\Validation\Rule;
8
use Illuminate\Contracts\Validation\ValidationRule;
9
use Illuminate\Contracts\Validation\ValidatorAwareRule;
10
use Illuminate\Support\Facades\Validator;
11
use Illuminate\Support\Str;
12
13
/**
14
 * @method static static itemRules()
15
 */
16
abstract class BackpackCustomRule implements ValidationRule, DataAwareRule, ValidatorAwareRule
17
{
18
19
    use \Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles;
20
    
21
    /**
22
     * @var \Illuminate\Contracts\Validation\Validator
23
     */
24
    protected $validator;
25
26
    protected array $data;
27
28
    public array $fieldRules = [];
29
30
    public bool $implicit = true;
31
32
    public static function field(string|array|ValidationRule|Rule $rules = []): self
33
    {
34
        $instance = new static();
35
        $instance->fieldRules = self::getRulesAsArray($rules);
36
37
        return $instance;
38
    }
39
40
    /**
41
     * Run the validation rule.
42
     *
43
     * @param  string  $attribute
44
     * @param  mixed  $value
45
     * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
46
     * @return void
47
     */
48
    public function validate(string $attribute, mixed $value, Closure $fail): void
49
    {
50
        $value = $this->ensureValueIsValid($value);
51
52
        if($value === false) {
53
            $fail('Invalid value for the attribute.')->translate();
54
            return;
55
        }
56
57
        $errors = $this->validateOnSubmit($attribute, $value);
58
        foreach ($errors as $error) {
59
            $fail($error)->translate();
60
        }
61
    }
62
63
    /**
64
     * Set the performing validator.
65
     *
66
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
67
     * @return $this
68
     */
69
    public function setValidator($validator)
70
    {
71
        $this->validator = $validator;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set the data under validation.
78
     *
79
     * @param  array  $data
80
     * @return $this
81
     */
82
    public function setData($data)
83
    {
84
        $this->data = $data;
85
86
        return $this;
87
    }
88
89
    public function getFieldRules(): array
90
    {
91
        return tap($this->fieldRules, function ($rule) {
92
            if (is_a($rule, BackpackCustomRule::class, true)) {
93
                $rule = $rule->getFieldRules();
94
            }
95
96
            return $rule;
97
        });
98
    }
99
100
    protected static function getRulesAsArray($rules)
101
    {
102
        if (is_string($rules)) {
103
            $rules = explode('|', $rules);
104
        }
105
106
        if (! is_array($rules)) {
107
            $rules = [$rules];
108
        }
109
110
        return $rules;
111
    }
112
113
    // from our POV, value is always valid, each validator may have their own 
114
    // validation needs. This is the first step in the validation process.
115
    protected function ensureValueIsValid($value)
116
    {
117
        return $value;
118
    }
119
120
    private function validateAndGetErrors(string $attribute, mixed $value, array $rules): array
121
    {
122
        $validator =  Validator::make($value, [
123
            $attribute => $rules,
124
        ], $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...
125
126
        return $validator->errors()->messages()[$attribute] ?? [];
127
    }
128
129
    protected function getValidationAttributeString(string $attribute)
130
    {
131
        return Str::substrCount($attribute, '.') > 1 ?
132
                Str::before($attribute, '.').'.*.'.Str::afterLast($attribute, '.') :
133
                $attribute;
134
    }
135
136
    protected function validateOnSubmit(string $attribute, mixed $value): array
137
    {
138
        return $this->validateRules($attribute, $value);
139
    }
140
141
    protected function validateFieldAndFile(string $attribute, mixed $value = null, array|null $data = null, array|null $customRules = null): array
142
    {
143
        $fieldErrors = $this->validateFieldRules($attribute, $value, $data, $customRules);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...e::validateFieldRules() has too many arguments starting with $customRules. ( Ignorable by Annotation )

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

143
        /** @scrutinizer ignore-call */ 
144
        $fieldErrors = $this->validateFieldRules($attribute, $value, $data, $customRules);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
        $fileErrors = $this->validateFileRules($attribute, $value);
145
        return array_merge($fieldErrors, $fileErrors);
146
    }
147
148
    /**
149
     * Implementation.
150
     */
151
    public function validateFieldRules(string $attribute, mixed $data = null, array|null $customRules = null): array
152
    {
153
        $data = $data ?? $this->data;
154
        $validationRuleAttribute = $this->getValidationAttributeString($attribute); 
155
        $data = $this->prepareValidatorData($data, $attribute);
156
        return $this->validateAndGetErrors($validationRuleAttribute, $data, $customRules ?? $this->getFieldRules());
157
    }
158
159
    protected function prepareValidatorData(array $data, string $attribute): array
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed. ( Ignorable by Annotation )

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

159
    protected function prepareValidatorData(array $data, /** @scrutinizer ignore-unused */ string $attribute): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        return $data;
162
    }
163
164
    protected function validateFileRules(string $attribute, mixed $value): array 
165
    {
166
        return $this->validateAndGetErrors($attribute, $value, $this->getFileRules());
167
    }
168
169
    public function validateRules(string $attribute, mixed $value): array
170
    {
171
        return $this->validateFieldAndFile($attribute, $value);
172
    }
173
174
}
175