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 — master ( bab703...dad563 )
by Cristian
08:35
created

Columns::addColumn()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0909

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 8
nop 1
dl 0
loc 25
ccs 11
cts 13
cp 0.8462
crap 5.0909
rs 8.439
c 0
b 0
f 0
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 14
    public function addColumn($column)
55
    {
56
        // if a string was passed, not an array, change it to an array
57 14
        if (! is_array($column)) {
58 6
            $column = ['name' => $column];
59 6
        }
60
61
        // make sure the column has a label
62 14
        $column_with_details = $this->addDefaultLabel($column);
63
64
        // make sure the column has a name
65 14
        if (! array_key_exists('name', $column_with_details)) {
66
            $column_with_details['name'] = 'anonymous_column_'.str_random(5);
67
        }
68
69 14
        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 14
        if (isset($column_with_details['entity']) && ! isset($column_with_details['model'])) {
74 1
            $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 1
        }
76
77 14
        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 11
    public function addColumns($columns)
86
    {
87 11
        if (count($columns)) {
88 11
            foreach ($columns as $key => $column) {
89 10
                $this->addColumn($column);
90 10
            }
91 10
        }
92 10
    }
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 2
    public function afterColumn($targetColumn)
100
    {
101 2
        $this->moveColumn($targetColumn, false);
102 2
    }
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 2
    public function beforeColumn($targetColumn)
110
    {
111 2
        $this->moveColumn($targetColumn);
112 2
    }
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 4
    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 4
        $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn;
125
126 4
        if (array_key_exists($targetColumnName, $this->columns)) {
127 2
            $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($this->columns)) :
128 2
                array_search($targetColumnName, array_keys($this->columns)) + 1;
129
130 2
            $element = array_pop($this->columns);
131 2
            $beginningPart = array_slice($this->columns, 0, $targetColumnPosition, true);
132 2
            $endingArrayPart = array_slice($this->columns, $targetColumnPosition, null, true);
133
134 2
            $this->columns = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart);
135 2
        }
136 4
    }
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 14
    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 14
        if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) {
163 7
            $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 7
            return $array;
166
        }
167
168 7
        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 4
    public function removeColumn($column)
177
    {
178 4
        array_forget($this->columns, $column);
179 4
    }
180
181
    /**
182
     * Remove multiple columns from the CRUD panel by name.
183
     *
184
     * @param array $columns Array of column names.
185
     */
186 2
    public function removeColumns($columns)
187
    {
188 2
        if (! empty($columns)) {
189 2
            foreach ($columns as $columnName) {
190 2
                $this->removeColumn($columnName);
191 2
            }
192 2
        }
193 2
    }
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
     * Set label for a specific column.
238
     *
239
     * @param string $column
240
     * @param string $label
241
     */
242
    public function setColumnLabel($column, $label)
243
    {
244
        $this->setColumnDetails($column, ['label' => $label]);
245
    }
246
247
    /**
248
     * Order the columns in a certain way.
249
     *
250
     * @param [string] Column name.
251
     * @param [attributes and values array]
252
     */
253
    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...
254
    {
255
        // TODO
256
    }
257
258
    // ALIAS of setColumnOrder($columns)
259
    public function setColumnsOrder($columns)
260
    {
261
        $this->setColumnOrder($columns);
262
    }
263
264
    /**
265
     * Get the relationships used in the CRUD columns.
266
     * @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...
267
     */
268 3
    public function getColumnsRelationships()
269
    {
270 3
        $columns = $this->getColumns();
271
272 3
        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...
273 2
            return $value == null;
274 3
        })->toArray();
275
    }
276
277
    // ------------
278
    // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED
279
    // ------------
280
    // TODO: check them
281
282 3
    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...
283
    {
284 3
        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...
285
    }
286
287
    public function orderColumns($order)
288
    {
289
        $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...
290
    }
291
}
292