HasColumns::collectColumns()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 9
eloc 24
c 5
b 1
f 0
nc 7
nop 1
dl 0
loc 43
ccs 0
cts 20
cp 0
crap 90
rs 8.0555
1
<?php
2
3
namespace Terranet\Administrator\Traits\Module;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Terranet\Administrator\Collection\Group;
7
use Terranet\Administrator\Collection\Mutable as MutableCollection;
8
use Terranet\Administrator\Dashboard\Manager;
9
use Terranet\Administrator\Decorators\Grid as GridDecorator;
10
use Terranet\Translatable\Translatable;
11
12
trait HasColumns
13
{
14
    /**
15
     * Fetch scaffold columns.
16
     *
17
     * @return MutableCollection
18
     */
19
    public function columns(): MutableCollection
20
    {
21
        return $this->scaffoldColumns();
22
    }
23
24
    /**
25
     * Define the list of attributes visible on View model page.
26
     *
27
     * @return MutableCollection
28
     */
29
    public function viewColumns(): MutableCollection
30
    {
31
        return $this->scaffoldColumns();
32
    }
33
34
    /**
35
     * List of widgets.
36
     *
37
     * @return Manager
38
     */
39
    public function widgets(): Manager
40
    {
41
        return new Manager();
42
    }
43
44
    /**
45
     * List of cards.
46
     *
47
     * @return Manager
48
     */
49
    public function cards(): Manager
50
    {
51
        return new Manager();
52
    }
53
54
    /**
55
     * Scaffold columns.
56
     *
57
     * @return MutableCollection
58
     */
59
    protected function scaffoldColumns(): MutableCollection
60
    {
61
        return $this->collectColumns(
62
            $this->model()
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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            $this->/** @scrutinizer ignore-call */ 
63
                   model()
Loading history...
63
        );
64
    }
65
66
    /**
67
     * @param $model
68
     *
69
     * @return MutableCollection
70
     */
71
    protected function collectColumns(Model $model = null): MutableCollection
72
    {
73
        if (!$model) {
74
            return new MutableCollection([]);
75
        }
76
77
        $pk = $model->getKeyName();
78
79
        $fillable = array_merge(
80
            is_array($pk) ? $pk : [$pk],
81
            $model->getFillable()
82
        );
83
        $hidden = $model->getHidden();
84
85
        if ($model instanceof Translatable && method_exists($model, 'getTranslatedAttributes')) {
86
            $fillable = array_merge($fillable, $model->getTranslatedAttributes());
87
            $hidden = array_merge($hidden, $model->getTranslationModel()->getHidden());
0 ignored issues
show
Bug introduced by
The method getTranslationModel() does not exist on Terranet\Translatable\Translatable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            $hidden = array_merge($hidden, $model->/** @scrutinizer ignore-call */ getTranslationModel()->getHidden());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        }
89
90
        $fillable = array_unique(array_diff($fillable, $hidden));
91
92
        $elements = new MutableCollection($fillable);
93
94
        if (property_exists($this, 'includeDateColumns')
95
            && $this->includeDateColumns
96
            && count($dates = $model->getDates())) {
97
            // allow setting specific timestamp: created_at
98
            if (is_string($this->includeDateColumns)) {
99
                $dates = array_intersect($dates, [$this->includeDateColumns]);
100
101
                $elements = $elements->merge($dates);
102
            } else {
103
                // add timestamps group
104
                $elements->group('dates', function (Group $group) use ($dates) {
105
                    $group->merge($dates);
106
107
                    return $group;
108
                });
109
            }
110
        }
111
112
        return $elements->build(
113
            new GridDecorator($model)
114
        );
115
    }
116
}
117