Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Failed
Pull Request — master (#3445)
by Cristian
12:56
created

ColumnsProtectedMethods::makeSureColumnHasType()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 8
c 2
b 0
f 0
nc 32
nop 1
dl 0
loc 17
rs 8.8333
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
trait ColumnsProtectedMethods
9
{
10
    /**
11
     * Add a column to the current operation, using the Setting API.
12
     *
13
     * @param array $column Column definition array.
14
     */
15
    protected function addColumnToOperationSettings($column)
16
    {
17
        $allColumns = $this->columns();
0 ignored issues
show
Bug introduced by
It seems like columns() 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

17
        /** @scrutinizer ignore-call */ 
18
        $allColumns = $this->columns();
Loading history...
18
        $allColumns = Arr::add($allColumns, $column['key'], $column);
19
20
        $this->setOperationSetting('columns', $allColumns);
0 ignored issues
show
Bug introduced by
It seems like setOperationSetting() 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

20
        $this->/** @scrutinizer ignore-call */ 
21
               setOperationSetting('columns', $allColumns);
Loading history...
21
    }
22
23
    /**
24
     * If a column priority has not been defined, provide a default one.
25
     *
26
     * @param array $column Column definition array.
27
     * @return array         Proper array defining the column.
28
     */
29
    protected function makeSureColumnHasPriority($column)
30
    {
31
        $columns_count = $this->countColumnsWithoutActions();
0 ignored issues
show
Bug introduced by
It seems like countColumnsWithoutActions() 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

31
        /** @scrutinizer ignore-call */ 
32
        $columns_count = $this->countColumnsWithoutActions();
Loading history...
32
        $assumed_priority = $columns_count ? $columns_count : 0;
33
34
        $column['priority'] = $column['priority'] ?? $assumed_priority;
35
36
        return $column;
37
    }
38
39
    /**
40
     * If the field definition array is actually a string, it means the programmer was lazy
41
     * and has only passed the name of the column. Turn that into a proper array.
42
     *
43
     * @param array $column Column definition array.
44
     * @return array         Proper array defining the column.
45
     */
46
    protected function makeSureColumnHasName($column)
47
    {
48
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always false.
Loading history...
49
            $column = ['name' => $column];
50
        }
51
52
        if (is_array($column) && ! isset($column['name'])) {
53
            $column['name'] = 'anonymous_column_'.Str::random(5);
54
        }
55
56
        return $column;
57
    }
58
59
    /**
60
     * If a column array is missing the "label" attribute, an ugly error would be show.
61
     * So we add the field Name as a label - it's better than nothing.
62
     *
63
     * @param array     $column  Column definition array.
64
     * @return array            Proper array defining the column.
65
     */
66
    protected function makeSureColumnHasLabel($column)
67
    {
68
        if (! isset($column['label'])) {
69
            $column['label'] = mb_ucfirst($this->makeLabel($column['name']));
0 ignored issues
show
Bug introduced by
It seems like makeLabel() 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

69
            $column['label'] = mb_ucfirst($this->/** @scrutinizer ignore-call */ makeLabel($column['name']));
Loading history...
70
        }
71
72
        return $column;
73
    }
74
75
    /**
76
     * If a column definition is missing the type, set a default.
77
     *
78
     * @param array $column Column definition array.
79
     * @return array        Column definition array with type.
80
     */
81
    protected function makeSureColumnHasType($column)
82
    {
83
        // check if method exists in model so it's a possible relation
84
        // but exclude possible matches if developer setup entity => false
85
        $could_be_relation = method_exists($this->model, $column['name'])
86
            ? ! isset($column['entity']) || $column['entity'] !== false
87
            : isset($column['entity']) && $column['entity'] !== false;
88
89
        if (! isset($column['type']) && $could_be_relation) {
90
            $column['type'] = 'relationship';
91
        }
92
93
        if (! isset($column['type'])) {
94
            $column['type'] = 'text';
95
        }
96
97
        return $column;
98
    }
99
100
    /**
101
     * If a column definition is missing the key, set the default.
102
     * The key is used when storing all columns using the Settings API,
103
     * it is used as the "key" of the associative array that holds all columns.
104
     *
105
     * @param array $column Column definition array.
106
     * @return array        Column definition array with key.
107
     */
108
    protected function makeSureColumnHasKey($column)
109
    {
110
        if (! isset($column['key'])) {
111
            $column['key'] = str_replace('.', '__', $column['name']);
112
        }
113
114
        return $column;
115
    }
116
117
    /**
118
     * If a column definition is missing the wrapper element, set the default (empty).
119
     * The wrapper is the HTML element that wrappes around the column text.
120
     * By defining this array a developer can wrap the text into an anchor (link),
121
     * span, div or whatever they want.
122
     *
123
     * @param array $column Column definition array.
124
     * @return array        Column definition array with wrapper.
125
     */
126
    protected function makeSureColumnHasWrapper($column)
127
    {
128
        if (! isset($column['wrapper'])) {
129
            $column['wrapper'] = [];
130
        }
131
132
        return $column;
133
    }
134
135
    /**
136
     * If an entity has been defined for the column, but no model,
137
     * determine the model from that relationship.
138
     *
139
     * @param array $column Column definition array.
140
     * @return array        Column definition array with model.
141
     */
142
    protected function makeSureColumnHasModel($column)
143
    {
144
        // if this is a relation type field and no corresponding model was specified,
145
        // get it from the relation method defined in the main model
146
        if (isset($column['entity']) && ! isset($column['model'])) {
147
            $column['model'] = $this->getRelationModel($column['entity']);
0 ignored issues
show
Bug introduced by
It seems like getRelationModel() 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

147
            /** @scrutinizer ignore-call */ 
148
            $column['model'] = $this->getRelationModel($column['entity']);
Loading history...
148
        }
149
150
        return $column;
151
    }
152
153
    /**
154
     * Move the most recently added column before or after the given target column. Default is before.
155
     *
156
     * @param string|array $targetColumn The target column name or array.
157
     * @param bool         $before       If true, the column will be moved before the target column, otherwise it will be moved after it.
158
     */
159
    protected function moveColumn($targetColumn, $before = true)
160
    {
161
        // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved
162
        //       into a common class
163
        $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn;
164
        $columnsArray = $this->columns();
165
166
        if (array_key_exists($targetColumnName, $columnsArray)) {
167
            $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) :
168
                array_search($targetColumnName, array_keys($columnsArray)) + 1;
169
170
            $element = array_pop($columnsArray);
171
            $beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true);
0 ignored issues
show
Bug introduced by
It seems like $targetColumnPosition can also be of type false and string; however, parameter $length of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

171
            $beginningPart = array_slice($columnsArray, 0, /** @scrutinizer ignore-type */ $targetColumnPosition, true);
Loading history...
172
            $endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true);
