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 ( bb98e0...8a852f )
by Cristian
7s
created

CrudTrait::allTranslations()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.8571
cc 3
eloc 13
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
    |--------------------------------------------------------------------------
93
    | Translation Methods
94
    |--------------------------------------------------------------------------
95
    */
96
97
    public function translations()
98
    {
99
        $model = '\\'.get_class($this);
100
101
        if (isset($this->translatable))
102
        {
103
            return $model::where('translation_of', $this->id)->get();
0 ignored issues
show
Bug introduced by
The property id 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...
104
        }
105
106
        return collect();
107
    }
108
109
    // get translations plus current item, plus original
110
    public function allTranslations()
111
    {
112
        $model = '\\'.get_class($this);
113
114
        // the translations
115
        $translations = $this->translations();
116
117
        $all_translations = new Collection();
118
        // the original
119
        if ($this->translation_of) {
120
            $original = $model::find($this->translation_of);
0 ignored issues
show
Bug introduced by
The property translation_of 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...
121
            $all_translations = $all_translations->push($original);
122
123
            // get all translations from the original
124
            $translationsOfOriginal = $model::where('translation_of', $original->id)->get();
125
            foreach($translationsOfOriginal as $translation) {
126
                $all_translations->push($translation);
127
            }
128
        } else {
129
            // prepend the translation to be first in the list of translations
130
            $all_translations = $translations->prepend($this);
131
        }
132
133
        return $all_translations;
134
    }
135
136
    public function translation($translation_lang = false)
137
    {
138
        if ($translation_lang == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
139
            $translation_lang = Lang::locale();
140
        }
141
142
        $model = '\\'.get_class($this);
143
        if (isset($this->translatable))
144
        {
145
            return $model::where('translation_of', $this->id)->where('translation_lang', $translation_lang)->first();
146
        }
147
148
        return false;
149
    }
150
151
    public function translationLanguages()
152
    {
153
        $model = '\\'.get_class($this);
0 ignored issues
show
Unused Code introduced by
$model 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...
154
        $translations = $this->translations();
155
156
        $translated_in = [];
157
158
        if ($translations->count())
159
        {
160
            foreach ($translations as $key => $translation) {
161
                $translated_in[] = $translation->language;
162
            }
163
        }
164
165
        return collect($translated_in);
166
    }
167
168
    public function language()
169
    {
170
        return $this->belongsTo('\Dick\TranslationManager\Models\Language', 'translation_lang', 'abbr');
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
171
    }
172
173
    /**
174
     * Overwriting the Eloquent save() method, to set a default translation language, if necessary.
175
     */
176
    public function save(array $options = [])
177
    {
178
        if (isset($this->translatable))
179
        {
180
            // set a default language (the one the user is currently using)
181
            if (!(isset($this->translation_lang)) || $this->translation_lang == '')
182
            {
183
                $this->translation_lang = \Lang::locale();
0 ignored issues
show
Bug introduced by
The property translation_lang 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...
184
            }
185
186
            // TODO: if some untranslatable attributes are empty, but its parent's are filled, copy them
187
        }
188
189
        parent::save($options);
190
    }
191
192
}
193