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 ( 00ccd2...5062f6 )
by Cristian
03:42
created

Columns   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 209
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
B setColumns() 0 32 6
A addColumn() 0 15 2
A addColumns() 0 8 3
A addDefaultTypeToColumn() 0 10 2
A addDefaultLabel() 0 10 3
A removeColumns() 0 4 1
A removeColumn() 0 4 1
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 type
62
        $column_with_details = $this->addDefaultTypeToColumn($column);
0 ignored issues
show
Unused Code introduced by
$column_with_details is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
64
        // make sure the column has a label
65
        $column_with_details = $this->addDefaultLabel($column);
66
67
        return array_filter($this->columns[] = $column_with_details);
68
    }
69
70
    /**
71
     * Add multiple columns at the end of the CRUD object's "columns" array.
72
     *
73
     * @param [array of columns]
74
     */
75
    public function addColumns($columns)
76
    {
77
        if (count($columns)) {
78
            foreach ($columns as $key => $column) {
79
                $this->addColumn($column);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Add the default column type to the given Column, inferring the type from the database column type.
86
     *
87
     * @param [column array]
88
     */
89
    public function addDefaultTypeToColumn($column)
90
    {
91
        if (array_key_exists('name', (array) $column)) {
92
            $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...
93
94
            return array_merge(['type' => $default_type], $column);
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * If a field or column array is missing the "label" attribute, an ugly error would be show.
102
     * So we add the field Name as a label - it's better than nothing.
103
     *
104
     * @param [field or column]
105
     */
106
    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...
107
    {
108
        if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) {
109
            $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...
110
111
            return $array;
112
        }
113
114
        return $array;
115
    }
116
117
    /**
118
     * Remove multiple columns from the CRUD object using their names.
119
     *
120
     * @param  [column array]
121
     */
122
    public function removeColumns($columns)
123
    {
124
        $this->columns = $this->remove('columns', $columns);
125
    }
126
127
    /**
128
     * Remove a column from the CRUD object using its name.
129
     *
130
     * @param  [column array]
131
     */
132
    public function removeColumn($column)
133
    {
134
        return $this->removeColumns([$column]);
135
    }
136
137
    /**
138
     * @param string $entity
139
     */
140
    public function remove($entity, $fields)
141
    {
142
        return array_values(array_filter($this->{$entity}, function ($field) use ($fields) {
143
            return ! in_array($field['name'], (array) $fields);
144
        }));
145
    }
146
147
    /**
148
     * Change attributes for multiple columns.
149
     *
150
     * @param [columns arrays]
151
     * @param [attributes and values array]
152
     */
153
    public function setColumnsDetails($columns, $attributes)
154
    {
155
        $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...
156
    }
157
158
    /**
159
     * Change attributes for a certain column.
160
     *
161
     * @param [string] Column name.
162
     * @param [attributes and values array]
163
     */
164
    public function setColumnDetails($column, $attributes)
165
    {
166
        $this->setColumnsDetails([$column], $attributes);
167
    }
168
169
    /**
170
     * Order the columns in a certain way.
171
     *
172
     * @param [string] Column name.
173
     * @param [attributes and values array]
174
     */
175
    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...
176
    {
177
        // TODO
178
    }
179
180
    // ALIAS of setColumnOrder($columns)
181
    public function setColumnsOrder($columns)
182
    {
183
        $this->setColumnOrder($columns);
184
    }
185
186
    /**
187
     * Get the relationships used in the CRUD columns.
188
     * @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...
189
     */
190
    public function getColumnsRelationships()
191
    {
192
        $columns = $this->getColumns();
193
194
        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...
195
            return $value == null;
196
        })->toArray();
197
    }
198
199
    // ------------
200
    // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED
201
    // ------------
202
    // TODO: check them
203
204
    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...
205
    {
206
        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...
207
    }
208
209
    public function orderColumns($order)
210
    {
211
        $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...
212
    }
213
}
214