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
14:01
created

Validation::unsetValidation()   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 0
dl 0
loc 3
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 $requiredFields)
15
    {
16
        $this->setOperationSetting('requiredFields', array_keys($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

16
        $this->/** @scrutinizer ignore-call */ 
17
               setOperationSetting('requiredFields', array_keys($requiredFields));
Loading history...
17
        $this->setOperationSetting('validationRules', $requiredFields);
18
    }
19
20
    /**
21
     * Mark a FormRequest file as required for the current operation, in Settings.
22
     * Adds the required rules to an array for easy access.
23
     *
24
     * @param string $class Class that extends FormRequest
25
     */
26
    public function setValidation($class)
27
    {
28
        $this->setFormRequest($class);
29
        $this->setRequiredFields($class);
30
    }
31
32
    /**
33
     * Remove the current FormRequest from configuration, so it will no longer be validated.
34
     */
35
    public function unsetValidation()
36
    {
37
        $this->setOperationSetting('formRequest', false);
38
    }
39
40
    /**
41
     * Remove the current FormRequest from configuration, so it will no longer be validated.
42
     */
43
    public function disableValidation()
44
    {
45
        $this->unsetValidation();
46
    }
47
48
    /**
49
     * Mark a FormRequest file as required for the current operation, in Settings.
50
     *
51
     * @param string $class Class that extends FormRequest
52
     */
53
    public function setFormRequest($class)
54
    {
55
        $this->setOperationSetting('formRequest', $class);
56
    }
57
58
    /**
59
     * Get the current form request file, in any.
60
     * Returns null if no FormRequest is required for the current operation.
61
     *
62
     * @return string Class that extends FormRequest
63
     */
64
    public function getFormRequest()
65
    {
66
        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

66
        return $this->/** @scrutinizer ignore-call */ getOperationSetting('formRequest');
Loading history...
67
    }
68
69
    /**
70
     * Run the authorization and validation the currently set FormRequest.
71
     *
72
     * @return \Illuminate\Http\Request
73
     */
74
    public function validateRequest()
75
    {
76
        $formRequest = $this->getFormRequest();
77
78
        if ($formRequest) {
79
            // authorize and validate the formRequest
80
            // this is done automatically by Laravel's FormRequestServiceProvider
81
            // because form requests implement ValidatesWhenResolved
82
            $request = app($formRequest);
83
        } else {
84
            $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

84
            /** @scrutinizer ignore-call */ 
85
            $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...
85
86
            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

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