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
05:49 queued 02:52
created

SpatieTranslatableAdaptor::getAttributeValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 9.2
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
    |--------------------------------------------------------------------------
98
    |                            CUSTOM METHODS
99
    |--------------------------------------------------------------------------
100
    */
101
102
    /**
103
     * Check if a model is translatable, by the adapter's standards.
104
     *
105
     * @return bool
106
     */
107
    public function translationEnabledForModel()
108
    {
109
        return property_exists($this, 'translatable');
110
    }
111
112
    /**
113
     * Get all locales the admin is allowed to use.
114
     *
115
     * @return array
116
     */
117
    public function getAvailableLocales()
118
    {
119
        return config('backpack.crud.locales');
120
    }
121
122
    /**
123
     * Set the locale property. Used in normalizeLocale() to force the translation
124
     * to a different language that the one set in app()->getLocale();.
125
     *
126
     * @param string
127
     */
128
    public function setLocale($locale)
129
    {
130
        $this->locale = $locale;
131
    }
132
133
    /**
134
     * Magic method to get the db entries already translated in the wanted locale.
135
     */
136
    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...
137
    {
138
        switch ($method) {
139
            // translate all find methods
140
            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...
141
            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...
142
            case 'findMany':
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...
143
144
                $translation_locale = \Request::input('locale');
145
                $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...
146
147
                if ($translation_locale) {
148
                    $item = parent::__call($method, $parameters);
149
                    $item->setLocale($translation_locale);
150
151
                    return $item;
152
                }
153
154
                return parent::__call($method, $parameters);
155
                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...
156
157
            // do not translate any other methods
158
            default:
159
                return parent::__call($method, $parameters);
160
                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...
161
        }
162
    }
163
}
164