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

Passed
Pull Request — main (#4267)
by Pedro
29:16 queued 14:55
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 Spatie\Translatable\HasTranslations as OriginalHasTranslations;
7
8
trait HasTranslations
9
{
10
    use OriginalHasTranslations;
11
12
    /**
13
     * @var bool
14
     */
15
    public $locale = false;
16
17
    /**
18
     * @var bool
19
     */
20
    public $useFallbackLocale = true;
21
22
    /*
23
    |--------------------------------------------------------------------------
24
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
25
    |--------------------------------------------------------------------------
26
    */
27
28
    /**
29
     * Use the forced locale if present.
30
     *
31
     * @param  string  $key
32
     * @return mixed
33
     */
34
    public function getAttributeValue($key)
35
    {
36
        if (! $this->isTranslatableAttribute($key)) {
37
            return parent::getAttributeValue($key);
38
        }
39
40
        $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

40
        $translation = $this->getTranslation($key, /** @scrutinizer ignore-type */ $this->locale ?: config('app.locale'), $this->useFallbackLocale);
Loading history...
41
42
        // if it's a fake field, json_encode it
43
        if (is_array($translation)) {
44
            return json_encode($translation, JSON_UNESCAPED_UNICODE);
45
        }
46
47
        return $translation;
48
    }
49
50
    public function getFallbackLocale()
51
    {
52
        return $this->getFallbackFromUrl() ?? config('app.fallback_locale');
53
    }
54
55
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
56
    {
57
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
58
59
        $translations = $this->getTranslations($key);
60
61
        $translation = $translations[$locale] ?? '';
62
63
        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

63
        if ($this->/** @scrutinizer ignore-call */ hasGetMutator($key)) {
Loading history...
64
            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

64
            return $this->/** @scrutinizer ignore-call */ mutateAttribute($key, $translation);
Loading history...
65
        }
66
67
        return $translation;
68
    }
69
70
    private function getFallbackFromUrl()
71
    {
72
        $fallback = app('crud')->getRequest()->get('_use_fallback');
73
        if (isset($fallback) && in_array($fallback, array_keys(app('crud')->getModel()->getAvailableLocales()))) {
74
            return $fallback;
75
        }
76
77
        return null;
78
    }
79
80
    /*
81
    |--------------------------------------------------------------------------
82
    |                            ELOQUENT OVERWRITES
83
    |--------------------------------------------------------------------------
84
    */
85
86
    /**
87
     * Create translated items as json.
88
     *
89
     * @param  array  $attributes
90
     * @return static
91
     */
92
    public static function create(array $attributes = [])
93
    {
94
        $locale = $attributes['locale'] ?? \App::getLocale();
95
        $attributes = Arr::except($attributes, ['locale']);
96
        $non_translatable = [];
97
98
        $model = new static();
99
100
        // do the actual saving
101
        foreach ($attributes as $attribute => $value) {
102
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
103
                $model->setTranslation($attribute, $locale, $value);
104
            } else { // the attribute is NOT translatable
105
                $non_translatable[$attribute] = $value;
106
            }
107
        }
108
        $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

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