Completed
Push — master ( b0d73a...d2854c )
by Song
02:52
created

Table::display()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 38
rs 8.6897
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Illuminate\Support\Arr;
6
7
class Table extends AbstractDisplayer
8
{
9
    public function display($titles = [])
10
    {
11
        if (empty($this->value)) {
12
            return '';
13
        }
14
15
        if (empty($titles)) {
16
            $titles = array_keys($this->value[0]);
17
        }
18
19
        if (Arr::isAssoc($titles)) {
20
            $columns = array_keys($titles);
21
        } else {
22
            $titles = array_combine($titles, $titles);
23
            $columns = $titles;
24
        }
25
26
        $data = array_map(function ($item) use ($columns) {
27
            $sorted = [];
28
29
            $arr = array_only($item, $columns);
30
31
            foreach ($columns as $column) {
32
                if (array_key_exists($column, $arr)) {
33
                    $sorted[$column] = $arr[$column];
34
                }
35
            }
36
37
            return $sorted;
38
        }, $this->value);
39
40
        $variables = [
41
            'titles' => $titles,
42
            'data'   => $data,
43
        ];
44
45
        return view('admin::grid.displayer.table', $variables)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
46
    }
47
}
48