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 ( c61b79...4016a1 )
by Cristian
09:20
created

CrudTrait::translations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php namespace Backpack\CRUD;
2
3
use Illuminate\Database\Eloquent\Collection;
4
use Illuminate\Database\Eloquent\Model;
5
use DB;
6
use Lang;
7
8
trait CrudTrait {
9
10
    /*
11
    |--------------------------------------------------------------------------
12
    | Methods for ENUM and SELECT crud fields.
13
    |--------------------------------------------------------------------------
14
    */
15
16
    public static function getPossibleEnumValues($field_name){
17
        $instance = new static; // create an instance of the model to be able to get the table name
18
        $type = DB::select( DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"') )[0]->Type;
0 ignored issues
show
Bug introduced by
It seems like getTable() 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...
19
        preg_match('/^enum\((.*)\)$/', $type, $matches);
20
        $enum = array();
21
        $exploded = explode(',', $matches[1]);
22
        foreach($exploded as $value){
23
            $v = trim( $value, "'" );
24
            $enum[] = $v;
25
        }
26
        return $enum;
27
    }
28
29
    public static function isColumnNullable($column_name) {
30
        $instance = new static; // create an instance of the model to be able to get the table name
31
        $answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0];
0 ignored issues
show
Bug introduced by
It seems like getTable() 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...
32
33
        return ($answer->IS_NULLABLE == 'YES' ? true : false);
34
    }
35
36
37
    /*
38
    |--------------------------------------------------------------------------
39
    | Methods for Fake Fields functionality (used in PageManager).
40
    |--------------------------------------------------------------------------
41
    */
42
43
    /**
44
     * Add fake fields as regular attributes, even though they are stored as JSON.
45
     *
46
     * @param  array  $columns - the database columns that contain the JSONs
47
     */
48
    public function addFakes($columns = ['extras']) {
49
        foreach ($columns as $key => $column) {
50
51
            $column_contents = $this->{$column};
52
53
            if (!is_object($this->{$column}))
54
            {
55
                $column_contents = json_decode($this->{$column});
56
            }
57
58
            if (count($column_contents))
59
            {
60
                foreach ($column_contents as $fake_field_name => $fake_field_value) {
61
                    $this->setAttribute($fake_field_name, $fake_field_value);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
62
                }
63
            }
64
        }
65
    }
66
67
    /**
68
     * Return the entity with fake fields as attributes.
69
     *
70
     * @param  array  $columns - the database columns that contain the JSONs
71
     * @return CrudTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type CrudTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
72
     */
73
    public function withFakes($columns = [])
74
    {
75
        $model = '\\'.get_class($this);
76
77
        if (!count($columns)) {
78
            if (property_exists($model, 'fakeColumns')) {
79
                $columns = $this->fakeColumns;
1 ignored issue
show
Bug introduced by
The property fakeColumns 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...
80
            } else
81
            {
82
                $columns = ['extras'];
83
            }
84
        }
85
86
        $this->addFakes($columns);
87
88
        return $this;
89
    }
90
91
}
92