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.
Passed
Push — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

MultiSelect::syncBelongsToManyRelation()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
cc 6
eloc 12
nc 9
nop 2
ccs 0
cts 14
cp 0
crap 42
rs 8.8571
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Collection;
7
8
class MultiSelect extends Select
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $taggable = false;
14
15
    /**
16
     * @var \Closure
17
     */
18
    protected $pivotCallback;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $deleteRelatedItem = false;
24
25
    /**
26
     * @var string
27
     */
28
    protected $view = 'form.element.select';
29
30
    /**
31
     * @return string
32
     */
33
    public function getName()
34
    {
35
        return parent::getName().'[]';
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getValueFromModel()
42
    {
43
        $value = parent::getValueFromModel();
44
        if ($value instanceof Collection && $value->count() > 0) {
45
            $value = $value->pluck($value->first()->getKeyName())->all();
46
        }
47
48
        if ($value instanceof Collection) {
49
            $value = $value->toArray();
50
        }
51
52
        return $value;
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function isTaggable()
59
    {
60
        return $this->taggable;
61
    }
62
63
    /**
64
     * @return $this
65
     */
66
    public function taggable()
67
    {
68
        $this->taggable = true;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return \Closure
75
     */
76
    public function getPivotCallback()
77
    {
78
        return $this->pivotCallback;
79
    }
80
81
    /**
82
     * @param \Closure $callable
83
     * @return $this
84
     */
85
    public function setPivotCallback(\Closure $callable)
86
    {
87
        $this->pivotCallback = $callable;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isDeleteRelatedItem()
96
    {
97
        return $this->deleteRelatedItem;
98
    }
99
100
    /**
101
     * @return $this
102
     */
103
    public function deleteRelatedItem()
104
    {
105
        $this->deleteRelatedItem = true;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function toArray()
114
    {
115
        $this->setHtmlAttributes([
116
            'id'    => $this->getName(),
117
            'class' => 'form-control input-select',
118
            'multiple',
119
        ]);
120
121
        if ($this->isTaggable()) {
122
            $this->setHtmlAttribute('class', 'input-taggable');
123
        }
124
125
        if ($this->isReadonly()) {
126
            $this->setHtmlAttribute('disabled', 'disabled');
127
        }
128
129
        return [
130
                'tagable'    => $this->isTaggable(),
131
                'attributes' => $this->getHtmlAttributes(),
132
            ] + parent::toArray();
133
    }
134
135
    /**
136
     * @param \Illuminate\Http\Request $request
137
     *
138
     * @return void
139
     */
140
    public function save(\Illuminate\Http\Request $request)
141
    {
142
        if (is_null($this->getModelForOptions())) {
143
            parent::save($request);
144
        }
145
    }
146
147
    /**
148
     * @param \Illuminate\Http\Request $request
149
     *
150
     * @return void
151
     */
152
    public function afterSave(\Illuminate\Http\Request $request)
153
    {
154
        if (is_null($this->getModelForOptions())) {
155
            return;
156
        }
157
158
        $attribute = $this->getModelAttributeKey();
159
160
        if (is_null($request->input($this->getPath()))) {
161
            $values = [];
162
        } else {
163
            $values = $this->getValueFromModel();
164
        }
165
166
        $relation = $this->getModel()->{$attribute}();
167
168
        if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) {
169
            $this->syncBelongsToManyRelation($relation, $values);
170
        } elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) {
171
            $this->deleteOldItemsFromHasManyRelation($relation, $values);
172
            $this->attachItemsToHasManyRelation($relation, $values);
173
        }
174
    }
175
176
    /**
177
     * @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation
178
     * @param array $values
179
     *
180
     * @return void
181
     */
182
    protected function syncBelongsToManyRelation(
183
        \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation,
184
        array $values
185
    ) {
186
        foreach ($values as $i => $value) {
187
            if (! array_key_exists($value, $this->getOptions()) and $this->isTaggable()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
188
                $model = clone $this->getModelForOptions();
189
                $model->{$this->getDisplay()} = $value;
190
                $model->save();
191
192
                $values[$i] = $model->getKey();
193
            }
194
        }
195
196
        if (is_callable($this->getPivotCallback())) {
197
            $values = call_user_func($this->pivotCallback, $values) ?: $values;
198
        }
199
200
        $relation->sync($values);
201
    }
202
203
    /**
204
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
205
     * @param array $values
206
     */
207
    protected function deleteOldItemsFromHasManyRelation(
208
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
209
        array $values
210
    ) {
211
        $items = $relation->get();
212
213
        foreach ($items as $item) {
214
            if (! in_array($item->getKey(), $values)) {
215
                if ($this->isDeleteRelatedItem()) {
216
                    $item->delete();
217
                } else {
218
                    $item->{$this->getForeignKeyNameFromRelation($relation)} = null;
219
                    $item->save();
220
                }
221
            }
222
        }
223
    }
224
225
    /**
226
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
227
     * @param array $values
228
     */
229
    protected function attachItemsToHasManyRelation(
230
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
231
        array $values
232
    ) {
233
        foreach ($values as $i => $value) {
234
            /** @var Model $model */
235
            $model = clone $this->getModelForOptions();
236
            $item = $model->find($value);
237
238
            if (is_null($item)) {
239
                if (! $this->isTaggable()) {
240
                    continue;
241
                }
242
243
                $model->{$this->getDisplay()} = $value;
244
                $item = $model;
245
            }
246
247
            $relation->save($item);
248
        }
249
    }
250
}
251