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 — master (#3533)
by
unknown
26:28 queued 07:18
created

Validation::setValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
7
trait Validation
8
{
9
    /**
10
     * Adds the required rules from an array and allows validation of that array.
11
     *
12
     * @param array $requiredFields
13
     */
14
    public function setValidationFromArray(array $rules)
15
    {
16
        $requiredFields = [];
17
18
        if (count($rules)) {
19
            foreach ($rules as $key => $rule) {
20
                if (
21
                    (is_string($rule) && strpos($rule, 'required') !== false && strpos($rule, 'required_') === false) ||
22
                    (is_array($rule) && array_search('required', $rule) !== false && array_search('required_', $rule) === false)
23
                ) {
24
                    $requiredFields[] = $key;
25
                }
26
            }
27
        }
28
        $this->setOperationSetting('requiredFields', $requiredFields);
0 ignored issues
show
Bug introduced by
It seems like setOperationSetting() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->/** @scrutinizer ignore-call */ 
29
               setOperationSetting('requiredFields', $requiredFields);
Loading history...
29
        $this->setOperationSetting('validationRules', $rules);
30
    }
31
32
    /**
33
     * Mark a FormRequest file as required for the current operation, in Settings.
34
     * Adds the required rules to an array for easy access.
35
     *
36
     * @param string $class Class that extends FormRequest
37
     */
38
    public function setValidation($class)
39
    {
40
        $this->setFormRequest($class);
41
        $this->setRequiredFields($class);
42
    }
43
44
    /**
45
     * Remove the current FormRequest from configuration, so it will no longer be validated.
46
     */
47
    public function unsetValidation()
48
    {
49
        $this->setOperationSetting('formRequest', false);
50
    }
51
52
    /**
53
     * Remove the current FormRequest from configuration, so it will no longer be validated.
54
     */
55
    public function disableValidation()
56
    {
57
        $this->unsetValidation();
58
    }
59
60
    /**
61
     * Mark a FormRequest file as required for the current operation, in Settings.
62
     *
63
     * @param string $class Class that extends FormRequest
64
     */
65
    public function setFormRequest($class)
66
    {
67
        $this->setOperationSetting('formRequest', $class);
68
    }
69
70
    /**
71
     * Get the current form request file, in any.
72
     * Returns null if no FormRequest is required for the current operation.
73
     *
74
     * @return string Class that extends FormRequest
75
     */
76
    public function getFormRequest()
77
    {
78
        return $this->getOperationSetting('formRequest');
0 ignored issues
show
Bug introduced by
It seems like getOperationSetting() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        return $this->/** @scrutinizer ignore-call */ getOperationSetting('formRequest');
Loading history...
79
    }
80
81
    /**
82
     * Run the authorization and validation the currently set FormRequest.
83
     *
84
     * @return \Illuminate\Http\Request
85
     */
86
    public function validateRequest()
87
    {
88
        $formRequest = $this->getFormRequest();
89
90
        if ($formRequest) {
91
            // authorize and validate the formRequest
92
            // this is done automatically by Laravel's FormRequestServiceProvider
93
            // because form requests implement ValidatesWhenResolved
94
            $request = app($formRequest);
95
        } else {
96
            $request = $this->getRequest();
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Backpack\CRUD\app\Librar...Panel\Traits\Validation. Did you maybe mean getFormRequest()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            /** @scrutinizer ignore-call */ 
97
            $request = $this->getRequest();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
98
            if ($this->hasOperationSetting('validationRules')) {
0 ignored issues
show
Bug introduced by
It seems like hasOperationSetting() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            if ($this->/** @scrutinizer ignore-call */ hasOperationSetting('validationRules')) {
Loading history...
99
                $rules = $this->getOperationSetting('validationRules');
100
                $request->validate($rules);
101
            }
102
        }
103
104
        return $request;
105
    }
106
107
    /**
108
     * Parse a FormRequest class, figure out what inputs are required
109
     * and store this knowledge in the current object.
110
     *
111
     * @param string $class Class that extends FormRequest
112
     */
113
    public function setRequiredFields($class)
114
    {
115
        $formRequest = new $class();
116
        $rules = $formRequest->rules();
117
        $requiredFields = [];
118
119
        if (count($rules)) {
120
            foreach ($rules as $key => $rule) {
121
                if (
122
                    (is_string($rule) && strpos($rule, 'required') !== false && strpos($rule, 'required_') === false) ||
123
                    (is_array($rule) && array_search('required', $rule) !== false && array_search('required_', $rule) === false)
124
                ) {
125
                    $requiredFields[] = $key;
126
                }
127
            }
128
        }
129
130
        $this->setOperationSetting('requiredFields', $requiredFields);
131
    }
132
133
    /**
134
     * Check the current object to see if an input is required
135
     * for the given operation.
136
     *
137
     * @param string $inputKey  Field or input name.
138
     * @param string $operation create / update
139
     *
140
     * @return bool
141
     */
142
    public function isRequired($inputKey)
143
    {
144
        if (! $this->hasOperationSetting('requiredFields')) {
145
            return false;
146
        }
147
148
        return in_array($inputKey, $this->getOperationSetting('requiredFields'));
149
    }
150
}
151