Completed
Push — master ( 10405b...259f27 )
by wen
13:10
created

MultiSelect   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 77
Duplicated Lines 16.88 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 13
loc 77
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A getValue() 0 15 4
A save() 0 6 3
A finishSave() 13 13 4
A isRelation() 0 4 1
A toArray() 0 7 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 Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Sco\Admin\Traits\HasSelectOptions;
7
8
class MultiSelect extends NamedElement
9
{
10
    use HasSelectOptions;
11
12
    protected $type = 'select';
13
14
    protected $defaultValue = [];
15
16
    /**
17
     *
18
     * @param string $name
19
     * @param string $title
20
     * @param array|Model $options
21
     */
22
    public function __construct(string $name, string $title, $options = null)
23
    {
24
        parent::__construct($name, $title);
25
26
        if (! is_null($options)) {
27
            $this->setOptions($options);
28
        }
29
30
        if (! ($this->isOptionsModel() && $this->isRelation())) {
31
            $this->setCast('json');
32
        }
33
    }
34
35
    public function getValue()
36
    {
37
        $value = parent::getValue();
38
39
        if ($this->isOptionsModel() && $this->isRelation()) {
40
            $model = $this->getOptionsModel();
41
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
42
43
            return collect($value)->pluck($key)->map(function ($item) {
44
                return (string) $item;
45
            });
46
        }
47
48
        return (array) $value;
49
    }
50
51
    public function save()
52
    {
53
        if (! ($this->isOptionsModel() && $this->isRelation())) {
54
            parent::save();
55
        }
56
    }
57
58 View Code Duplication
    public function finishSave()
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...
59
    {
60
        if (! ($this->isOptionsModel() && $this->isRelation())) {
61
            return;
62
        }
63
        $attribute = $this->getName();
64
        $values = $this->getValueFromRequest();
65
66
        $relation = $this->getModel()->{$attribute}();
67
        if ($relation instanceof BelongsToMany) {
68
            $relation->sync($values);
0 ignored issues
show
Bug introduced by
It seems like $values defined by $this->getValueFromRequest() on line 64 can also be of type string; however, Illuminate\Database\Eloq...sWithPivotTable::sync() does only seem to accept object<Illuminate\Databa...pport\Collection>|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
        }
70
    }
71
72
    protected function isRelation()
73
    {
74
        return method_exists($this->getModel(), $this->getName());
75
    }
76
77
    public function toArray()
78
    {
79
        return parent::toArray() + [
80
                'options'  => $this->getOptions(),
81
                'multiple' => true,
82
            ];
83
    }
84
}
85