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
Push — master ( a9bc98...b864c9 )
by butschster
10:50
created

FormElement::isReadonly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
rs 9.4285
1
<?php
2
3
namespace SleepingOwl\Admin\Form;
4
5
use Closure;
6
use SleepingOwl\Admin\Traits\Assets;
7
use Illuminate\Database\Eloquent\Model;
8
use SleepingOwl\Admin\Traits\VisibleCondition;
9
use SleepingOwl\Admin\Contracts\FormElementInterface;
10
11
abstract class FormElement implements FormElementInterface
12
{
13
    use Assets, VisibleCondition;
14
15
    /**
16
     * @var \SleepingOwl\Admin\Contracts\TemplateInterface
17
     */
18
    protected $template;
19
20
    /**
21
     * @var string|\Illuminate\View\View
22
     */
23
    protected $view;
24
25
    /**
26
     * @var Model
27
     */
28
    protected $model;
29
30
    /**
31
     * @var array
32
     */
33
    protected $validationRules = [];
34
35
    /**
36
     * @var array
37
     */
38
    protected $validationMessages = [];
39
40
    /**
41
     * @var bool
42
     */
43
    protected $readonly = false;
44
45
    public function __construct()
46
    {
47
        $this->initializePackage();
48
    }
49
50
    public function initialize()
51
    {
52
        $this->includePackage();
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getValidationMessages()
59
    {
60
        return $this->validationMessages;
61
    }
62
63
    /**
64
     * @param string $rule
65
     * @param string $message
66
     *
67
     * @return $this
68
     */
69
    public function addValidationMessage($rule, $message)
70
    {
71
        if (($pos = strpos($rule, ':')) !== false) {
72
            $rule = substr($rule, 0, $pos);
73
        }
74
75
        $this->validationMessages[$rule] = $message;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param array $validationMessages
82
     *
83
     * @return $this
84
     */
85
    public function setValidationMessages(array $validationMessages)
86
    {
87
        $this->validationMessages = $validationMessages;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getValidationLabels()
96
    {
97
        return [];
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getValidationRules()
104
    {
105
        return $this->validationRules;
106
    }
107
108
    /**
109
     * @param string $rule
110
     * @param string|null $message
111
     *
112
     * @return $this
113
     */
114
    public function addValidationRule($rule, $message = null)
115
    {
116
        $this->validationRules[] = $rule;
117
118
        if (is_null($message)) {
119
            return $this;
120
        }
121
122
        return $this->addValidationMessage($rule, $message);
123
    }
124
125
    /**
126
     * @param array|string $validationRules
127
     *
128
     * @return $this
129
     */
130
    public function setValidationRules($validationRules)
131
    {
132
        if (! is_array($validationRules)) {
133
            $validationRules = func_get_args();
134
        }
135
136
        $this->validationRules = [];
137
        foreach ($validationRules as $rule) {
138
            $rules = explode('|', $rule);
139
140
            foreach ($rules as $rule) {
141
                $this->addValidationRule($rule);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return string|\Illuminate\View\View
150
     */
151 View Code Duplication
    public function getView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        if (is_null($this->view)) {
154
            $name = (new \ReflectionClass($this))->getShortName();
155
            $this->view = 'form.element.'.strtolower($name);
156
        }
157
158
        return $this->view;
159
    }
160
161
    /**
162
     * @param \Illuminate\View\View|string $view
163
     *
164
     * @return $this
165
     */
166
    public function setView($view)
167
    {
168
        $this->view = $view;
169
170
        return $this;
171
    }
172
173
    /**
174
     * @return Model
175
     */
176
    public function getModel()
177
    {
178
        return $this->model;
179
    }
180
181
    /**
182
     * @param Model $model
183
     *
184
     * @return $this
185
     */
186
    public function setModel(Model $model)
187
    {
188
        $this->model = $model;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return bool
195
     */
196
    public function isReadonly()
197
    {
198
        if (is_callable($this->readonly)) {
199
            return (bool) call_user_func($this->readonly, $this->getModel());
200
        }
201
202
        return (bool) $this->readonly;
203
    }
204
205
    /**
206
     * @param Closure|bool $readonly
207
     *
208
     * @return $this
209
     */
210
    public function setReadonly($readonly)
211
    {
212
        $this->readonly = $readonly;
0 ignored issues
show
Documentation Bug introduced by
It seems like $readonly can also be of type object<Closure>. However, the property $readonly is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
213
214
        return $this;
215
    }
216
217
    /**
218
     * @return mixed
219
     */
220
    public function getValue()
221
    {
222
    }
223
224
    public function save()
225
    {
226
    }
227
228
    public function afterSave()
229
    {
230
    }
231
232
    /**
233
     * @return array
234
     */
235
    public function toArray()
236
    {
237
        return [
238
            'value' => $this->getValue(),
239
            'readonly' => $this->isReadonly(),
240
            'model' => $this->getModel(),
241
        ];
242
    }
243
244
    /**
245
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
246
     */
247
    public function render()
248
    {
249
        return app('sleeping_owl.template')->view($this->getView(), $this->toArray())->render();
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function __toString()
256
    {
257
        return (string) $this->render();
258
    }
259
}
260