Completed
Push — master ( 8bf0fe...b37564 )
by Song
05:18 queued 03:01
created

TotalRow::getVisibleColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Grid\Column;
6
use Illuminate\Database\Query\Builder;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
10
class TotalRow extends AbstractTool
11
{
12
    /**
13
     * @var Builder
14
     */
15
    protected $query;
16
17
    /**
18
     * @var array
19
     */
20
    protected $columns;
21
22
    /**
23
     * @var Collection
24
     */
25
    protected $visibleColumns;
26
27
    /**
28
     * TotalRow constructor.
29
     *
30
     * @param Builder $query
31
     * @param array   $columns
32
     */
33
    public function __construct($query, array $columns)
34
    {
35
        $this->query = $query;
36
37
        $this->columns = $columns;
38
    }
39
40
    /**
41
     * Get total value of current column.
42
     *
43
     * @param string $column
44
     * @param mixed  $display
45
     *
46
     * @return mixed
47
     */
48
    protected function total($column, $display = null)
49
    {
50
        if (!is_callable($display) && !is_null($display)) {
51
            return $display;
52
        }
53
54
        $sum = $this->query->sum($column);
55
56
        if (is_callable($display)) {
57
            return call_user_func($display, $sum);
58
        }
59
60
        return $sum;
61
    }
62
63
    /**
64
     * @param Collection $columns
65
     */
66
    public function setVisibleColumns($columns)
67
    {
68
        $this->visibleColumns = $columns;
69
    }
70
71
    /**
72
     * @return Collection|static
73
     */
74
    public function getVisibleColumns()
75
    {
76
        if ($this->visibleColumns) {
77
            return $this->visibleColumns;
78
        }
79
80
        return $this->getGrid()->visibleColumns();
81
    }
82
83
    /**
84
     * Render total-row.
85
     *
86
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
87
     */
88
    public function render()
89
    {
90
        $columns = $this->getVisibleColumns()->map(function (Column $column) {
0 ignored issues
show
Bug introduced by
The method map 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...
91
            $name = $column->getName();
92
93
            $total = '';
94
95
            if (Arr::has($this->columns, $name)) {
96
                $total = $this->total($name, Arr::get($this->columns, $name));
97
            }
98
99
            return [
100
                'class' => $column->getClassName(),
101
                'value' => $total
102
            ];
103
        });
104
105
        return view('admin::grid.total-row', compact('columns'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin::grid.total-...', compact('columns')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 105 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
106
    }
107
}
108