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
Pull Request — main (#5440)
by Cristian
26:23 queued 11:28
created

BackpackCustomRule::validateAndGetErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 7
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
    use \Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles;
19
20
    /**
21
     * @var \Illuminate\Contracts\Validation\Validator
22
     */
23
    protected $validator;
24
25
    protected array $data;
26
27
    public array $fieldRules = [];
28
29
    public bool $implicit = true;
30
31
    public static function field(string|array|ValidationRule|Rule $rules = []): self
32
    {
33
        $instance = new static();
34
        $instance->fieldRules = self::getRulesAsArray($rules);
35
36
        return $instance;
37
    }
38
39
    /**
40
     * Run the validation rule.
41
     *
42
     * @param  string  $attribute
43
     * @param  mixed  $value
44
     * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
45
     * @return void
46
     */
47
    public function validate(string $attribute, mixed $value, Closure $fail): void
48
    {
49
        $value = $this->ensureValueIsValid($value);
50
51
        if ($value === false) {
52
            $fail('Invalid value for the attribute.')->translate();
53
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
146
        return array_merge($fieldErrors, $fileErrors);
147
    }
148
149
    /**
150
     * Implementation.
151
     */
152
    public function validateFieldRules(string $attribute, mixed $data = null, array|null $customRules = null): array
153
    {
154
        $data = $data ?? $this->data;
155
        $validationRuleAttribute = $this->getValidationAttributeString($attribute);
156
        $data = $this->prepareValidatorData($data, $attribute);
157
158
        return $this->validateAndGetErrors($validationRuleAttribute, $data, $customRules ?? $this->getFieldRules());
159
    }
160
161
    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

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