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
03:08
created

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