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 ( f7a12e...ca282c )
by Dave
08:55
created

Select::getDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column\Editable;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Form\FormDefault;
8
use SleepingOwl\Admin\Traits\SelectOptionsFromModel;
9
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;
10
11
class Select extends EditableColumn implements ColumnEditableInterface
12
{
13
    use SelectOptionsFromModel;
14
15
    /**
16
     * @var string
17
     */
18
    protected $view = 'column.editable.select';
19
20
    /**
21
     * @var null
22
     */
23
    protected $relationKey = null;
24
    /**
25
     * @var array
26
     */
27
    protected $options = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $optionList = [];
33
34
    /**
35
     * @var array
36
     */
37
    protected $exclude = [];
38
39
    /**
40
     * @var bool
41
     */
42
    protected $sortable = true;
43
44
    /**
45
     * @var null
46
     */
47
    protected $defaultValue = null;
48
49
    /**
50
     * Select constructor.
51
     * @param $name
52
     * @param null $label
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $label is correct as it would always require null to be passed?
Loading history...
53
     * @param array $options
54
     * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException
55
     */
56
    public function __construct($name, $label = null, $options = [])
57
    {
58
        parent::__construct($name, $label);
59
60
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
61
            $this->setOptions($options);
62
        } elseif (($options instanceof Model) || is_string($options)) {
63
            $this->setModelForOptions($options);
64
        }
65
    }
66
67
    /**
68
     * @param $relationKey
69
     * @return $this
70
     */
71
    public function setRelationKey($relationKey)
72
    {
73
        $this->relationKey = $relationKey;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return null
80
     */
81
    public function getRelationKey()
82
    {
83
        return $this->relationKey;
84
    }
85
86
    /**
87
     * @param $defaultValue
88
     * @return $this
89
     */
90
    public function setDefaultValue($defaultValue)
91
    {
92
        $this->defaultValue = $defaultValue;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return null
99
     */
100
    public function getDefaultValue()
101
    {
102
        return $this->defaultValue;
103
    }
104
105
    /**
106
     * @param bool $sortable
107
     *
108
     * @return $this
109
     */
110
    public function setSortable($sortable)
111
    {
112
        $this->sortable = (bool) $sortable;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function isSortable()
121
    {
122
        return $this->sortable;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getOptions()
129
    {
130
        if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) {
131
            $this->setOptions(
132
                $this->loadOptions()
133
            );
134
        }
135
136
        $options = array_except($this->options, $this->exclude);
137
        if ($this->isSortable()) {
138
            asort($options);
139
        }
140
141
        return $options;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function mutateOptions()
148
    {
149
        $options = [];
150
151
        $this->optionList = $this->getOptions();
152
153
        foreach ($this->optionList as $key => $value) {
154
            $options[] = ['value' => $key, 'text' => $value];
155
        }
156
157
        return $options;
158
    }
159
160
    /**
161
     * @param $key
162
     * @return mixed|null
163
     */
164
    public function getOptionName($value)
165
    {
166
        if (isset($value)) {
167
            if (isset($this->optionList[$value])) {
168
                return $this->optionList[$value];
169
            }
170
171
            return $value;
172
        }
173
    }
174
175
    /**
176
     * @param array
177
     *
178
     * @return $this
179
     */
180
    public function setOptions(array $options)
181
    {
182
        $this->options = $options;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @param array $values
189
     *
190
     * @return $this
191
     */
192
    public function setEnum(array $values)
193
    {
194
        return $this->setOptions(array_combine($values, $values));
195
    }
196
197
    /**
198
     * @return array
199
     */
200
    public function toArray()
201
    {
202
        return parent::toArray() + [
203
                'options'    => $this->mutateOptions(),
204
                'optionName' => $this->getOptionName($this->getModelValue()),
205
            ];
206
    }
207
208
    /**
209
     * @param Request $request
210
     * @throws \SleepingOwl\Admin\Exceptions\Form\FormException
211
     */
212
    public function save(Request $request)
213
    {
214
        $model = $this->getModel();
215
216
        if (strpos($this->getName(), '.') !== false) {
217
            if ($this->getRelationKey()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getRelationKey() targeting SleepingOwl\Admin\Displa...elect::getRelationKey() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
218
                $this->setName($this->getRelationKey());
0 ignored issues
show
Bug introduced by
$this->getRelationKey() of type void is incompatible with the type string expected by parameter $name of SleepingOwl\Admin\Displa...\NamedColumn::setName(). ( Ignorable by Annotation )

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

218
                $this->setName(/** @scrutinizer ignore-type */ $this->getRelationKey());
Loading history...
Bug introduced by
Are you sure the usage of $this->getRelationKey() targeting SleepingOwl\Admin\Displa...elect::getRelationKey() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
219
            } else {
220
                //@TODO Make Relation Resolver
221
                $relationName = explode('.', $this->getName());
0 ignored issues
show
Unused Code introduced by
The assignment to $relationName is dead and can be removed.
Loading history...
222
            }
223
        }
224
225
        $form = new FormDefault([
226
            new \SleepingOwl\Admin\Form\Element\Select(
227
                $this->getName()
228
            ),
229
        ]);
230
231
        $request->offsetSet($this->getName(), $request->input('value', $this->getDefaultValue()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getDefaultValue() targeting SleepingOwl\Admin\Displa...lect::getDefaultValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
232
233
        $form->setModelClass(get_class($model));
234
        $form->initialize();
235
        $form->setId($model->getKey());
236
237
        $form->saveForm($request);
238
    }
239
}
240