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

ValidFileArray::validateFileRules()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 30
rs 9.2222
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Validation\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Contracts\Validation\ValidationRule;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Support\Str;
10
11
abstract class ValidFileArray extends BackpackCustomRule
12
{
13
    public static function field(string|array|ValidationRule|Rule $rules = []): self
14
    {
15
        $instance = new static();
16
        $instance->fieldRules = self::getRulesAsArray($rules);
17
18
        if (! in_array('array', $instance->getFieldRules())) {
19
            $instance->fieldRules[] = 'array';
20
        }
21
22
        return $instance;
23
    }
24
25
    protected function validateFileRules(string $attribute, mixed $items): array
26
    {
27
        $cleanAttribute = Str::afterLast($attribute, '.');
28
        $errors = [];
29
30
        // we validate each file individually to avoid returning messages like: `field.0` is not a pdf.
31
        foreach ($items as $file) {
32
            if (is_file($file)) {
33
                $validator = Validator::make(
34
                    [
35
                        $cleanAttribute => $file,
36
                    ],
37
                    [
38
                        $cleanAttribute => $this->getFileRules(),
39
                    ],
40
                    $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...
41
                    $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...
42
                );
43
44
                if ($validator->fails()) {
45
                    foreach ($validator->errors()->messages() ?? [] as $attr => $message) {
46
                        foreach ($message as $messageText) {
47
                            $errors[] = $messageText;
48
                        }
49
                    }
50
                }
51
            }
52
        }
53
54
        return $errors;
55
    }
56
57
    protected function ensureValueIsValid($value)
58
    {
59
        if (! is_array($value)) {
60
            try {
61
                $value = json_decode($value, true) ?? [];
62
            } catch(\Exception $e) {
63
                return false;
64
            }
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
102
        return array_merge($fieldErrors, $fileErrors);
103
    }
104
}
105