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 (#317)
by Cristian
02:35
created

SpatieTranslatableAdaptor::setLocale()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\ModelTraits\Translatable;
4
5
use Spatie\Translatable\HasTranslations;
6
7
trait SpatieTranslatableAdaptor
8
{
9
    use HasTranslations;
10
11
    public $locale = false;
12
13
    /*
14
    |--------------------------------------------------------------------------
15
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
16
    |--------------------------------------------------------------------------
17
    */
18
19
    /**
20
     * Use the forced locale if present.
21
     *
22
     * @param  [type] $key [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
     * @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...
24
     */
25
    public function getAttributeValue($key)
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...
26
    {
27
        if (! $this->isTranslatableAttribute($key)) {
28
            return parent::getAttributeValue($key);
29
        }
30
31
        return $this->getTranslation($key, $this->locale ?: config('app.locale'));
32
    }
33
34
    /*
35
    |--------------------------------------------------------------------------
36
    |                            ELOQUENT OVERWRITES
37
    |--------------------------------------------------------------------------
38
    */
39
40
    /**
41
     * Create translated items as json.
42
     *
43
     * @param  array  $attributes [description]
44
     * @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...
45
     */
46
    public static function create(array $attributes = [])
47
    {
48
        $locale = $attributes['locale'] ? $attributes['locale'] : App::getLocale();
49
        $attributes = array_except($attributes, ['locale']);
50
51
        $model = new static();
52
53
        // do the actual saving
54
        foreach ($attributes as $attribute => $value) {
55
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
56
                $model->setTranslation($attribute, $locale, $value);
57
            } else { // the attribute is NOT translatable
58
                $model->{$attribute} = $value;
59
            }
60
        }
61
        $model->save();
62
63
        return $model;
64
    }
65
66
    /**
67
     * Update translated items as json.
68
     *
69
     * @param  array  $attributes
70
     * @param  array  $options
71
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|SpatieTranslatableAdaptor?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
72
     */
73
    public function update(array $attributes = [], array $options = [])
74
    {
75
        if (! $this->exists) {
76
            return false;
77
        }
78
79
        $locale = $attributes['locale'] ? $attributes['locale'] : App::getLocale();
80
        $attributes = array_except($attributes, ['locale']);
81
82
        // do the actual saving
83
        foreach ($attributes as $attribute => $value) {
84
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
85
                $this->setTranslation($attribute, $locale, $value);
86
            } else { // the attribute is NOT translatable
87
                $this->{$attribute} = $value;
88
            }
89
        }
90
        $this->save($options);
91
92
        return $this;
93
    }
94
95
    /**
96
     * Get the database entry in the wanted locale.
97
     *
98
     * @param  [int] The id of the row in the db to fetch.
99
     *
100
     * @return [Eloquent Collection] The row in the db.
0 ignored issues
show
Documentation introduced by
The doc-type Eloquent">[Eloquent 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...
101
     */
102 View Code Duplication
    public function findOrFail($id)
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...
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
        $translation_locale = \Request::input('locale');
105
        $default_locale = \App::getLocale();
0 ignored issues
show
Unused Code introduced by
$default_locale 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...
106
107
        if ($translation_locale) {
108
            $item = parent::findOrFail($id);
109
            $item->setLocale($translation_locale);
110
111
            return $item;
112
        }
113
114
        return parent::findOrFail($id);
115
    }
116
117
    /**
118
     * Get the database entry in the wanted locale.
119
     *
120
     * @param  [int] The id of the row in the db to fetch.
121
     *
122
     * @return [Eloquent Collection] The row in the db.
0 ignored issues
show
Documentation introduced by
The doc-type Eloquent">[Eloquent 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...
123
     */
124 View Code Duplication
    public function find($id)
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...
Duplication introduced by
This method seems to be duplicated in 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...
125
    {
126
        $translation_locale = \Request::input('locale');
127
        $default_locale = \App::getLocale();
0 ignored issues
show
Unused Code introduced by
$default_locale 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...
128
129
        if ($translation_locale) {
130
            $item = parent::find($id);
131
            $item->setLocale($translation_locale);
132
133
            return $item;
134
        }
135
136
        return parent::find($id);
137
    }
138
139
    /*
140
    |--------------------------------------------------------------------------
141
    |                            CUSTOM METHODS
142
    |--------------------------------------------------------------------------
143
    */
144
145
    /**
146
     * Check if a model is translatable, by the adapter's standards.
147
     *
148
     * @return bool
149
     */
150
    public function translationEnabledForModel()
151
    {
152
        return property_exists($this, 'translatable');
153
    }
154
155
    /**
156
     * Get all locales the admin is allowed to use.
157
     *
158
     * @return array
159
     */
160
    public function getAvailableLocales()
161
    {
162
        return config('backpack.crud.locales');
163
    }
164
165
    /**
166
     * Set the locale property. Used in normalizeLocale() to force the translation
167
     * to a different language that the one set in app()->getLocale();.
168
     *
169
     * @param string
170
     */
171
    public function setLocale($locale)
172
    {
173
        $this->locale = $locale;
174
    }
175
}
176