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 ( fd8510...b7d577 )
by Cristian
03:04
created

HasTranslations::getCastedAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable;
4
5
use Spatie\Translatable\HasTranslations as OriginalHasTranslations;
6
7
trait HasTranslations
8
{
9
    use OriginalHasTranslations;
10
11
    /**
12
     * @var bool
13
     */
14
    public $locale = false;
15
16
17
    /**
18
     * Get the attributes that were casted in the model.
19
     * Used for translations because Spatie/Laravel-Translatable
20
     * overwrites the getCasts() method.
21
     *
22
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] 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...
23
     */
24
    public function getCastedAttributes() : array
25
    {
26
        return parent::getCasts();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getCasts() instead of getCastedAttributes()). Are you sure this is correct? If so, you might want to change this to $this->getCasts().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
27
    }
28
29
    /*
30
    |--------------------------------------------------------------------------
31
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
32
    |--------------------------------------------------------------------------
33
    */
34
35
    /**
36
     * Use the forced locale if present.
37
     *
38
     * @param string $key
39
     * @return mixed
40
     */
41
    public function getAttributeValue($key)
42
    {
43
        if (! $this->isTranslatableAttribute($key)) {
44
            return parent::getAttributeValue($key);
45
        }
46
47
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'));
48
49
        return is_array($translation) ? array_first($translation) : $translation;
50
    }
51
52
    /*
53
    |--------------------------------------------------------------------------
54
    |                            ELOQUENT OVERWRITES
55
    |--------------------------------------------------------------------------
56
    */
57
58
    /**
59
     * Create translated items as json.
60
     *
61
     * @param array $attributes
62
     * @return static
63
     */
64
    public static function create(array $attributes = [])
65
    {
66
        $locale = $attributes['locale'] ?? \App::getLocale();
67
        $attributes = array_except($attributes, ['locale']);
68
69
        $model = new static();
70
71
        // do the actual saving
72
        foreach ($attributes as $attribute => $value) {
73
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
74
                $model->setTranslation($attribute, $locale, $value);
75
            } else { // the attribute is NOT translatable
76
                $model->{$attribute} = $value;
77
            }
78
        }
79
        $model->save();
80
81
        return $model;
82
    }
83
84
    /**
85
     * Update translated items as json.
86
     *
87
     * @param array $attributes
88
     * @param array $options
89
     * @return bool
90
     */
91
    public function update(array $attributes = [], array $options = [])
92
    {
93
        if (! $this->exists) {
94
            return false;
95
        }
96
97
        $locale = $attributes['locale'] ?? \App::getLocale();
98
        $attributes = array_except($attributes, ['locale']);
99
100
        // do the actual saving
101
        foreach ($attributes as $attribute => $value) {
102
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
103
                $this->setTranslation($attribute, $locale, $value);
104
            } else { // the attribute is NOT translatable
105
                $this->{$attribute} = $value;
106
            }
107
        }
108
109
        return $this->save($options);
110
    }
111
112
    /*
113
    |--------------------------------------------------------------------------
114
    |                            CUSTOM METHODS
115
    |--------------------------------------------------------------------------
116
    */
117
118
    /**
119
     * Check if a model is translatable, by the adapter's standards.
120
     *
121
     * @return bool
122
     */
123
    public function translationEnabledForModel()
124
    {
125
        return property_exists($this, 'translatable');
126
    }
127
128
    /**
129
     * Get all locales the admin is allowed to use.
130
     *
131
     * @return array
132
     */
133
    public function getAvailableLocales()
134
    {
135
        return config('backpack.crud.locales');
136
    }
137
138
    /**
139
     * Set the locale property. Used in normalizeLocale() to force the translation
140
     * to a different language that the one set in app()->getLocale();.
141
     *
142
     * @param string
143
     */
144
    public function setLocale($locale)
145
    {
146
        $this->locale = $locale;
147
    }
148
149
    /**
150
     * Get the locale property. Used in SpatieTranslatableSluggableService
151
     * to save the slug for the appropriate language.
152
     *
153
     * @param string
154
     */
155
    public function getLocale()
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...
156
    {
157
        if ($this->locale) {
158
            return $this->locale;
159
        }
160
161
        return \Request::input('locale', \App::getLocale());
162
    }
163
164
    /**
165
     * Magic method to get the db entries already translated in the wanted locale.
166
     *
167
     * @param string $method
168
     * @param array $parameters
169
     * @return
170
     */
171
    public function __call($method, $parameters)
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...
172
    {
173
        switch ($method) {
174
            // translate all find methods
175
            case 'find':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
176
            case 'findOrFail':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
177
            case 'findMany':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
178
            case 'findBySlug':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
179
            case 'findBySlugOrFail':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
180
181
                $translation_locale = \Request::input('locale', \App::getLocale());
182
183
                if ($translation_locale) {
184
                    $item = parent::__call($method, $parameters);
185
186
                    if ($item) {
187
                        $item->setLocale($translation_locale);
188
                    }
189
190
                    return $item;
191
                }
192
193
                return parent::__call($method, $parameters);
194
                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...
195
196
            // do not translate any other methods
197
            default:
198
                return parent::__call($method, $parameters);
199
                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...
200
        }
201
    }
202
}
203