Completed
Push — master ( 5fc65a...affc4f )
by Song
02:25
created

HasHeader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 11.57 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 14
loc 121
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addHeader() 0 11 2
A addSorter() 0 8 1
A addHelp() 0 4 1
B addFilter() 0 24 6
A bindFilterQuery() 0 6 2
A renderHeader() 14 14 3

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\Grid\Column;
4
5
use Encore\Admin\Grid\Column;
6
use Encore\Admin\Grid\Model;
7
use Illuminate\Contracts\Support\Htmlable;
8
use Illuminate\Contracts\Support\Renderable;
9
10
trait HasHeader
11
{
12
    /**
13
     * @var Filter
14
     */
15
    public $filter;
16
17
    /**
18
     * @var array
19
     */
20
    protected $headers = [];
21
22
    /**
23
     * Add contents to column header.
24
     *
25
     * @param string|Renderable|Htmlable $header
26
     *
27
     * @return $this
28
     */
29
    public function addHeader($header)
30
    {
31
        if ($header instanceof Filter) {
32
            $header->setParent($this);
33
            $this->filter = $header;
34
        }
35
36
        $this->headers[] = $header;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Add a column sortable to column header.
43
     *
44
     * @param string $cast
45
     *
46
     * @return Column|string
47
     */
48
    protected function addSorter($cast = null)
49
    {
50
        $sortName = $this->grid->model()->getSortName();
0 ignored issues
show
Bug introduced by
The property grid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
52
        $sorter = new Sorter($sortName, $this->getName(), $cast);
0 ignored issues
show
Bug introduced by
It seems like getName() 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...
53
54
        return $this->addHeader($sorter);
55
    }
56
57
    /**
58
     * Add a help tooltip to column header.
59
     *
60
     * @param string $message
61
     *
62
     * @return $this
63
     */
64
    protected function addHelp($message)
65
    {
66
        return $this->addHeader(new Help($message));
67
    }
68
69
    /**
70
     * Add a filter to column header.
71
     *
72
     * @return $this
73
     */
74
    protected function addFilter($type = null, $formal = null)
75
    {
76
        if (is_array($type)) {
77
            return $this->addHeader(new CheckFilter($type));
78
        }
79
80
        if (is_null($type)) {
81
            $type = 'equal';
82
        }
83
84
        if (in_array($type, ['equal', 'like', 'date', 'time', 'datetime'])) {
85
            return $this->addHeader(new InputFilter($type));
86
        }
87
88
        if ($type === 'range') {
89
            if (is_null($formal)) {
90
                $formal = 'equal';
91
            }
92
93
            return $this->addHeader(new RangeFilter($formal));
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Add a binding based on filter to the model query.
101
     *
102
     * @param Model $model
103
     */
104
    public function bindFilterQuery(Model $model)
105
    {
106
        if ($this->filter) {
107
            $this->filter->addBinding(request($this->getName()), $model);
0 ignored issues
show
Bug introduced by
It seems like getName() 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...
108
        }
109
    }
110
111
    /**
112
     * Render Column header.
113
     *
114
     * @return string
115
     */
116 View Code Duplication
    public function renderHeader()
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...
117
    {
118
        return collect($this->headers)->map(function ($item) {
119
            if ($item instanceof Renderable) {
120
                return $item->render();
121
            }
122
123
            if ($item instanceof Htmlable) {
124
                return $item->toHtml();
125
            }
126
127
            return (string)$item;
128
        })->implode('');
129
    }
130
}
131