GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — new (#886)
by
unknown
08:46
created

FormElement::setValidationRules()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 11
nc 8
nop 1
dl 0
loc 19
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SleepingOwl\Admin\Form;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Validation\Rules\Dimensions;
8
use Illuminate\Validation\Rules\Exists;
9
use Illuminate\Validation\Rules\In;
10
use Illuminate\Validation\Rules\NotIn;
11
use Illuminate\Validation\Rules\Unique;
12
use SleepingOwl\Admin\Traits\Assets;
13
use Illuminate\Database\Eloquent\Model;
14
use SleepingOwl\Admin\Traits\Renderable;
15
use SleepingOwl\Admin\Traits\VisibleCondition;
16
use SleepingOwl\Admin\Contracts\Form\FormElementInterface;
17
use SleepingOwl\Admin\Contracts\Template\TemplateInterface;
18
19
abstract class FormElement implements FormElementInterface
20
{
21
    use Assets, VisibleCondition, Renderable;
0 ignored issues
show
Bug introduced by
The trait SleepingOwl\Admin\Traits\Renderable requires the property $view which is not provided by SleepingOwl\Admin\Form\FormElement.
Loading history...
22
23
    /**
24
     * @var TemplateInterface
25
     */
26
    protected $template;
27
28
    /**
29
     * @var Model
30
     */
31
    protected $model;
32
33
    /**
34
     * @var array
35
     */
36
    protected $validationRules = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $validationMessages = [];
42
43
    /**
44
     * @var bool|callable
45
     */
46
    protected $readonly = false;
47
48
    /**
49
     * @var bool|callable
50
     */
51
    protected $valueSkipped = false;
52
53
    /**
54
     * @var mixed
55
     */
56
    protected $value;
57
58
    public function __construct()
59
    {
60
        $this->initializePackage();
61
    }
62
63
    public function initialize()
64
    {
65
        $this->includePackage();
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getValidationMessages()
72
    {
73
        return $this->validationMessages;
74
    }
75
76
    /**
77
     * @param string $rule
78
     * @param string $message
79
     *
80
     * @return $this
81
     */
82
    public function addValidationMessage($rule, $message)
83
    {
84
        if (($pos = strpos($rule, ':')) !== false) {
85
            $rule = substr($rule, 0, $pos);
86
        }
87
88
        $this->validationMessages[$rule] = $message;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param array $validationMessages
95
     *
96
     * @return $this
97
     */
98
    public function setValidationMessages(array $validationMessages)
99
    {
100
        $this->validationMessages = $validationMessages;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getValidationLabels()
109
    {
110
        return [];
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getValidationRules()
117
    {
118
        return $this->validationRules;
119
    }
120
121
    /**
122
     * @param string $rule
123
     * @param string|null $message
124
     *
125
     * @return $this
126
     */
127
    public function addValidationRule($rule, $message = null)
128
    {
129
        $this->validationRules[] = $rule;
130
131
        if (is_null($message)) {
132
            return $this;
133
        }
134
135
        return $this->addValidationMessage($rule, $message);
136
    }
137
138
    /**
139
     * @param array|string $validationRules
140
     *
141
     * @return $this
142
     */
143
    public function setValidationRules($validationRules)
144
    {
145
        if (! is_array($validationRules)) {
146
            $validationRules = func_get_args();
147
        }
148
149
        $this->validationRules = [];
150
        foreach ($validationRules as $rule) {
151
            if($rule instanceof \Illuminate\Validation\Rule || $rule instanceof \Illuminate\Contracts\Validation\Rule || $rule instanceof Dimensions || $rule instanceof Exists || $rule instanceof In || $rule instanceof NotIn || $rule instanceof Unique){
152
                $this->addValidationRule($rule);
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type Illuminate\Contracts\Validation\Rule and Illuminate\Validation\Rule; however, parameter $rule of SleepingOwl\Admin\Form\F...nt::addValidationRule() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

152
                $this->addValidationRule(/** @scrutinizer ignore-type */ $rule);
Loading history...
153
            } else {
154
                $rules = explode('|', $rule);
155
                foreach ($rules as $addRule) {
156
                    $this->addValidationRule($addRule);
157
                }
158
            }
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return Model
166
     */
167
    public function getModel()
168
    {
169
        return $this->model;
170
    }
171
172
    /**
173
     * @param Model $model
174
     *
175
     * @return $this
176
     */
177
    public function setModel(Model $model)
178
    {
179
        $this->model = $model;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return bool
186
     */
187
    public function isReadonly()
188
    {
189
        if (is_callable($this->readonly)) {
190
            return (bool) call_user_func($this->readonly, $this->getModel());
0 ignored issues
show
Bug introduced by
It seems like $this->readonly can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

190
            return (bool) call_user_func(/** @scrutinizer ignore-type */ $this->readonly, $this->getModel());
Loading history...
191
        }
192
193
        return (bool) $this->readonly;
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function isValueSkipped()
200
    {
201
        if (is_callable($this->valueSkipped)) {
202
            return (bool) call_user_func($this->valueSkipped, $this->getModel());
0 ignored issues
show
Bug introduced by
It seems like $this->valueSkipped can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

202
            return (bool) call_user_func(/** @scrutinizer ignore-type */ $this->valueSkipped, $this->getModel());
Loading history...
203
        }
204
205
        return (bool) $this->valueSkipped;
206
    }
207
208
    /**
209
     * @param Closure|bool $valueSkipped
210
     *
211
     * @return $this
212
     */
213
    public function setValueSkipped($valueSkipped)
214
    {
215
        $this->valueSkipped = $valueSkipped;
216
217
        return $this;
218
    }
219
220
    /**
221
     * @param Closure|bool $readonly
222
     *
223
     * @return $this
224
     */
225
    public function setReadonly($readonly)
226
    {
227
        $this->readonly = $readonly;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return mixed
234
     */
235
    public function getValue()
236
    {
237
        return $this->value;
238
    }
239
240
    /**
241
     * @param mixed $value
242
     *
243
     * @return $this
244
     */
245
    public function setValue($value)
246
    {
247
        $this->value = $value;
248
249
        return $this;
250
    }
251
252
    /**
253
     * @param Request $request
254
     *
255
     * @return void
256
     */
257
    public function save(Request $request)
258
    {
259
    }
260
261
    /**
262
     * @param Request $request
263
     *
264
     * @return void
265
     */
266
    public function afterSave(Request $request)
267
    {
268
    }
269
270
    /**
271
     * @return array
272
     */
273
    public function toArray()
274
    {
275
        return [
276
            'value' => $this->getValue(),
277
            'readonly' => $this->isReadonly(),
278
            'model' => $this->getModel(),
279
        ];
280
    }
281
}
282