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 — main (#5427)
by Pedro
14:44
created

ValidUpload::createValidator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Validation\Rules;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade;
6
use Backpack\CRUD\app\Library\Uploaders\Uploader;
7
use Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles;
8
use Closure;
9
use Illuminate\Contracts\Validation\Rule;
10
use Illuminate\Contracts\Validation\ValidationRule;
11
use Illuminate\Support\Facades\Validator;
12
use Illuminate\Support\Str;
13
14
class ValidUpload extends BackpackCustomRule
15
{
16
    use HasFiles;
17
18
    /**
19
     * Run the validation rule.
20
     *
21
     * @param  string  $attribute
22
     * @param  mixed  $value
23
     * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
24
     * @return void
25
     */
26
    public function validate(string $attribute, mixed $value, Closure $fail): void
27
    {
28
        $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

28
        /** @scrutinizer ignore-call */ 
29
        $entry = CrudPanelFacade::getCurrentEntry();
Loading history...
29
30
        if (Str::contains($attribute, '.')) {
31
            $this->validateUploadInSubfield($attribute, $value, $fail, $entry);
32
33
            return;
34
        }
35
36
        if (! array_key_exists($attribute, $this->data) && $entry) {
37
            return;
38
        }
39
40
        $this->createValidator($attribute, $this->getFieldRules(), $value, $fail);
41
42
        if (! empty($value) && ! empty($this->getFileRules())) {
43
            $this->createValidator($attribute, $this->getFileRules(), $value, $fail);
44
        }
45
    }
46
47
    public static function field(string|array|ValidationRule|Rule $rules = []): self
48
    {
49
        return parent::field($rules);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::field($rules) returns the type Backpack\CRUD\app\Librar...ules\BackpackCustomRule which includes types incompatible with the type-hinted return Backpack\CRUD\app\Librar...ation\Rules\ValidUpload.
Loading history...
50
    }
51
52
    protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null)
53
    {
54
        $mainField = Str::before($attribute, '.');
55
        $subfield = Str::afterLast($attribute, '.');
56
        $row = (int) Str::before(Str::after($attribute, '.'), '.');
57
58
        $values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField] ?? []);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.
Loading history...
59
60
        if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) {
61
            return;
62
        }
63
64
        $this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
65
66
        if (! empty($value) && ! empty($this->getFileRules())) {
67
            $this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
68
        }
69
    }
70
71
    protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void
72
    {
73
        $validator = Validator::make([$attribute => $value], [
74
            $attribute => $rules,
75
        ], $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...
76
77
        if ($validator->fails()) {
78
            foreach ($validator->errors()->messages()[$attribute] as $message) {
79
                $fail($message)->translate();
80
            }
81
        }
82
    }
83
}
84