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

Test Failed
Pull Request — main (#4267)
by Cristian
28:44 queued 14:24
created

HasTranslations::getFallbackFromUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits\SpatieTranslatable;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Session;
7
use Spatie\Translatable\HasTranslations as OriginalHasTranslations;
8
9
trait HasTranslations
10
{
11
    use OriginalHasTranslations;
12
13
    /**
14
     * @var bool
15
     */
16
    public $locale = false;
17
18
    /**
19
     * @var bool
20
     */
21
    public $useFallbackLocale = true;
22
23
    /*
24
    |--------------------------------------------------------------------------
25
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
26
    |--------------------------------------------------------------------------
27
    */
28
29
    /**
30
     * Use the forced locale if present.
31
     *
32
     * @param  string  $key
33
     * @return mixed
34
     */
35
    public function getAttributeValue($key)
36
    {
37
        if (! $this->isTranslatableAttribute($key)) {
38
            return parent::getAttributeValue($key);
39
        }
40
41
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'), $this->useFallbackLocale);
0 ignored issues
show
Bug introduced by
It seems like $this->locale ?: config('app.locale') can also be of type true; however, parameter $locale of Backpack\CRUD\app\Models...tions::getTranslation() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $translation = $this->getTranslation($key, /** @scrutinizer ignore-type */ $this->locale ?: config('app.locale'), $this->useFallbackLocale);
Loading history...
42
43
        // if it's a fake field, json_encode it
44
        if (is_array($translation)) {
45
            return json_encode($translation, JSON_UNESCAPED_UNICODE);
46
        }
47
48
        return $translation;
49
    }
50
51
    public function getFallbackLocale()
52
    {
53
        return $this->getFallbackFromUrl() ?? Session::get('backpack_fallback_locale') ?? config('app.fallback_locale');
54
    }
55
56
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
57
    {
58
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
59
60
        $translations = $this->getTranslations($key);
61
62
        $translation = $translations[$locale] ?? '';
63
64
        if ($this->hasGetMutator($key)) {
0 ignored issues
show
Bug introduced by
The method hasGetMutator() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        if ($this->/** @scrutinizer ignore-call */ hasGetMutator($key)) {
Loading history...
65
            return $this->mutateAttribute($key, $translation);
0 ignored issues
show
Bug introduced by
The method mutateAttribute() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            return $this->/** @scrutinizer ignore-call */ mutateAttribute($key, $translation);
Loading history...
66
        }
67
68
        return $translation;
69
    }
70
71
    private function getFallbackFromUrl()
72
    {
73
        $fallback = app('crud')->getRequest()->get('_use_fallback');
74
        if (isset($fallback) && in_array($fallback, array_keys(app('crud')->getModel()->getAvailableLocales()))) {
75
            return $fallback;
76
        }
77
78
        return null;
79
    }
80
81
    /*
82
    |--------------------------------------------------------------------------
83
    |                            ELOQUENT OVERWRITES
84
    |--------------------------------------------------------------------------
85
    */
86
87
    /**
88
     * Create translated items as json.
89
     *
90
     * @param  array  $attributes
91
     * @return static
92
     */
93
    public static function create(array $attributes = [])
94
    {
95
        $locale = $attributes['locale'] ?? \App::getLocale();
96
        $attributes = Arr::except($attributes, ['locale']);
97
        $non_translatable = [];
98
99
        $model = new static();
100
101
        // do the actual saving
102
        foreach ($attributes as $attribute => $value) {
103
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
104
                $model->setTranslation($attribute, $locale, $value);
105
            } else { // the attribute is NOT translatable
106
                $non_translatable[$attribute] = $value;
107
            }
108
        }
109
        $model->fill($non_translatable)->save();
0 ignored issues
show
Bug introduced by
The method fill() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        $model->/** @scrutinizer ignore-call */ 
110
                fill($non_translatable)->save();
Loading history...
110
111
        return $model;
112
    }
113
114
    /**
115
     * Update translated items as json.
116
     *
117
     * @param  array  $attributes
118
     * @param  array  $options
119
     * @return bool
120
     */
121
    public function update(array $attributes = [], array $options = [])
122
    {
123
        if (! $this->exists) {
124
            return false;
125
        }
126
127
        $locale = $attributes['_locale'] ?? \App::getLocale();
128
        $attributes = Arr::except($attributes, ['_locale']);
129
        $non_translatable = [];
130
131
        // do the actual saving
132
        foreach ($attributes as $attribute => $value) {
133
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
134
                $this->setTranslation($attribute, $locale, $value);
135
            } else { // the attribute is NOT translatable
136
                $non_translatable[$attribute] = $value;
137
            }
138
        }
139
140
        return $this->fill($non_translatable)->save($options);
141
    }
142
143
    /*
144
    |--------------------------------------------------------------------------
145
    |                            CUSTOM METHODS
146
    |--------------------------------------------------------------------------
147
    */
148
149
    /**
150
     * Check if a model is translatable, by the adapter's standards.
151
     *
152
     * @return bool
153
     */
154
    public function translationEnabledForModel()
155
    {
156
        return property_exists($this, 'translatable');
157
    }
158
159
    /**
160
     * Get all locales the admin is allowed to use.
161
     *
162
     * @return array
163
     */
164
    public function getAvailableLocales()
165
    {
166
        return config('backpack.crud.locales');
167
    }
168
169
    /**
170
     * Set the locale property. Used in normalizeLocale() to force the translation
171
     * to a different language that the one set in app()->getLocale();.
172
     *
173
     * @param string
174
     */
175
    public function setLocale($locale)
176
    {
177
        $this->locale = $locale;
178
    }
179
180
    /**
181
     * Get the locale property. Used in SpatieTranslatableSluggableService
182
     * to save the slug for the appropriate language.
183
     *
184
     * @param string
185
     */
186
    public function getLocale()
187
    {
188
        if ($this->locale) {
189
            return $this->locale;
190
        }
191
192
        return \Request::input('_locale', \App::getLocale());
193
    }
194
195
    /**
196
     * Magic method to get the db entries already translated in the wanted locale.
197
     *
198
     * @param  string  $method
199
     * @param  array  $parameters
200
     * @return
201
     */
202
    public function __call($method, $parameters)
203
    {
204
        switch ($method) {
205
            // translate all find methods
206
            case 'find':
207
            case 'findOrFail':
208
            case 'findMany':
209
            case 'findBySlug':
210
            case 'findBySlugOrFail':
211
                $translation_locale = \Request::input('_locale', \App::getLocale());
212
213
                if ($translation_locale) {
214
                    $item = parent::__call($method, $parameters);
215
216
                    if ($item instanceof \Traversable) {
217
                        foreach ($item as $instance) {
218
                            $instance->setLocale($translation_locale);
219
                        }
220
                    } elseif ($item) {
221
                        $item->setLocale($translation_locale);
222
                    }
223
224
                    return $item;
225
                }
226
227
                return parent::__call($method, $parameters);
228
                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...
229
230
                // do not translate any other methods
231
            default:
232
                return parent::__call($method, $parameters);
233
                break;
234
        }
235
    }
236
}
237