Completed
Push — master ( 8187a2...82be35 )
by Song
02:30
created

FixColumns::addScript()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 87
rs 8.2836
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid;
7
use Illuminate\Support\Collection;
8
9
class FixColumns
10
{
11
    /**
12
     * @var Grid
13
     */
14
    protected $grid;
15
16
    /**
17
     * @var int
18
     */
19
    protected $head;
20
21
    /**
22
     * @var int
23
     */
24
    protected $tail;
25
26
    /**
27
     * @var Collection
28
     */
29
    protected $left;
30
31
    /**
32
     * @var Collection
33
     */
34
    protected $right;
35
36
    /**
37
     * @var string
38
     */
39
    protected $view = 'admin::grid.fixed-table';
40
41
    /**
42
     * FixColumns constructor.
43
     *
44
     * @param Grid $grid
45
     * @param int  $head
46
     * @param int  $tail
47
     */
48
    public function __construct(Grid $grid, $head, $tail = -1)
49
    {
50
        $this->grid = $grid;
51
        $this->head = $head;
52
        $this->tail = $tail;
53
54
        $this->left = Collection::make();
55
        $this->right = Collection::make();
56
    }
57
58
    /**
59
     * @return Collection
60
     */
61
    public function leftColumns()
62
    {
63
        return $this->left;
64
    }
65
66
    /**
67
     * @return Collection
68
     */
69
    public function rightColumns()
70
    {
71
        return $this->right;
72
    }
73
74
    /**
75
     * @return \Closure
76
     */
77
    public function apply()
78
    {
79
        $this->grid->setView($this->view, [
80
            'allName' => $this->grid->getSelectAllName(),
81
            'rowName' => $this->grid->getGridRowName(),
82
        ]);
83
84
        return function (Grid $grid) {
85
            if ($this->head > 0) {
86
                $this->left = $grid->visibleColumns()->slice(0, $this->head);
0 ignored issues
show
Bug introduced by
The method slice does only exist in Illuminate\Support\Collection, but not in Encore\Admin\Grid\Concerns\CanHidesColumns.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
87
            }
88
89
            if ($this->tail < 0) {
90
                $this->right = $grid->visibleColumns()->slice($this->tail);
91
            }
92
        };
93
    }
94
}
95