Completed
Push — master ( be75e1...df533a )
by wen
13:25
created

Tree::setTitleAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\View;
4
5
use Illuminate\Support\Collection;
6
use InvalidArgumentException;
7
8
class Tree extends View
9
{
10
    protected $type = 'tree';
11
12
    protected $titleAttribute;
13
14
    public function get()
15
    {
16
        $builder = $this->getQuery();
17
        return $this->getTree($builder->get());
0 ignored issues
show
Bug introduced by
It seems like $builder->get() targeting Illuminate\Database\Eloquent\Builder::get() can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Sco\Admin\View\Tree::getTree() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
18
    }
19
20
    public function getTitleAttribute()
21
    {
22
        return $this->titleAttribute;
23
    }
24
25
    public function setTitleAttribute($value)
26
    {
27
        $this->titleAttribute = $value;
28
29
        return $this;
30
    }
31
32
    public function getTree(Collection $collection)
33
    {
34
        if (is_null($key = $this->getTitleAttribute())) {
35
            throw new InvalidArgumentException('Must set Tree title attribute');
36
        }
37
38
        return $collection->map(function ($row) use ($key) {
39
            if (!isset($row->$key)) {
40
                throw new \InvalidArgumentException("Not Found '{$key}' attribute");
41
            }
42
43
            return [
44
                'title' => $row->$key,
45
                'children' => [],
46
                '_primary' => $row->getKey(),
47
            ];
48
        });
49
    }
50
}
51