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 ( 13df59...0ad3ab )
by Cristian
02:32
created

Columns   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 236
Duplicated Lines 6.78 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 16
loc 236
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
B setColumns() 0 32 6
A addColumn() 0 14 2
A addColumns() 0 8 3
A beforeColumn() 8 8 3
A afterColumn() 8 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

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        array_filter($this->columns[] = $column_with_details);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Add multiple columns at the end of the CRUD object's "columns" array.
71
     *
72
     * @param [array of columns]
73
     */
74
    public function addColumns($columns)
75
    {
76
        if (count($columns)) {
77
            foreach ($columns as $key => $column) {
78
                $this->addColumn($column);
79
            }
80
        }
81
    }
82
83
    /**
84
     * Moves the recently added column to 'before' the $target_col
85
     *
86
     * @param $target_col
87
     */
88 View Code Duplication
    public function beforeColumn($target_col) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The parameter $target_col is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
89
        foreach ($this->columns as $column => $value) {
90
            if ($value['name'] == $target_col) {
91
                array_splice($this->columns, $column, 0, array(array_pop($this->columns)));
92
                break;
93
            }
94
        }
95
    }
96
97
    /**
98
     * Moves the recently added column to 'after' the $target_col
99
     *
100
     * @param $target
101
     */
102 View Code Duplication
    public function afterColumn($target_col) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The parameter $target_col is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
103
        foreach ($this->columns as $column => $value) {
104
            if ($value['name'] == $target_col) {
105
                array_splice($this->columns, $column + 1, 0, array(array_pop($this->columns)));
106
                break;
107
            }
108
        }
109
    }
110
111
    /**
112
     * Add the default column type to the given Column, inferring the type from the database column type.
113
     *
114
     * @param [column array]
115
     */
116
    public function addDefaultTypeToColumn($column)
117
    {
118
        if (array_key_exists('name', (array) $column)) {
119
            $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...
120
121
            return array_merge(['type' => $default_type], $column);
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * If a field or column array is missing the "label" attribute, an ugly error would be show.
129
     * So we add the field Name as a label - it's better than nothing.
130
     *
131
     * @param [field or column]
132
     */
133
    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...
134
    {
135
        if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) {
136
            $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...
137
138
            return $array;
139
        }
140
141
        return $array;
142
    }
143
144
    /**
145
     * Remove multiple columns from the CRUD object using their names.
146
     *
147
     * @param  [column array]
148
     */
149
    public function removeColumns($columns)
150
    {
151
        $this->columns = $this->remove('columns', $columns);
152
    }
153
154
    /**
155
     * Remove a column from the CRUD object using its name.
156
     *
157
     * @param  [column array]
158
     */
159
    public function removeColumn($column)
160
    {
161
        return $this->removeColumns([$column]);
162
    }
163
164
    /**
165
     * @param string $entity
166
     */
167
    public function remove($entity, $fields)
168
    {
169
        return array_values(array_filter($this->{$entity}, function ($field) use ($fields) {
170
            return ! in_array($field['name'], (array) $fields);
171
        }));
172
    }
173
174
    /**
175
     * Change attributes for multiple columns.
176
     *
177
     * @param [columns arrays]
178
     * @param [attributes and values array]
179
     */
180
    public function setColumnsDetails($columns, $attributes)
181
    {
182
        $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...
183
    }
184
185
    /**
186
     * Change attributes for a certain column.
187
     *
188
     * @param [string] Column name.
189
     * @param [attributes and values array]
190
     */
191
    public function setColumnDetails($column, $attributes)
192
    {
193
        $this->setColumnsDetails([$column], $attributes);
194
    }
195
196
    /**
197
     * Order the columns in a certain way.
198
     *
199
     * @param [string] Column name.
200
     * @param [attributes and values array]
201
     */
202
    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...
203
    {
204
        // TODO
205
    }
206
207
    // ALIAS of setColumnOrder($columns)
208
    public function setColumnsOrder($columns)
209
    {
210
        $this->setColumnOrder($columns);
211
    }
212
213
    /**
214
     * Get the relationships used in the CRUD columns.
215
     * @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...
216
     */
217
    public function getColumnsRelationships()
218
    {
219
        $columns = $this->getColumns();
220
221
        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...
222
            return $value == null;
223
        })->toArray();
224
    }
225
226
    // ------------
227
    // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED
228
    // ------------
229
    // TODO: check them
230
231
    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...
232
    {
233
        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...
234
    }
235
236
    public function orderColumns($order)
237
    {
238
        $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...
239
    }
240
}
241