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 — bs4 ( 193743...d4da84 )
by butschster
20:32 queued 14:27
created

Select::getModifierValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

208
        return $this->setOptions(/** @scrutinizer ignore-type */ array_combine($values, $values));
Loading history...
209
    }
210
211
    /**
212
     * @return array
213
     */
214
    public function toArray()
215
    {
216
        return array_merge(parent::toArray(), [
217
            'options' => $this->mutateOptions(),
218
            'optionName' => $this->getOptionName($this->getModelValue()),
219
            'text' => $this->getModifierValue(),
220
        ]);
221
    }
222
223
    /**
224
     * @param \Illuminate\Http\Request $request
225
     * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException
226
     * @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException
227
     * @throws \SleepingOwl\Admin\Exceptions\Form\FormException
228
     */
229
    public function save(Request $request)
230
    {
231
        $model = $this->getModel();
232
233
        if (strpos($this->getName(), '.') !== false) {
234
            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...
235
                $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

235
                $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...
236
            } else {
237
                //@TODO Make Relation Resolver
238
                $relationName = explode('.', $this->getName());
0 ignored issues
show
Unused Code introduced by
The assignment to $relationName is dead and can be removed.
Loading history...
239
            }
240
        }
241
242
        $form = new FormDefault([
243
            new \SleepingOwl\Admin\Form\Element\Select(
244
                $this->getName()
245
            ),
246
        ]);
247
248
        $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...
249
250
        $form->setModelClass(get_class($model));
251
        $form->initialize();
252
        $form->setId($model->getKey());
253
254
        $form->saveForm($request);
255
    }
256
}
257