0 ignored issues
show
Bug introduced by
It seems like $targetColumnPosition can also be of type false and string; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

172
            $endingArrayPart = array_slice($columnsArray, /** @scrutinizer ignore-type */ $targetColumnPosition, null, true);
Loading history...
173
174
            $columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart);
175
            $this->setOperationSetting('columns', $columnsArray);
176
        }
177
    }
178
179
    /**
180
     * Check if the column exists in the database, as a DB column.
181
     *
182
     * @param string $table
183
     * @param string $name
184
     *
185
     * @return bool
186
     */
187
    protected function hasDatabaseColumn($table, $name)
188
    {
189
        static $cache = [];
190
191
        if (! $this->driverIsSql()) {
0 ignored issues
show
Bug introduced by
It seems like driverIsSql() 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

191
        if (! $this->/** @scrutinizer ignore-call */ driverIsSql()) {
Loading history...
192
            return true;
193
        }
194
195
        if (isset($cache[$table])) {
196
            $columns = $cache[$table];
197
        } else {
198
            $columns = $cache[$table] = $this->getSchema()->getColumnListing($table);
0 ignored issues
show
Bug introduced by
It seems like getSchema() 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

198
            $columns = $cache[$table] = $this->/** @scrutinizer ignore-call */ getSchema()->getColumnListing($table);
Loading history...
199
        }
200
201
        return in_array($name, $columns);
202
    }
203
}
204