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 — master (#688)
by
unknown
18:52
created

Select   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 14.45 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 25
loc 173
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 4
A setSortable() 0 6 1
A isSortable() 0 4 1
A getOptions() 15 15 4
A mutateOptions() 0 12 2
A getOptionName() 0 10 3
A setOptions() 0 6 1
A setEnum() 0 4 1
A toArray() 0 7 1
A save() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 array
22
     */
23
    protected $options = [];
24
25
    /**
26
     * @var array
27
     */
28
    protected $optionList = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $exclude = [];
34
35
    /**
36
     * @var bool
37
     */
38
    protected $sortable = true;
39
40
    /**
41
     * Text constructor.
42
     *
43
     * @param             $name
44
     * @param             $label
45
     */
46 View Code Duplication
    public function __construct($name, $label = null, $options = [])
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...
47
    {
48
        parent::__construct($name, $label);
49
50
        if (is_array($options)) {
51
            $this->setOptions($options);
52
        } elseif (($options instanceof Model) || is_string($options)) {
53
            $this->setModelForOptions($options);
54
        }
55
    }
56
57
    /**
58
     * @param bool $sortable
59
     *
60
     * @return $this
61
     */
62
    public function setSortable($sortable)
63
    {
64
        $this->sortable = (bool) $sortable;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isSortable()
73
    {
74
        return $this->sortable;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 View Code Duplication
    public function getOptions()
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...
81
    {
82
        if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) {
83
            $this->setOptions(
84
                $this->loadOptions()
85
            );
86
        }
87
88
        $options = array_except($this->options, $this->exclude);
89
        if ($this->isSortable()) {
90
            asort($options);
91
        }
92
93
        return $options;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function mutateOptions()
100
    {
101
        $options = [];
102
103
        $this->optionList = $this->getOptions();
104
105
        foreach ($this->optionList as $key => $value) {
106
            $options[] = ['value' => $key, 'text' => $value];
107
        }
108
109
        return $options;
110
    }
111
112
    /**
113
     * @param $key
114
     * @return mixed|null
115
     */
116
    public function getOptionName($value)
117
    {
118
        if (isset($value)) {
119
            if (isset($this->optionList[$value])) {
120
                return $this->optionList[$value];
121
            }
122
123
            return $value;
124
        }
125
    }
126
127
    /**
128
     * @param array
129
     *
130
     * @return $this
131
     */
132
    public function setOptions(array $options)
133
    {
134
        $this->options = $options;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param array $values
141
     *
142
     * @return $this
143
     */
144
    public function setEnum(array $values)
145
    {
146
        return $this->setOptions(array_combine($values, $values));
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function toArray()
153
    {
154
        return parent::toArray() + [
155
                'options'        => $this->mutateOptions(),
156
                'optionName'     => $this->getOptionName($this->getModelValue()),
157
            ];
158
    }
159
160
    /**
161
     * @param Request $request
162
     *
163
     * @return void
164
     */
165
    public function save(Request $request)
166
    {
167
        $form = new FormDefault([
168
            new \SleepingOwl\Admin\Form\Element\Select(
169
                $this->getName()
170
            ),
171
        ]);
172
173
        $model = $this->getModel();
174
175
        $request->offsetSet($this->getName(), $request->input('value', null));
176
177
        $form->setModelClass(get_class($model));
178
        $form->initialize();
179
        $form->setId($model->getKey());
180
181
        $form->saveForm($request);
182
    }
183
}
184