Completed
Push — master ( 650783...0c9838 )
by wen
15:06
created

Select   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 17.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 11
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getOptions() 11 23 5
A setOptions() 0 6 1
A toArray() 0 6 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\Display\Filters;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Sco\Admin\Traits\SelectOptionsFromModel;
7
8
class Select extends Filter
9
{
10
    use SelectOptionsFromModel;
11
12
    protected $type = 'select';
13
14
    protected $options;
15
16
    protected $defaultValue = '';
17
18
    public function __construct($name, $title, $options = null)
19
    {
20
        parent::__construct($name, $title);
21
22
        $this->setOptions($options);
23
    }
24
25
    /**
26
     * @return mixed
27
     */
28
    public function getOptions()
29
    {
30 View Code Duplication
        if ($this->options instanceof \Closure) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
            $options = ($this->options)();
32
        } elseif (is_string($this->options) || $this->options instanceof Model) {
33
            $options = $this->getOptionsFromModel();
34
        } elseif (is_array($this->options)) {
35
            $options = $this->options;
36
        } else {
37
            throw new InvalidArgumentException(
38
                "The select options must be return array(key=>value)"
39
            );
40
        }
41
42
        return collect($options)->mapWithKeys(function ($value, $key) {
43
            return [
44
                $key => [
45
                    'label' => $value,
46
                    'value' => (string) $key,
47
                ],
48
            ];
49
        })->values();
50
    }
51
52
    /**
53
     * @param mixed $options
54
     *
55
     * @return Select
56
     */
57
    public function setOptions($options)
58
    {
59
        $this->options = $options;
60
61
        return $this;
62
    }
63
64
    public function toArray()
65
    {
66
        return parent::toArray() + [
67
                'options' => $this->getOptions(),
68
            ];
69
    }
70
}
71