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
Pull Request — master (#966)
by Cristian
03:02 queued 25s
created

Columns   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 276
rs 8.6
c 2
b 2
f 0
wmc 37
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
B setColumns() 0 32 6
B addColumn() 0 25 5
A addColumns() 0 8 3
A afterColumn() 0 4 1
A beforeColumn() 0 4 1
A moveColumn() 0 17 4
A addDefaultTypeToColumn() 0 10 2
A addDefaultLabel() 0 10 3
A removeColumn() 0 4 1
A removeColumns() 0 8 3
A remove() 0 6 1
A setColumnsDetails() 0 4 1
A setColumnDetails() 0 4 1
A setColumnOrder() 0 4 1
A setColumnsOrder() 0 4 1
A getColumnsRelationships() 0 8 1
A getColumns() 0 4 1
A orderColumns() 0 4 1
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait Columns
6
{
7
    // ------------
8
    // COLUMNS
9
    // ------------
10
11
    /**
12
     * Add a bunch of column names and their details to the CRUD object.
13
     *
14
     * @param [array or multi-dimensional array]
15
     */
16
    public function setColumns($columns)
17
    {
18
        // clear any columns already set
19
        $this->columns = [];
0 ignored issues
show
Bug introduced by
The property columns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
21
        // if array, add a column for each of the items
22
        if (is_array($columns) && count($columns)) {
23
            foreach ($columns as $key => $column) {
24
                // if label and other details have been defined in the array
25
                if (is_array($column)) {
26
                    $this->addColumn($column);
27
                } else {
28
                    $this->addColumn([
29
                                    'name'  => $column,
30
                                    'label' => ucfirst($column),
31
                                    'type'  => 'text',
32
                                ]);
33
                }
34
            }
35
        }
36
37
        if (is_string($columns)) {
38
            $this->addColumn([
39
                                'name'  => $columns,
40
                                'label' => ucfirst($columns),
41
                                'type'  => 'text',
42
                                ]);
43
        }
44
45
        // This was the old setColumns() function, and it did not work:
46
        // $this->columns = array_filter(array_map([$this, 'addDefaultTypeToColumn'], $columns));
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
    }
48
49
    /**
50
     * Add a column at the end of to the CRUD object's "columns" array.
51
     *
52
     * @param [string or array]
53
     */
54
    public function addColumn($column)
55
    {
56
        // if a string was passed, not an array, change it to an array
57
        if (! is_array($column)) {
58
            $column = ['name' => $column];
59
        }
60
61
        // make sure the column has a label
62
        $column_with_details = $this->addDefaultLabel($column);
63
64
        // make sure the column has a name
65
        if (! array_key_exists('name', $column_with_details)) {
66
            $column_with_details['name'] = 'anonymous_column_'.str_random(5);
67
        }
68
69
        array_filter($this->columns[$column_with_details['name']] = $column_with_details);
70
71
        // if this is a relation type field and no corresponding model was specified, get it from the relation method
72
        // defined in the main model
73
        if (isset($column_with_details['entity']) && ! isset($column_with_details['model'])) {
74
            $column_with_details['model'] = $this->getRelationModel($column_with_details['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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * Add multiple columns at the end of the CRUD object's "columns" array.
82
     *
83
     * @param [array of columns]
84
     */
85
    public function addColumns($columns)
86
    {
87
        if (count($columns)) {
88
            foreach ($columns as $key => $column) {
89
                $this->addColumn($column);
90
            }
91
        }
92
    }
93
94
    /**
95
     * Move the most recently added column after the given target column.
96
     *
97
     * @param string|array $targetColumn The target column name or array.
98
     */
99
    public function afterColumn($targetColumn)
100
    {
101
        $this->moveColumn($targetColumn, false);
102
    }
103
104
    /**
105
     * Move the most recently added column before the given target column.
106
     *
107
     * @param string|array $targetColumn The target column name or array.
108
     */
109
    public function beforeColumn($targetColumn)
110
    {
111
        $this->moveColumn($targetColumn);
112
    }
113
114
    /**
115
     * Move the most recently added column before or after the given target column. Default is before.
116
     *
117
     * @param string|array $targetColumn The target column name or array.
118
     * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
119
     */
120
    private function moveColumn($targetColumn, $before = true)
121
    {
122
        // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved
123
        //       into a common class
124
        $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn;
125
126
        if (array_key_exists($targetColumnName, $this->columns)) {
127
            $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($this->columns)) :
128
                array_search($targetColumnName, array_keys($this->columns)) + 1;
129
130
            $element = array_pop($this->columns);
131
            $beginningPart = array_slice($this->columns, 0, $targetColumnPosition, true);
132
            $endingArrayPart = array_slice($this->columns, $targetColumnPosition, null, true);
133
134
            $this->columns = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart);
135
        }
136
    }
137
138
    /**
139
     * Add the default column type to the given Column, inferring the type from the database column type.
140
     *
141
     * @param [column array]
142
     */
143
    public function addDefaultTypeToColumn($column)
144
    {
145
        if (array_key_exists('name', (array) $column)) {
146
            $default_type = $this->getFieldTypeFromDbColumnType($column['name']);
0 ignored issues
show
Bug introduced by
It seems like getFieldTypeFromDbColumnType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
147
148
            return array_merge(['type' => $default_type], $column);
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * If a field or column array is missing the "label" attribute, an ugly error would be show.
156
     * So we add the field Name as a label - it's better than nothing.
157
     *
158
     * @param [field or column]
159
     */
160
    public function addDefaultLabel($array)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
161
    {
162
        if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) {
163
            $array = array_merge(['label' => ucfirst($this->makeLabel($array['name']))], $array);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
164
165
            return $array;
166
        }
167
168
        return $array;
169
    }
170
171
    /**
172
     * Remove a column from the CRUD panel by name.
173
     *
174
     * @param string $column The column name.
175
     */
176
    public function removeColumn($column)
177
    {
178
        array_forget($this->columns, $column);
179
    }
180
181
    /**
182
     * Remove multiple columns from the CRUD panel by name.
183
     *
184
     * @param array $columns Array of column names.
185
     */
186
    public function removeColumns($columns)
187
    {
188
        if (! empty($columns)) {
189
            foreach ($columns as $columnName) {
190
                $this->removeColumn($columnName);
191
            }
192
        }
193
    }
194
195
    /**
196
     * Remove an entry from an array.
197
     *
198
     * @param string $entity
199
     * @param array $fields
200
     * @return array values
201
     *
202
     * @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the
203
     *             target array keys.
204
     * @see Columns::removeColumn() to remove a column from the CRUD panel by name.
205
     * @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name.
206
     */
207
    public function remove($entity, $fields)
208
    {
209
        return array_values(array_filter($this->{$entity}, function ($field) use ($fields) {
210
            return ! in_array($field['name'], (array) $fields);
211
        }));
212
    }
213
214
    /**
215
     * Change attributes for multiple columns.
216
     *
217
     * @param [columns arrays]
218
     * @param [attributes and values array]
219
     */
220
    public function setColumnsDetails($columns, $attributes)
221
    {
222
        $this->sync('columns', $columns, $attributes);
0 ignored issues
show
Bug introduced by
It seems like sync() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
223
    }
224
225
    /**
226
     * Change attributes for a certain column.
227
     *
228
     * @param [string] Column name.
229
     * @param [attributes and values array]
230
     */
231
    public function setColumnDetails($column, $attributes)
232
    {
233
        $this->setColumnsDetails([$column], $attributes);
234
    }
235
236
    /**
237
     * Order the columns in a certain way.
238
     *
239
     * @param [string] Column name.
240
     * @param [attributes and values array]
241
     */
242
    public function setColumnOrder($columns)
0 ignored issues
show
Unused Code introduced by
The parameter $columns is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
243
    {
244
        // TODO
245
    }
246
247
    // ALIAS of setColumnOrder($columns)
248
    public function setColumnsOrder($columns)
249
    {
250
        $this->setColumnOrder($columns);
251
    }
252
253
    /**
254
     * Get the relationships used in the CRUD columns.
255
     * @return [array] Relationship names
0 ignored issues
show
Documentation introduced by
The doc-type [array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
256
     */
257
    public function getColumnsRelationships()
258
    {
259
        $columns = $this->getColumns();
260
261
        return collect($columns)->pluck('entity')->reject(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
262
            return $value == null;
263
        })->toArray();
264
    }
265
266
    // ------------
267
    // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED
268
    // ------------
269
    // TODO: check them
270
271
    public function getColumns()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
272
    {
273
        return $this->sort('columns');
0 ignored issues
show
Bug introduced by
It seems like sort() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
274
    }
275
276
    public function orderColumns($order)
277
    {
278
        $this->setSort('columns', (array) $order);
0 ignored issues
show
Bug introduced by
It seems like setSort() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
279
    }
280
}
281