MultiSelect   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 17.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 5
dl 13
loc 75
ccs 0
cts 48
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
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
/**
9
 * Class MultiSelect
10
 *
11
 * @package Sco\Admin\Form\Elements
12
 * @see http://element.eleme.io/#/en-US/component/select
13
 */
14
class MultiSelect extends NamedElement
15
{
16
    use HasSelectOptions;
17
18
    protected $type = 'select';
19
20
    protected $cast = 'json';
21
22
    protected $defaultValue = [];
23
24
    /**
25
     *
26
     * @param string $name
27
     * @param string $title
28
     * @param array|Model $options
29
     */
30
    public function __construct(string $name, string $title, $options = null)
31
    {
32
        parent::__construct($name, $title);
33
34
        if (! is_null($options)) {
35
            $this->setOptions($options);
36
        }
37
    }
38
39
    public function getValue()
40
    {
41
        $value = parent::getValue();
42
43
        if ($this->isOptionsModel() && $this->isRelation()) {
44
            $model = $this->getOptionsModel();
45
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
46
47
            return collect($value)->pluck($key)->map(function ($item) {
48
                return (string) $item;
49
            });
50
        }
51
52
        return (array) $value;
53
    }
54
55
    public function save()
56
    {
57
        if (! ($this->isOptionsModel() && $this->isRelation())) {
58
            return parent::save();
59
        }
60
    }
61
62 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...
63
    {
64
        if (! ($this->isOptionsModel() && $this->isRelation())) {
65
            return;
66
        }
67
        $attribute = $this->getName();
68
        $values = $this->getValueFromRequest();
69
70
        $relation = $this->getModel()->{$attribute}();
71
        if ($relation instanceof BelongsToMany) {
72
            $relation->sync($values);
0 ignored issues
show
Bug introduced by
It seems like $values defined by $this->getValueFromRequest() on line 68 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...
73
        }
74
    }
75
76
    protected function isRelation()
77
    {
78
        return method_exists($this->getModel(), $this->getName());
79
    }
80
81
    public function toArray()
82
    {
83
        return parent::toArray() + [
84
                'options'  => $this->getOptions(),
85
                'multiple' => true,
86
            ];
87
    }
88
}
89