Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Radio   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 27.72 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 4
dl 28
loc 101
rs 10
c 0
b 0
f 0
ccs 0
cts 75
cp 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B isCurrentOption() 17 30 7
A shouldSkip() 0 4 1
A afterStore() 0 4 1
A afterUpdate() 11 11 3
A columns() 0 6 1
A getColumns() 0 4 1
A getListView() 0 8 1
A getEditFormView() 0 9 2
A getCreateFormView() 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 Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
8
use Yaro\Jarboe\Table\Fields\Traits\Relations;
9
10
class Radio extends AbstractField
11
{
12
    use Orderable;
13
    use Relations;
14
15
    protected $columns = 1;
16
17
    public function isCurrentOption($option, $model = null, $relationIndex = 0): bool
18
    {
19
        if ($this->hasOld()) {
20 View Code Duplication
            if ($this->isGroupedRelation()) {
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...
21
                $option = crc32($this->relations[$relationIndex]['group']) .'~~~'. $option;
22
            }
23
            return $this->old() == $option;
24
        }
25
26
        if (is_null($model)) {
27
            return $option == $this->getDefault();
28
        }
29
30 View Code Duplication
        if ($this->isRelationField()) {
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
            $related = $model->{$this->getRelationMethod($relationIndex)};
32
            if ($related) {
33
                $relatedModelClass = get_class($model->{$this->getRelationMethod($relationIndex)}()->getRelated());
34
                $freshRelatedModel = new $relatedModelClass;
35
                $collection = $related;
36
                if (!is_a($related, Collection::class)) {
37
                    $collection = collect([$related]);
38
                }
39
40
                return $collection->contains($freshRelatedModel->getKeyName(), $option);
41
            }
42
            return false;
43
        }
44
45
        return $option == $model->{$this->name};
46
    }
47
48
    public function shouldSkip(Request $request)
49
    {
50
        return $this->isRelationField();
51
    }
52
53
    public function afterStore($model, Request $request)
54
    {
55
        $this->afterUpdate($model, $request);
56
    }
57
58 View Code Duplication
    public function afterUpdate($model, Request $request)
59
    {
60
        if (!$this->isRelationField()) {
61
            return;
62
        }
63
        if ($this->isReadonly()) {
64
            return;
65
        }
66
67
        $this->syncRelations($model, $request->get($this->name()));
68
    }
69
70
    public function columns(int $columns)
71
    {
72
        $this->columns = $columns;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getColumns(): int
81
    {
82
        return $this->columns;
83
    }
84
85
    public function getListView($model)
86
    {
87
        return view('jarboe::crud.fields.radio.list', [
88
            'model' => $model,
89
            'field' => $this,
90
            'options' => $this->getOptions(),
91
        ]);
92
    }
93
94
    public function getEditFormView($model)
95
    {
96
        $template = $this->isReadonly() ? 'readonly' : 'edit';
97
98
        return view('jarboe::crud.fields.radio.'. $template, [
99
            'model' => $model,
100
            'field' => $this,
101
        ]);
102
    }
103
104
    public function getCreateFormView()
105
    {
106
        return view('jarboe::crud.fields.radio.create', [
107
            'field' => $this,
108
        ]);
109
    }
110
}
111