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

Completed
Push — v4dot1 ( 1d33b7...cabaa1 )
by Cristian
06:48 queued 21s
created

makeSureColumnHasWrapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
     * The only REALLY MANDATORY attribute for a column is the 'name'.
12
     * Everything else, Backpack can probably guess.
13
     *
14
     * This method checks that all necessary attributes are set.
15
     * If not, it tries to guess them.
16
     *
17
     * @param  string|array $column The column definition array OR column name as string.
18
     * @return array                Proper column definition array.
19
     */
20
    protected function makeSureColumnHasNeededAttributes($column)
21
    {
22
        $column = $this->makeSureColumnHasName($column);
0 ignored issues
show
Bug introduced by
It seems like $column can also be of type string; however, parameter $column of Backpack\CRUD\app\Librar...makeSureColumnHasName() does only seem to accept array, 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

22
        $column = $this->makeSureColumnHasName(/** @scrutinizer ignore-type */ $column);
Loading history...
23
        $column = $this->makeSureColumnHasLabel($column);
24
        $column = $this->makeSureColumnHasType($column);
25
        $column = $this->makeSureColumnHasKey($column);
26
        $column = $this->makeSureColumnHasModel($column);
27
28
        // check if the column exists in the database (as a db column)
29
        $columnExistsInDb = $this->hasColumn($this->model->getTable(), $column['name']);
30
31
        // make sure column has tableColumn, orderable and searchLogic
32
        $column['tableColumn'] = $column['tableColumn'] ?? $columnExistsInDb;
33
        $column['orderable'] = $column['orderable'] ?? $columnExistsInDb;
34
        $column['searchLogic'] = $column['searchLogic'] ?? $columnExistsInDb;
35
36
        return $column;
37
    }
38
39
    /**
40
     * Add a column to the current operation, using the Setting API.
41
     *
42
     * @param array $column Column definition array.
43
     */
44
    protected function addColumnToOperationSettings($column)
45
    {
46
        $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

46
        /** @scrutinizer ignore-call */ 
47
        $allColumns = $this->columns();
Loading history...
47
        $allColumns = Arr::add($allColumns, $column['key'], $column);
48
49
        $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

49
        $this->/** @scrutinizer ignore-call */ 
50
               setOperationSetting('columns', $allColumns);
Loading history...
50
51
        // make sure the column has a priority in terms of visibility
52
        // if no priority has been defined, use the order in the array plus one
53
        if (! array_key_exists('priority', $column)) {
54
            $position_in_columns_array = (int) array_search($column['key'], array_keys($this->columns()));
55
            $allColumns[$column['key']]['priority'] = $position_in_columns_array + 1;
56
        }
57
58
        $this->setOperationSetting('columns', $allColumns);
59
    }
60
61
    /**
62
     * If the field definition array is actually a string, it means the programmer was lazy
63
     * and has only passed the name of the column. Turn that into a proper array.
64
     *
65
     * @param array $column Column definition array.
66
     * @return array         Proper array defining the column.
67
     */
68
    protected function makeSureColumnHasName($column)
69
    {
70
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always false.
Loading history...
71
            $column = ['name' => $column];
72
        }
73
74
        if (is_array($column) && ! isset($column['name'])) {
75
            $column['name'] = 'anonymous_column_'.Str::random(5);
76
        }
77
78
        return $column;
79
    }
80
81
    /**
82
     * If a column array is missing the "label" attribute, an ugly error would be show.
83
     * So we add the field Name as a label - it's better than nothing.
84
     *
85
     * @param array     $column  Column definition array.
86
     * @return array            Proper array defining the column.
87
     */
88
    protected function makeSureColumnHasLabel($column)
89
    {
90
        if (! isset($column['label'])) {
91
            $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

91
            $column['label'] = mb_ucfirst($this->/** @scrutinizer ignore-call */ makeLabel($column['name']));
Loading history...
92
        }
93
94
        return $column;
95
    }
96
97
    /**
98
     * If a column definition is missing the type, set a default.
99
     *
100
     * @param array $column Column definition array.
101
     * @return array        Column definition array with type.
102
     */
