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 — crud-uploads (#5038)
by Pedro
13:06
created

ValidUploadMultiple   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 33
c 2
b 1
f 0
dl 0
loc 68
rs 10
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 58 12
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders\Validation;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade;
6
use Closure;
7
use Illuminate\Support\Facades\Validator;
8
9
class ValidUploadMultiple extends ValidBackpackUpload
10
{
11
    /**
12
     * Run the validation rule.
13
     *
14
     * @param  string  $attribute
15
     * @param  mixed  $value
16
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
17
     * @return void
18
     */
19
    public function validate(string $attribute, mixed $value, Closure $fail): void
20
    {
21
        if (! is_array($value)) {
22
            try {
23
                $value = json_decode($value, true);
24
            } catch(\Exception $e) {
25
                $fail('Unable to determine the value type');
26
27
                return;
28
            }
29
        }
30
        // `upload_multiple` sends [[0 => null]] (null after `ConvertEmptyStringsToNull`) when nothing changes on the field
31
        // for that reason we need to manually check what we are getting from the request
32
        if (CrudPanelFacade::getCurrentEntry() !== false) {
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

32
        if (CrudPanelFacade::/** @scrutinizer ignore-call */ getCurrentEntry() !== false) {
Loading history...
33
            $filesToClear = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? [];
0 ignored issues
show
Bug introduced by
The method getRequest() 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

33
            $filesToClear = CrudPanelFacade::/** @scrutinizer ignore-call */ getRequest()->input('clear_'.$attribute) ?? [];
Loading history...
34
            $previousFiles = CrudPanelFacade::getCurrentEntry()->{$attribute} ?? [];
35
36
            if (is_string($previousFiles) && ! isset(CrudPanelFacade::getCurrentEntry()->getCasts()[$attribute])) {
37
                $previousFiles = json_decode($previousFiles, true);
38
            }
39
40
            $previousFilesWithoutCleared[$attribute] = array_diff($previousFiles, $filesToClear);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$previousFilesWithoutCleared was never initialized. Although not strictly required by PHP, it is generally a good practice to add $previousFilesWithoutCleared = array(); before regardless.
Loading history...
41
42
            // we are only going to check if the deleted files could break the validation rules
43
            if (count($value) === 1 && empty($value[0])) {
44
                $validator = Validator::make($previousFilesWithoutCleared, [
45
                    $attribute => $this->arrayRules,
46
                ], $this->validator->customMessages, $this->validator->customAttributes);
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...
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...
47
48
                if ($validator->fails()) {
49
                    $fail($validator->errors()->first($attribute));
50
                }
51
52
                return;
53
            }
54
55
            // we are now going to check if the previous files - deleted files + new files still pass the validation
56
            $previousFilesWithoutClearedPlusNewFiles[$attribute] = array_merge($previousFilesWithoutCleared[$attribute], $value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$previousFilesWithoutClearedPlusNewFiles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $previousFilesWithoutCle...PlusNewFiles = array(); before regardless.
Loading history...
57
58
            $validator = Validator::make($previousFilesWithoutClearedPlusNewFiles, [
59
                $attribute => $this->arrayRules,
60
            ], $this->validator->customMessages, $this->validator->customAttributes);
61
62
            if ($validator->fails()) {
63
                $fail($validator->errors()->first($attribute));
64
65
                return;
66
            }
67
        }
68
69
        // we are now going to perform the file validation on the actual files
70
        foreach ($value as $file) {
71
            $validator = Validator::make([$attribute => $file], [
72
                $attribute => $this->fileRules,
73
            ], $this->validator->customMessages, $this->validator->customAttributes);
74
75
            if ($validator->fails()) {
76
                $fail($validator->errors()->first($attribute));
77
            }
78
        }
79
    }
80
}
81