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 (#382)
by Cristian
03:31
created

HasTranslations::__call()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 16
nop 2
dl 0
loc 31
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable;
4
5
use Spatie\Translatable\HasTranslations;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Backpack\CRUD\ModelTrait...latable\HasTranslations.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
trait HasTranslations
8
{
9
    use HasTranslations;
10
11
    /**
12
     * @var bool
13
     */
14
    public $locale = false;
15
16
    /*
17
    |--------------------------------------------------------------------------
18
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
19
    |--------------------------------------------------------------------------
20
    */
21
22
    /**
23
     * Use the forced locale if present.
24
     *
25
     * @param string $key
26
     * @return mixed
27
     */
28
    public function getAttributeValue($key)
29
    {
30
        if (! $this->isTranslatableAttribute($key)) {
31
            return parent::getAttributeValue($key);
32
        }
33
34
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'));
35
36
        return is_array($translation) ? array_first($translation) : $translation;
37
    }
38
39
    /*
40
    |--------------------------------------------------------------------------
41
    |                            ELOQUENT OVERWRITES
42
    |--------------------------------------------------------------------------
43
    */
44
45
    /**
46
     * Create translated items as json.
47
     *
48
     * @param array $attributes
49
     * @return static
50
     */
51
    public static function create(array $attributes = [])
52
    {
53
        $locale = $attributes['locale'] ?? \App::getLocale();
54
        $attributes = array_except($attributes, ['locale']);
55
56
        $model = new static();
57
58
        // do the actual saving
59
        foreach ($attributes as $attribute => $value) {
60
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
61
                $model->setTranslation($attribute, $locale, $value);
62
            } else { // the attribute is NOT translatable
63
                $model->{$attribute} = $value;
64
            }
65
        }
66
        $model->save();
67
68
        return $model;
69
    }
70
71
    /**
72
     * Update translated items as json.
73
     *
74
     * @param array $attributes
75
     * @param array $options
76
     * @return bool
77
     */
78
    public function update(array $attributes = [], array $options = [])
79
    {
80
        if (! $this->exists) {
81
            return false;
82
        }
83
84
        $locale = $attributes['locale'] ?? \App::getLocale();
85
        $attributes = array_except($attributes, ['locale']);
86
87
        // do the actual saving
88
        foreach ($attributes as $attribute => $value) {
89
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
90
                $this->setTranslation($attribute, $locale, $value);
91
            } else { // the attribute is NOT translatable
92
                $this->{$attribute} = $value;
93
            }
94
        }
95
96
        return $this->save($options);
97
    }
98
99
    /*
100
    |--------------------------------------------------------------------------
101
    |                            CUSTOM METHODS
102
    |--------------------------------------------------------------------------
103
    */
104
105
    /**
106
     * Check if a model is translatable, by the adapter's standards.
107
     *
108
     * @return bool
109
     */
110
    public function translationEnabledForModel()
111
    {
112
        return property_exists($this, 'translatable');
113
    }
114
115
    /**
116
     * Get all locales the admin is allowed to use.
117
     *
118
     * @return array
119
     */
120
    public function getAvailableLocales()
121
    {
122
        return config('backpack.crud.locales');
123
    }
124
125
    /**
126
     * Set the locale property. Used in normalizeLocale() to force the translation
127
     * to a different language that the one set in app()->getLocale();.
128
     *
129
     * @param string
130
     */
131
    public function setLocale($locale)
132
    {
133
        $this->locale = $locale;
134
    }
135
136
    /**
137
     * Get the locale property. Used in SpatieTranslatableSluggableService
138
     * to save the slug for the appropriate language.
139
     *
140
     * @param string
141
     */
142
    public function getLocale()
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...
143
    {
144
        if ($this->locale) {
145
            return $this->locale;
146
        }
147
148
        return \Request::input('locale', \App::getLocale());
149
    }
150
151
    /**
152
     * Magic method to get the db entries already translated in the wanted locale.
153
     *
154
     * @param string $method
155
     * @param array $parameters
156
     * @return
157
     */
158
    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...
159
    {
160
        switch ($method) {
161
            // translate all find methods
162
            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...
163
            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...
164
            case 'findMany':
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...
165
            case 'findBySlug':
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...
166
            case 'findBySlugOrFail':
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...
167
168
                $translation_locale = \Request::input('locale', \App::getLocale());
169
170
                if ($translation_locale) {
171
                    $item = parent::__call($method, $parameters);
172
173
                    if ($item) {
174
                        $item->setLocale($translation_locale);
175
                    }
176
177
                    return $item;
178
                }
179
180
                return parent::__call($method, $parameters);
181
                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...
182
183
            // do not translate any other methods
184
            default:
185
                return parent::__call($method, $parameters);
186
                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...
187
        }
188
    }
189
}
190