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 — fix-upload-validators-in-repea... ( 0d6f3d )
by Pedro
14:44
created

ValidUpload::validate()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 9.2222
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
            return;
33
        }
34
       
35
        if (! array_key_exists($attribute, $this->data) && $entry) {
36
            return;
37
        }
38
39
        $this->createValidator($attribute, $this->getFieldRules(), $value, $fail);
40
41
        if (! empty($value) && ! empty($this->getFileRules())) {
42
            $this->createValidator($attribute, $this->getFileRules(), $value, $fail);
43
        }
44
    }
45
46
    public static function field(string|array|ValidationRule|Rule $rules = []): self
47
    {
48
        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...
49
    }
50
51
    protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null)
52
    {
53
        $mainField = Str::before($attribute, '.');
54
        $subfield = Str::afterLast($attribute, '.');
55
        $row = (int)Str::before(Str::after($attribute, '.'), '.');
56
57
        $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...
58
59
        if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) {
60
            return;
61
        }
62
63
        $this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
64
        
65
        if(!empty($value) && !empty($this->getFileRules())) {
66
            $this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail);
67
        }
68
    }
69
70
    protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void
71
    {
72
        $validator = Validator::make([$attribute => $value], [
73
            $attribute => $rules,
74
        ], $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...
75
76
        if ($validator->fails()) {
77
            foreach ($validator->errors()->messages()[$attribute] as $message) {
78
                $fail($message)->translate();
79
            }
80
        }
81
    }
82
}
83