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 — fix-upload-validators-in-repea... ( 2c7240...9b0ef7 )
by Pedro
15:03
created

ValidUpload::field()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Str;
12
13
class ValidUpload extends BackpackCustomRule
14
{
15
    use HasFiles;
16
17
    /**
18
     * Run the validation rule.
19
     *
20
     * @param  string  $attribute
21
     * @param  mixed  $value
22
     * @param  Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
23
     * @return void
24
     */
25
    public function validate(string $attribute, mixed $value, Closure $fail): void
26
    {
27
        $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

27
        /** @scrutinizer ignore-call */ 
28
        $entry = CrudPanelFacade::getCurrentEntry();
Loading history...
28
        // check if string contains two dots
29
        if (Str::contains($attribute, '.') && Str::substrCount($attribute, '.') > 1) {
30
            $this->validateUploadInSubfield($attribute, $value, $fail, $entry);
31
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