Completed
Push — master ( c6a049...7f7ede )
by Song
03:29
created

HasElementNames::getSelectedRowsName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
trait HasElementNames
6
{
7
    /**
8
     * Grid name.
9
     *
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * HTML element names.
16
     *
17
     * @var array
18
     */
19
    protected $elementNames = [
20
        'grid_row'        => 'grid-row',
21
        'grid_select_all' => 'grid-select-all',
22
        'grid_per_page'   => 'grid-per-pager',
23
        'grid_batch'      => 'grid-batch',
24
        'export_selected' => 'export-selected',
25
        'selected_rows'   => 'selectedRows',
26
    ];
27
28
    /**
29
     * Set name to grid.
30
     *
31
     * @param string $name
32
     *
33
     * @return $this
34
     */
35
    public function setName($name)
36
    {
37
        $this->name = $name;
38
39
        $this->model()->setPerPageName("{$name}_{$this->model()->getPerPageName()}");
0 ignored issues
show
Bug introduced by
It seems like model() 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...
40
41
        $this->getFilter()->setName($name);
0 ignored issues
show
Bug introduced by
It seems like getFilter() 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...
42
43
        return $this;
44
    }
45
46
    /**
47
     * Get name of grid.
48
     *
49
     * @return string
50
     */
51
    public function getName()
52
    {
53
        return $this->name;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getGridRowName()
60
    {
61
        return $this->elementNameWithPrefix('grid_row');
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getSelectAllName()
68
    {
69
        return $this->elementNameWithPrefix('grid_select_all');
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getPerPageName()
76
    {
77
        return $this->elementNameWithPrefix('grid_per_page');
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getGridBatchName()
84
    {
85
        return $this->elementNameWithPrefix('grid_batch');
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getExportSelectedName()
92
    {
93
        return $this->elementNameWithPrefix('export_selected');
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getSelectedRowsName()
100
    {
101
        $elementName = $this->elementNames['selected_rows'];
102
103
        if ($this->name) {
104
            return sprintf('%s%s', $this->name, ucfirst($elementName));
105
        }
106
107
        return $elementName;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    protected function elementNameWithPrefix($name)
114
    {
115
        $elementName = $this->elementNames[$name];
116
117
        if ($this->name) {
118
            return sprintf('%s-%s', $this->name, $elementName);
119
        }
120
121
        return $elementName;
122
    }
123
}
124