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 — analysis-8A5djK ( 15f6a3 )
by butschster
08:05
created

Select::setRelationKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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
     * Select constructor.
46
     * @param $name
47
     * @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...
48
     * @param array $options
49
     * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException
50
     */
51
    public function __construct($name, $label = null, $options = [])
52
    {
53
        parent::__construct($name, $label);
54
55
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
56
            $this->setOptions($options);
57
        } elseif (($options instanceof Model) || is_string($options)) {
58
            $this->setModelForOptions($options);
59
        }
60
    }
61
62
    /**
63
     * @param $relationKey
64
     * @return $this
65
     */
66
    public function setRelationKey($relationKey)
67
    {
68
        $this->relationKey = $relationKey;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return null
75
     */
76
    public function getRelationKey()
77
    {
78
        return $this->relationKey;
79
    }
80
81
    /**
82
     * @param bool $sortable
83
     *
84
     * @return $this
85
     */
86
    public function setSortable($sortable)
87
    {
88
        $this->sortable = (bool) $sortable;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isSortable()
97
    {
98
        return $this->sortable;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getOptions()
105
    {
106
        if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) {
107
            $this->setOptions(
108
                $this->loadOptions()
109
            );
110
        }
111
112
        $options = array_except($this->options, $this->exclude);
113
        if ($this->isSortable()) {
114
            asort($options);
115
        }
116
117
        return $options;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function mutateOptions()
124
    {
125
        $options = [];
126
127
        $this->optionList = $this->getOptions();
128
129
        foreach ($this->optionList as $key => $value) {
130
            $options[] = ['value' => $key, 'text' => $value];
131
        }
132
133
        return $options;
134
    }
135
136
    /**
137
     * @param $key
138
     * @return mixed|null
139
     */
140
    public function getOptionName($value)
141
    {
142
        if (isset($value)) {
143
            if (isset($this->optionList[$value])) {
144
                return $this->optionList[$value];
145
            }
146
147
            return $value;
148
        }
149
    }
150
151
    /**
152
     * @param array
153
     *
154
     * @return $this
155
     */
156
    public function setOptions(array $options)
157
    {
158
        $this->options = $options;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param array $values
165
     *
166
     * @return $this
167
     */
168
    public function setEnum(array $values)
169
    {
170
        return $this->setOptions(array_combine($values, $values));
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function toArray()
177
    {
178
        return parent::toArray() + [
179
                'options'    => $this->mutateOptions(),
180
                'optionName' => $this->getOptionName($this->getModelValue()),
181
            ];
182
    }
183
184
    /**
185
     * @param Request $request
186
     * @throws \SleepingOwl\Admin\Exceptions\Form\FormException
187
     */
188
    public function save(Request $request)
189
    {
190
        $model = $this->getModel();
191
192
        if (strpos($this->getName(), '.') !== false) {
193
            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...
194
                $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

194
                $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...
195
            } else {
196
                //@TODO Make Relation Resolver
197
                $relationName = explode('.', $this->getName());
0 ignored issues
show
Unused Code introduced by
The assignment to $relationName is dead and can be removed.
Loading history...
198
            }
199
        }
200
201
        $form = new FormDefault([
202
            new \SleepingOwl\Admin\Form\Element\Select(
203
                $this->getName()
204
            ),
205
        ]);
206
207
        $request->offsetSet($this->getName(), $request->input('value', null));
208
209
        $form->setModelClass(get_class($model));
210
        $form->initialize();
211
        $form->setId($model->getKey());
212
213
        $form->saveForm($request);
214
    }
215
}
216