Completed
Push — master ( 675c3f...4830ba )
by wen
11:40
created

Tree   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 56.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 15
c 4
b 1
f 2
lcom 1
cbo 5
dl 41
loc 73
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getValue() 17 17 5
A save() 0 6 3
A finishSave() 13 13 4
A parseOptions() 11 11 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\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Sco\Admin\Form\Elements\Concerns\HasOptions;
7
8
class Tree extends NamedElement
9
{
10
    use HasOptions;
11
12
    protected $type = 'tree';
13
14
    protected $defaultValue = [];
15
16
    public function __construct($name, $title, $options)
17
    {
18
        parent::__construct($name, $title);
19
20
        $this->setOptions($options);
21
    }
22
23 View Code Duplication
    public function getValue()
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...
24
    {
25
        $value = $this->getValueFromModel();
26
        if (empty($value)) {
27
            return $value;
28
        }
29
30
        if ($this->isOptionsModel() && $this->isRelation()) {
31
            $model = $this->getOptionsModel();
32
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
33
34
            $value = $value->pluck($key)->map(function ($item) {
35
                return (string)$item;
36
            });
37
        }
38
        return $value;
39
    }
40
41
    public function save()
42
    {
43
        if (!($this->isOptionsModel() && $this->isRelation())) {
44
            parent::save();
45
        }
46
    }
47
48 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...
49
    {
50
        if (!($this->isOptionsModel() && $this->isRelation())) {
51
            return;
52
        }
53
        $attribute = $this->getName();
54
        $values = $this->getValueFromRequest();
55
56
        $relation = $this->getModel()->{$attribute}();
57
        if ($relation instanceof BelongsToMany) {
58
            $relation->sync($values);
59
        }
60
    }
61
62 View Code Duplication
    protected function parseOptions($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...
63
    {
64
        return collect($options)->mapWithKeys(function ($value, $key) {
65
            return [
66
                $key => [
67
                    'id' => (string)$key,
68
                    'label' => $value,
69
                ],
70
            ];
71
        })->values();
72
    }
73
74
    public function toArray()
75
    {
76
        return parent::toArray() + [
77
            'options' => $this->getOptions(),
78
        ];
79
    }
80
}
81