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 ( 4c0b8d...17a619 )
by Cristian
11:35
created

AutoSet::makeLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait AutoSet
6
{
7
    // ------------------------------------------------------
8
    // AUTO-SET-FIELDS-AND-COLUMNS FUNCTIONALITY
9
    // ------------------------------------------------------
10
11
    /**
12
     * For a simple CRUD Panel, there should be no need to add/define the fields.
13
     * The public columns in the database will be converted to be fields.
14
     */
15 2
    public function setFromDb()
16
    {
17 2
        $this->setDoctrineTypesMapping();
18 2
        $this->getDbColumnTypes();
19
20 2
        array_map(function ($field) {
21
            // $this->labels[$field] = $this->makeLabel($field);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
22
23
            $new_field = [
24 2
                'name'       => $field,
25 2
                'label'      => ucfirst($field),
26
                'value'      => null,
27 2
                'default'    => isset($this->db_column_types[$field]['default']) ? $this->db_column_types[$field]['default'] : null,
0 ignored issues
show
Bug introduced by
The property db_column_types 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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
28 2
                'type'       => $this->getFieldTypeFromDbColumnType($field),
29
                'values'     => [],
30
                'attributes' => [],
31
                'autoset'    => true,
32
            ];
33 2
            if (! isset($this->create_fields[$field])) {
34 2
                $this->create_fields[$field] = $new_field;
0 ignored issues
show
Bug introduced by
The property create_fields 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...
35
            }
36 2
            if (! isset($this->update_fields[$field])) {
37 2
                $this->update_fields[$field] = $new_field;
0 ignored issues
show
Bug introduced by
The property update_fields 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...
38
            }
39
40 2
            if (! in_array($field, $this->model->getHidden()) && ! isset($this->columns[$field])) {
0 ignored issues
show
Bug introduced by
The property model 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...
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...
41 2
                $this->addColumn([
0 ignored issues
show
Bug introduced by
It seems like addColumn() 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...
42 2
                    'name'  => $field,
43 2
                    'label' => ucfirst($field),
44 2
                    'type'  => $this->getFieldTypeFromDbColumnType($field),
45
                    'autoset' => true,
46
                ]);
47
            }
48 2
        }, $this->getDbColumnsNames());
49 2
    }
50
51
    /**
52
     * Get all columns from the database for that table.
53
     *
54
     * @return [array]
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...
55
     */
56 3
    public function getDbColumnTypes()
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...
57
    {
58 3
        $table = $this->model->getTable();
59 3
        $conn = $this->model->getConnection();
60 3
        $table_columns = $conn->getSchemaBuilder()->getColumnListing($table);
61
62 3
        foreach ($table_columns as $key => $column) {
63 3
            $column_type = $conn->getSchemaBuilder()->getColumnType($table, $column);
64 3
            $this->db_column_types[$column]['type'] = trim(preg_replace('/\(\d+\)(.*)/i', '', $column_type));
65 3
            $this->db_column_types[$column]['default'] = $conn->getDoctrineSchemaManager()->listTableDetails($table)->getColumn($column)->getDefault();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 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...
66
        }
67
68 3
        return $this->db_column_types;
69
    }
70
71
    /**
72
     * Intuit a field type, judging from the database column type.
73
     *
74
     * @param  [string] Field name.
75
     *
76
     * @return [string] Fielt type.
0 ignored issues
show
Documentation introduced by
The doc-type [string] 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...
77
     */
78 65
    public function getFieldTypeFromDbColumnType($field)
79
    {
80 65
        if (! array_key_exists($field, $this->db_column_types)) {
81 63
            return 'text';
82
        }
83
84 2
        if ($field == 'password') {
85
            return 'password';
86
        }
87
88 2
        if ($field == 'email') {
89
            return 'email';
90
        }
91
92 2
        switch ($this->db_column_types[$field]['type']) {
93
            case 'int':
94
            case 'smallint':
95
            case 'mediumint':
96
            case 'longint':
97
                return 'number';
98
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
99
100
            case 'string':
101
            case 'varchar':
102
            case 'set':
103 2
                return 'text';
104
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
105
106
            // case 'enum':
107
            //     return 'enum';
108
            // break;
109
110
            case 'tinyint':
111
                return 'active';
112
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
113
114
            case 'text':
115 2
                return 'textarea';
116
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
117
118
            case 'mediumtext':
119
            case 'longtext':
120
                return 'textarea';
121
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
122
123
            case 'date':
124 2
                return 'date';
125
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
126
127
            case 'datetime':
128
            case 'timestamp':
129 2
                return 'datetime';
130
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
131
            case 'time':
132 2
                return 'time';
133
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
134
135
            default:
136 2
                return 'text';
137
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
138
        }
139
    }
140
141
    // Fix for DBAL not supporting enum
142 2
    public function setDoctrineTypesMapping()
143
    {
144 2
        $types = ['enum' => 'string'];
145 2
        $platform = \DB::getDoctrineConnection()->getDatabasePlatform();
146 2
        foreach ($types as $type_key => $type_value) {
147 2
            if (! $platform->hasDoctrineTypeMappingFor($type_key)) {
148 2
                $platform->registerDoctrineTypeMapping($type_key, $type_value);
149
            }
150
        }
151 2
    }
152
153
    /**
154
     * Turn a database column name or PHP variable into a pretty label to be shown to the user.
155
     *
156
     * @param  [string]
157
     *
158
     * @return [string]
0 ignored issues
show
Documentation introduced by
The doc-type [string] 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...
159
     */
160 8
    public function makeLabel($value)
161
    {
162 8
        return trim(preg_replace('/(id|at|\[\])$/i', '', ucfirst(str_replace('_', ' ', $value))));
163
    }
164
165
    /**
166
     * Get the database column names, in order to figure out what fields/columns to show in the auto-fields-and-columns functionality.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
167
     *
168
     * @return [array] Database column names as an array.
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...
169
     */
170 3
    public function getDbColumnsNames()
171
    {
172
        // Automatically-set columns should be both in the database, and in the $fillable variable on the Eloquent Model
173 3
        $columns = $this->model->getConnection()->getSchemaBuilder()->getColumnListing($this->model->getTable());
174 3
        $fillable = $this->model->getFillable();
175
176 3
        if (! empty($fillable)) {
177
            $columns = array_intersect($columns, $fillable);
178
        }
179
180
        // but not updated_at, deleted_at
181 3
        return array_values(array_diff($columns, [$this->model->getKeyName(), $this->model->getCreatedAtColumn(), $this->model->getUpdatedAtColumn(), 'deleted_at']));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 166 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...
182
    }
183
}
184