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
Pull Request — master (#1330)
by Oliver
06:56
created

HasTranslations::getAttributeValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 15
rs 9.2
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
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
19
    |--------------------------------------------------------------------------
20
    */
21
22
    /**
23
     * Use the forced locale if present.
24
     *
25
     * @param string $key
26
     * @return mixed
27
     */
28
    public function getAttributeValue($key)
29
    {
30
        if (! $this->isTranslatableAttribute($key)) {
31
            return parent::getAttributeValue($key);
32
        }
33
34
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'));
35
36
        // if it's a fake field, json_encode it
37
        if (is_array($translation)) {
38
            return json_encode($translation);
39
        }
40
41
        return $translation;
42
    }
43
44
    /*
45
    |--------------------------------------------------------------------------
46
    |                            ELOQUENT OVERWRITES
47
    |--------------------------------------------------------------------------
48
    */
49
50
    /**
51
     * Create translated items as json.
52
     *
53
     * @param array $attributes
54
     * @return static
55
     */
56
    public static function create(array $attributes = [])
57
    {
58
        $locale = $attributes['locale'] ?? \App::getLocale();
59
        $attributes = array_except($attributes, ['locale']);
60
        $non_translatable = [];
61
62
        $model = new static();
63
64
        // do the actual saving
65 View Code Duplication
        foreach ($attributes as $attribute => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
67
                $model->setTranslation($attribute, $locale, $value);
68
            } else { // the attribute is NOT translatable
69
                $non_translatable[$attribute] = $value;
70
            }
71
        }
72
        $model->fill($non_translatable)->save();
73
74
        return $model;
75
    }
76
77
    /**
78
     * Update translated items as json.
79
     *
80
     * @param array $attributes
81
     * @param array $options
82
     * @return bool
83
     */
84
    public function update(array $attributes = [], array $options = [])
85
    {
86
        if (! $this->exists) {
87
            return false;
88
        }
89
90
        $locale = $attributes['locale'] ?? \App::getLocale();
91
        $attributes = array_except($attributes, ['locale']);
92
        $non_translatable = [];
93
94
        // do the actual saving
95 View Code Duplication
        foreach ($attributes as $attribute => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
97
                $this->setTranslation($attribute, $locale, $value);
98
            } else { // the attribute is NOT translatable
99
                $non_translatable[$attribute] = $value;
100
            }
101
        }
102
103
        return $this->fill($non_translatable)->save($options);
104
    }
105
106
    /*
107
    |--------------------------------------------------------------------------
108
    |                            CUSTOM METHODS
109
    |--------------------------------------------------------------------------
110
    */
111
112
    /**
113
     * Check if a model is translatable, by the adapter's standards.
114
     *
115
     * @return bool
116
     */
117
    public function translationEnabledForModel()
118
    {
119
        return property_exists($this, 'translatable');
120
    }
121
122
    /**
123
     * Get all locales the admin is allowed to use.
124
     *
125
     * @return array
126
     */
127
    public function getAvailableLocales()
128
    {
129
        return config('backpack.crud.locales');
130
    }
131
132
    /**
133
     * Set the locale property. Used in normalizeLocale() to force the translation
134
     * to a different language that the one set in app()->getLocale();.
135
     *
136
     * @param string
137
     */
138
    public function setLocale($locale)
139
    {
140
        $this->locale = $locale;
141
    }
142
143
    /**
144
     * Get the locale property. Used in SpatieTranslatableSluggableService
145
     * to save the slug for the appropriate language.
146
     *
147
     * @param string
148
     */
149
    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...
150
    {
151
        if ($this->locale) {
152
            return $this->locale;
153
        }
154
155
        return \Request::input('locale', \App::getLocale());
156
    }
157
158
    /**
159
     * Magic method to get the db entries already translated in the wanted locale.
160
     *
161
     * @param string $method
162
     * @param array $parameters
163
     * @return
164
     */
165
    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...
166
    {
167
        switch ($method) {
168
            // translate all find methods
169
            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...
170
            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...
171
            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...
172
            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...
173
            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...
174
175
                $translation_locale = \Request::input('locale', \App::getLocale());
176
177
                if ($translation_locale) {
178
                    $item = parent::__call($method, $parameters);
179
180
                    if ($item instanceof \Traversable) {
181
                        foreach ($item as $instance) {
182
                            $instance->setLocale($translation_locale);
183
                        }
184
                    } elseif ($item) {
185
                        $item->setLocale($translation_locale);
186
                    }
187
188
                    return $item;
189
                }
190
191
                return parent::__call($method, $parameters);
192
                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...
193
194
            // do not translate any other methods
195
            default:
196
                return parent::__call($method, $parameters);
197
                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...
198
        }
199
    }
200
}
201