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

ValidFileArray::prepareValidatorData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Validation\Rules;
4
5
use Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles;
6
use Closure;
7
use Illuminate\Contracts\Validation\Rule;
8
use Illuminate\Contracts\Validation\ValidationRule;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Facades\Validator;
11
use Illuminate\Support\Str;
12
13
abstract class ValidFileArray extends BackpackCustomRule
14
{
15
    public static function field(string|array|ValidationRule|Rule $rules = []): self
16
    {
17
        $instance = new static();
18
        $instance->fieldRules = self::getRulesAsArray($rules);
19
20
        if (! in_array('array', $instance->getFieldRules())) {
21
            $instance->fieldRules[] = 'array';
22
        }
23
24
        return $instance;
25
    }
26
27
    protected function validateFileRules(string $attribute, mixed $items): array
28
    {
29
        $cleanAttribute = Str::afterLast($attribute, '.');
30
        $errors = [];
31
        
32
        // we validate each file individually to avoid returning messages like: `field.0` is not a pdf. 
33
        foreach ($items as $file) {
34
            if(is_file($file)) {
35
                $validator = Validator::make(
36
                    [
37
                        $cleanAttribute => $file
38
                    ], 
39
                    [
40
                        $cleanAttribute => $this->getFileRules(),
41
                    ], 
42
                    $this->validator->customMessages, 
0 ignored issues
show
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...
43
                    $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...
44
                );
45
46
                if ($validator->fails()) {
47
                    foreach ($validator->errors()->messages() ?? [] as $attr => $message) {
48
                        foreach ($message as $messageText) {
49
                            $errors[] = $messageText;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
        return $errors;
56
    }
57
58
    protected function ensureValueIsValid($value)
59
    {
60
        if (! is_array($value)) {
61
            try {
62
                $value = json_decode($value, true) ?? [];
63
            } catch(\Exception $e) {
64
                return false;
65
            }
66
        }
67
        return $value;
68
    }
69
70
    protected function prepareValidatorData(array $data, $attribute): array
71
    {
72
        return Arr::has($data, $attribute) ? [$attribute => Arr::get($data, $attribute)] : $data;
73
    }
74
75
   /*  public function validateFieldRules(string $attribute, mixed $value = null, array|null $data = null, array|null $customRules = null): array
76
    {
77
        
78
        $validatorData = $this->prepareValidatorData($data, $attribute);
79
80
        $validator = Validator::make($validatorData, [
81
            $attribute => $rules,
82
        ], $this->validator->customMessages, $this->validator->customAttributes);
83
84
        $errors = [];
85
        if ($validator->fails()) {
86
            foreach ($validator->errors()->messages()[$attribute] as $message) {
87
                $errors[] = $message;
88
            }
89
        }
90
        
91
        return $errors;
92
    } */
93
94
    /**
95
     * Run both field and file validations. 
96
     */
97
    protected function validateFieldAndFile(string $attribute, mixed $value = null, ?array $data = null, array|null $customRules = null): array
98
    {
99
        $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

99
        /** @scrutinizer ignore-call */ 
100
        $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...
100
        $fileErrors = $this->validateFileRules($attribute, $value);
101
        return array_merge($fieldErrors, $fileErrors);
102
    }
103
}
104