103
    protected function makeSureColumnHasType($column)
104
    {
105
        if (! isset($column['type'])) {
106
            $column['type'] = 'text';
107
        }
108
109
        return $column;
110
    }
111
112
    /**
113
     * If a column definition is missing the key, set the default.
114
     * The key is used when storing all columns using the Settings API,
115
     * it is used as the "key" of the associative array that holds all columns.
116
     *
117
     * @param array $column Column definition array.
118
     * @return array        Column definition array with key.
119
     */
120
    protected function makeSureColumnHasKey($column)
121
    {
122
        if (! isset($column['key'])) {
123
            $column['key'] = str_replace('.', '__', $column['name']);
124
        }
125
126
        return $column;
127
    }
128
129
    /**
130
     * If a column definition is missing the wrapper element, set the default (empty).
131
     * The wrapper is the HTML element that wrappes around the column text.
132
     * By defining this array a developer can wrap the text into an anchor (link),
133
     * span, div or whatever they want.
134
     *
135
     * @param array $column Column definition array.
136
     * @return array        Column definition array with wrapper.
137
     */
138
    protected function makeSureColumnHasWrapper($column)
139
    {
140
        if (! isset($column['wrapper'])) {
141
            $column['wrapper'] = [];
142
        }
143
144
        return $column;
145
    }
146
147
    /**
148
     * If an entity has been defined for the column, but no model,
149
     * determine the model from that relationship.
150
     *
151
     * @param array $column Column definition array.
152
     * @return array        Column definition array with model.
153
     */
154
    protected function makeSureColumnHasModel($column)
155
    {
156
        // if this is a relation type field and no corresponding model was specified,
157
        // get it from the relation method defined in the main model
158
        if (isset($column['entity']) && ! isset($column['model'])) {
159
            $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

159
            /** @scrutinizer ignore-call */ 
160
            $column['model'] = $this->getRelationModel($column['entity']);
Loading history...
160
        }
161
162
        return $column;
163
    }
164
165
    /**
166
     * Move the most recently added column before or after the given target column. Default is before.
167
     *
168
     * @param string|array $targetColumn The target column name or array.
169
     * @param bool         $before       If true, the column will be moved before the target column, otherwise it will be moved after it.
170
     */
171
    protected function moveColumn($targetColumn, $before = true)
172
    {
173
        // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved
174
        //       into a common class
175
        $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn;
176
        $columnsArray = $this->columns();
177
178
        if (array_key_exists($targetColumnName, $columnsArray)) {
179
            $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) :
180
                array_search($targetColumnName, array_keys($columnsArray)) + 1;
181
182
            $element = array_pop($columnsArray);
183
            $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

183
            $beginningPart = array_slice($columnsArray, 0, /** @scrutinizer ignore-type */ $targetColumnPosition, true);
Loading history...
184
            $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

184
            $endingArrayPart = array_slice($columnsArray, /** @scrutinizer ignore-type */ $targetColumnPosition, null, true);
Loading history...
185
186
            $columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart);
187
            $this->setOperationSetting('columns', $columnsArray);
188
        }
189
    }
190
191
    /**
192
     * Check if the column exists in the database, as a DB column.
193
     *
194
     * @param string $table
195
     * @param string $name
196
     *
197
     * @return bool
198
     */
199
    protected function hasColumn($table, $name)
200
    {
201
        static $cache = [];
202
203
        if ($this->driverIsMongoDb()) {
0 ignored issues
show
Bug introduced by
It seems like driverIsMongoDb() 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

203
        if ($this->/** @scrutinizer ignore-call */ driverIsMongoDb()) {
Loading history...
204
            return true;
205
        }
206
207
        if (isset($cache[$table])) {
208
            $columns = $cache[$table];
209
        } else {
210
            $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

210
            $columns = $cache[$table] = $this->/** @scrutinizer ignore-call */ getSchema()->getColumnListing($table);
Loading history...
211
        }
212
213
        return in_array($name, $columns);
214
    }
215
}
216