Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

BelongsToRelation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 155
Duplicated Lines 24.52 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 38
loc 155
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setSelectable() 5 10 3
A getSelectable() 0 4 1
A getLoadUrl() 0 6 1
A addHtml() 33 33 1
A addStyle() 0 29 1
A makeGrid() 0 7 1
A render() 0 13 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 Encore\Admin\Form\Field;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Selectable;
7
8
trait BelongsToRelation
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $modalID;
14
15
    /**
16
     * @var string
17
     */
18
    protected $selectable;
19
20
    /**
21
     * BelongsToRelation constructor.
22
     * @param string $column
23
     * @param array $arguments
24
     */
25
    public function __construct($column, $arguments = [])
26
    {
27
        $this->setSelectable($arguments[0]);
28
29
        parent::__construct($column, array_slice($arguments, 1));
30
    }
31
32
    /**
33
     * @param string $selectable
34
     */
35
    protected function setSelectable($selectable)
36
    {
37 View Code Duplication
        if (!class_exists($selectable) || !is_subclass_of($selectable, Selectable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Encore\Admin\Grid\Selectable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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...
38
            throw new \InvalidArgumentException(
39
                "[Class [{$selectable}] must be a sub class of Encore\Admin\Grid\Selectable"
40
            );
41
        }
42
43
        $this->selectable = $selectable;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getSelectable()
50
    {
51
        return $this->selectable;
52
    }
53
54
    /**
55
     * @param int $multiple
56
     * @return string
57
     */
58
    protected function getLoadUrl($multiple = 0)
59
    {
60
        $selectable = str_replace('\\', '_', $this->selectable);
61
62
        return route('admin.handle-selectable', compact('selectable', 'multiple'));
63
    }
64
65
    /**
66
     * @return $this
67
     */
68 View Code Duplication
    public function addHtml()
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...
69
    {
70
        $trans = [
71
            'choose' => admin_trans('admin.choose'),
72
            'cancal' => admin_trans('admin.cancel'),
73
            'submit' => admin_trans('admin.submit'),
74
        ];
75
76
        $html = <<<HTML
77
<div class="modal fade" id="{$this->modalID}" tabindex="-1" role="dialog">
78
  <div class="modal-dialog modal-lg" role="document">
79
    <div class="modal-content" style="border-radius: 5px;">
80
      <div class="modal-header">
81
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
82
            <span aria-hidden="true">&times;</span>
83
        </button>
84
        <h4 class="modal-title">{$trans['choose']}</h4>
85
      </div>
86
      <div class="modal-body">
87
        <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
88
      </div>
89
      <div class="modal-footer">
90
        <button type="button" class="btn btn-default" data-dismiss="modal">{$trans['cancal']}</button>
91
        <button type="button" class="btn btn-primary submit">{$trans['submit']}</button>
92
      </div>
93
    </div>
94
  </div>
95
</div>
96
HTML;
97
        Admin::html($html);
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return $this
104
     */
105
    public function addStyle()
106
    {
107
        $style = <<<STYLE
108
#{$this->modalID} tr {
109
    cursor: pointer;
110
}
111
#{$this->modalID} .box {
112
    border-top: none;
113
    margin-bottom: 0;
114
    box-shadow: none;
115
}
116
117
.grid-table .empty-grid {
118
    padding: 20px !important;
119
}
120
121
.grid-table .empty-grid svg {
122
    width: 60px !important;
123
    height: 60px !important;
124
}
125
.grid-box .box-footer {
126
    border-top: none !important;
127
}
128
STYLE;
129
130
        Admin::style($style);
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return \Encore\Admin\Grid
137
     */
138
    protected function makeGrid()
139
    {
140
        /** @var Selectable $selectable */
141
        $selectable = new $this->selectable;
142
143
        return $selectable->renderFormGrid($this->value());
0 ignored issues
show
Bug introduced by
It seems like value() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function render()
150
    {
151
        $this->modalID = sprintf('modal-selector-%s', $this->getElementClassString());
0 ignored issues
show
Bug introduced by
It seems like getElementClassString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
152
153
        $this->addScript()->addHtml()->addStyle();
0 ignored issues
show
Bug introduced by
It seems like addScript() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
154
155
        $this->addVariables([
0 ignored issues
show
Bug introduced by
It seems like addVariables() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
156
            'grid'    => $this->makeGrid(),
157
            'options' => $this->getOptions(),
0 ignored issues
show
Bug introduced by
It seems like getOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
158
        ]);
159
160
        return parent::fieldRender();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fieldRender() instead of render()). Are you sure this is correct? If so, you might want to change this to $this->fieldRender().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
161
    }
162
}
163