Test Setup Failed
Push — master ( f433ca...bf4061 )
by Tom
04:24 queued 10s
created

Translatable::isEmptyTranslatableAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Dimsav\Translatable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Query\JoinClause;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Illuminate\Database\Query\Builder as QueryBuilder;
10
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
11
12
trait Translatable
13
{
14
    protected static $autoloadTranslations = null;
15
16
    protected $defaultLocale;
17
18
    /**
19
     * Alias for getTranslation().
20
     *
21
     * @param string|null $locale
22
     * @param bool        $withFallback
23
     *
24
     * @return \Illuminate\Database\Eloquent\Model|null
25
     */
26 52
    public function translate($locale = null, $withFallback = false)
27
    {
28 52
        return $this->getTranslation($locale, $withFallback);
29
    }
30
31
    /**
32
     * Alias for getTranslation().
33
     *
34
     * @param string $locale
35
     *
36
     * @return \Illuminate\Database\Eloquent\Model|null
37
     */
38 4
    public function translateOrDefault($locale = null)
39
    {
40 4
        return $this->getTranslation($locale, true);
41
    }
42
43
    /**
44
     * Alias for getTranslationOrNew().
45
     *
46
     * @param string $locale
47
     *
48
     * @return \Illuminate\Database\Eloquent\Model|null
49
     */
50 4
    public function translateOrNew($locale = null)
51
    {
52 4
        return $this->getTranslationOrNew($locale);
53
    }
54
55
    /**
56
     * @param string|null $locale
57
     * @param bool        $withFallback
58
     *
59
     * @return \Illuminate\Database\Eloquent\Model|null
60
     */
61 252
    public function getTranslation($locale = null, $withFallback = null)
62
    {
63 252
        $configFallbackLocale = $this->getFallbackLocale();
64 252
        $locale = $locale ?: $this->locale();
65 252
        $withFallback = $withFallback === null ? $this->useFallback() : $withFallback;
66 252
        $fallbackLocale = $this->getFallbackLocale($locale);
67
68 252
        if ($translation = $this->getTranslationByLocaleKey($locale)) {
69 144
            return $translation;
70
        }
71 172
        if ($withFallback && $fallbackLocale) {
72 28
            if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) {
73 16
                return $translation;
74
            }
75 12
            if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) {
76 8
                return $translation;
77
            }
78
        }
79
80 168
        return null;
81
    }
82
83
    /**
84
     * @param string|null $locale
85
     *
86
     * @return bool
87
     */
88 12
    public function hasTranslation($locale = null)
89
    {
90 12
        $locale = $locale ?: $this->locale();
91
92 12
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93 4
            if ($translation->getAttribute($this->getLocaleKey()) == $locale) {
94 4
                return true;
95
            }
96
        }
97
98 12
        return false;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 316
    public function getTranslationModelName()
105
    {
106 316
        return $this->translationModel ?: $this->getTranslationModelNameDefault();
0 ignored issues
show
Bug introduced by
The property translationModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
    }
108
109
    /**
110
     * @return string
111
     */
112 304
    public function getTranslationModelNameDefault()
113
    {
114 304
        $modelName = get_class($this);
115
116 304
        if ($namespace = $this->getTranslationModelNamespace()) {
117 4
            $modelName = $namespace.'\\'.class_basename(get_class($this));
118
        }
119
120 304
        return $modelName.config('translatable.translation_suffix', 'Translation');
121
    }
122
123
    /**
124
     * @return string|null
125
     */
126 304
    public function getTranslationModelNamespace()
127
    {
128 304
        return config('translatable.translation_model_namespace');
129
    }
130
131
    /**
132
     * @return string
133
     */
134 316
    public function getRelationKey()
135
    {
136 316
        if ($this->translationForeignKey) {
137 28
            $key = $this->translationForeignKey;
0 ignored issues
show
Bug introduced by
The property translationForeignKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
138 292
        } elseif ($this->primaryKey !== 'id') {
0 ignored issues
show
Bug introduced by
The property primaryKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
139
            $key = $this->primaryKey;
140
        } else {
141 292
            $key = $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
142
        }
143
144 316
        return $key;
145
    }
146
147
    /**
148
     * @return string
149
     */
150 296
    public function getLocaleKey()
151
    {
152 296
        return $this->localeKey ?: config('translatable.locale_key', 'locale');
0 ignored issues
show
Bug introduced by
The property localeKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
153
    }
154
155
    /**
156
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
157
     */
158 296
    public function translations()
159
    {
160 296
        return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey());
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 16
    private function usePropertyFallback()
167
    {
168 16
        return $this->useFallback() && config('translatable.use_property_fallback', false);
169
    }
170
171
    /**
172
     * Returns the attribute value from fallback translation if value of attribute
173
     * is empty and the property fallback is enabled in the configuration.
174
     * in model.
175
     * @param $locale
176
     * @param $attribute
177
     * @return mixed
178
     */
179 96
    private function getAttributeOrFallback($locale, $attribute)
180
    {
181 96
        $translation = $this->getTranslation($locale);
182
183
        if (
184
            (
185 96
                ! $translation instanceof Model ||
186 96
                $this->isEmptyTranslatableAttribute($attribute, $translation->$attribute)
187
            ) &&
188 96
            $this->usePropertyFallback()
189
        ) {
190 12
            $translation = $this->getTranslation($this->getFallbackLocale(), false);
191
        }
192
193 96
        if ($translation instanceof Model) {
194 92
            return $translation->$attribute;
195
        }
196
197 8
        return null;
198
    }
199
200 88
    protected function isEmptyTranslatableAttribute(string $key, $value): bool
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
    {
202 88
        return empty($value);
203
    }
204
205
    /**
206
     * @param string $key
207
     *
208
     * @return mixed
209
     */
210 392
    public function getAttribute($key)
211
    {
212 392
        [$attribute, $locale] = $this->getAttributeAndLocale($key);
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $locale does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
213
214 392
        if ($this->isTranslationAttribute($attribute)) {
215 76
            if ($this->getTranslation($locale) === null) {
216 12
                return $this->getAttributeValue($attribute);
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Dimsav\Translatable\Translatable. Did you maybe mean getAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
217
            }
218
219
            // If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue
220
            // on it. This way, we can use Eloquent's checking for Mutation, type casting, and
221
            // Date fields.
222 64
            if ($this->hasGetMutator($attribute)) {
0 ignored issues
show
Bug introduced by
It seems like hasGetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
223 4
                $this->attributes[$attribute] = $this->getAttributeOrFallback($locale, $attribute);
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
224
225 4
                return $this->getAttributeValue($attribute);
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Dimsav\Translatable\Translatable. Did you maybe mean getAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
226
            }
227
228 60
            return $this->getAttributeOrFallback($locale, $attribute);
229
        }
230
231 392
        return parent::getAttribute($key);
232
    }
233
234
    /**
235
     * @param string $key
236
     * @param mixed  $value
237
     *
238
     * @return $this
239
     */
240 392
    public function setAttribute($key, $value)
241
    {
242 392
        [$attribute, $locale] = $this->getAttributeAndLocale($key);
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $locale does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
243
244 392
        if ($this->isTranslationAttribute($attribute)) {
245 40
            $this->getTranslationOrNew($locale)->$attribute = $value;
246
        } else {
247 392
            return parent::setAttribute($key, $value);
248
        }
249
250 40
        return $this;
251
    }
252
253
    /**
254
     * @param array $options
255
     *
256
     * @return bool
257
     */
258 392
    public function save(array $options = [])
259
    {
260 392
        if ($this->exists && ! $this->isDirty()) {
0 ignored issues
show
Bug introduced by
The property exists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like isDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
261
            // If $this->exists and not dirty, parent::save() skips saving and returns
262
            // false. So we have to save the translations
263 24
            if ($this->fireModelEvent('saving') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
264
                return false;
265
            }
266
267 24
            if ($saved = $this->saveTranslations()) {
268 24
                $this->fireModelEvent('saved', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
269 24
                $this->fireModelEvent('updated', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
270
            }
271
272 24
            return $saved;
273
        }
274
275
        // We save the translations only if the instance is saved in the database.
276 392
        if (parent::save($options)) {
277 392
            return $this->saveTranslations();
278
        }
279
280 8
        return false;
281
    }
282
283
    /**
284
     * @param string $locale
285
     *
286
     * @return \Illuminate\Database\Eloquent\Model
287
     */
288 144
    protected function getTranslationOrNew($locale = null)
289
    {
290 144
        $locale = $locale ?: $this->locale();
291
292 144
        if (($translation = $this->getTranslation($locale, false)) === null) {
293 128
            $translation = $this->getNewTranslation($locale);
294
        }
295
296 144
        return $translation;
297
    }
298
299
    /**
300
     * @param array $attributes
301
     *
302
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
303
     * @return $this
304
     */
305 392
    public function fill(array $attributes)
306
    {
307 392
        foreach ($attributes as $key => $values) {
308 116
            if ($this->isKeyALocale($key)) {
309 52
                $this->getTranslationOrNew($key)->fill($values);
310 44
                unset($attributes[$key]);
311
            } else {
312 104
                [$attribute, $locale] = $this->getAttributeAndLocale($key);
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $locale does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
313 104
                if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) {
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
314 48
                    $this->getTranslationOrNew($locale)->fill([$attribute => $values]);
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
315 48
                    unset($attributes[$key]);
316
                }
317
            }
318
        }
319
320 392
        return parent::fill($attributes);
321
    }
322
323
    /**
324
     * @param string $key
325
     */
326 252
    private function getTranslationByLocaleKey($key)
327
    {
328 252
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
329 200
            if ($translation->getAttribute($this->getLocaleKey()) == $key) {
330 168
                return $translation;
331
            }
332
        }
333
334 172
        return null;
335
    }
336
337
    /**
338
     * @param null $locale
339
     *
340
     * @return string
341
     */
342 256
    private function getFallbackLocale($locale = null)
343
    {
344 256
        if ($locale && $this->isLocaleCountryBased($locale)) {
345 28
            if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) {
346 28
                return $fallback;
347
            }
348
        }
349
350 256
        return config('translatable.fallback_locale');
351
    }
352
353
    /**
354
     * @param $locale
355
     *
356
     * @return bool
357
     */
358 252
    private function isLocaleCountryBased($locale)
359
    {
360 252
        return strpos($locale, $this->getLocaleSeparator()) !== false;
361
    }
362
363
    /**
364
     * @param $locale
365
     *
366
     * @return string
367
     */
368 28
    private function getLanguageFromCountryBasedLocale($locale)
369
    {
370 28
        $parts = explode($this->getLocaleSeparator(), $locale);
371
372 28
        return array_get($parts, 0);
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
373
    }
374
375
    /**
376
     * @return bool|null
377
     */
378 148
    private function useFallback()
379
    {
380 148
        if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) {
381 12
            return $this->useTranslationFallback;
0 ignored issues
show
Bug introduced by
The property useTranslationFallback does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
382
        }
383
384 136
        return config('translatable.use_fallback');
385
    }
386
387
    /**
388
     * @param string $key
389
     *
390
     * @return bool
391
     */
392 392
    public function isTranslationAttribute($key)
393
    {
394 392
        return in_array($key, $this->translatedAttributes);
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
395
    }
396
397
    /**
398
     * @param string $key
399
     *
400
     * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
401
     * @return bool
402
     */
403 116
    protected function isKeyALocale($key)
404
    {
405 116
        $locales = $this->getLocales();
406
407 112
        return in_array($key, $locales);
408
    }
409
410
    /**
411
     * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
412
     * @return array
413
     */
414 116
    protected function getLocales()
415
    {
416 116
        $localesConfig = (array) config('translatable.locales');
417
418 116
        if (empty($localesConfig)) {
419 4
            throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '.
420 4
                ' and that the locales configuration is defined.');
421
        }
422
423 112
        $locales = [];
424 112
        foreach ($localesConfig as $key => $locale) {
425 112
            if (is_array($locale)) {
426 20
                $locales[] = $key;
427 20
                foreach ($locale as $countryLocale) {
428 20
                    $locales[] = $key.$this->getLocaleSeparator().$countryLocale;
429
                }
430
            } else {
431 104
                $locales[] = $locale;
432
            }
433
        }
434
435 112
        return $locales;
436
    }
437
438
    /**
439
     * @return string
440
     */
441 252
    protected function getLocaleSeparator()
442
    {
443 252
        return config('translatable.locale_separator', '-');
444
    }
445
446
    /**
447
     * @return bool
448
     */
449 392
    protected function saveTranslations()
450
    {
451 392
        $saved = true;
452
453 392
        if (! $this->relationLoaded('translations')) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
454 392
            return $saved;
455
        }
456
457 104
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
458 104
            if ($saved && $this->isTranslationDirty($translation)) {
459 104
                if (! empty($connectionName = $this->getConnectionName())) {
0 ignored issues
show
Bug introduced by
It seems like getConnectionName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
460 104
                    $translation->setConnection($connectionName);
461
                }
462
463 104
                $translation->setAttribute($this->getRelationKey(), $this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
464 104
                $saved = $translation->save();
465
            }
466
        }
467
468 100
        return $saved;
469
    }
470
471
    /**
472
     * @param array
473
     *
474
     * @return \Illuminate\Database\Eloquent\Model
475
     */
476 4
    public function replicateWithTranslations(array $except = null)
477
    {
478 4
        $newInstance = parent::replicate($except);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (replicate() instead of replicateWithTranslations()). Are you sure this is correct? If so, you might want to change this to $this->replicate().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
479
480 4
        unset($newInstance->translations);
481 4
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
482 4
            $newTranslation = $translation->replicate();
483 4
            $newInstance->translations->add($newTranslation);
484
        }
485
486 4
        return  $newInstance;
487
    }
488
489
    /**
490
     * @param \Illuminate\Database\Eloquent\Model $translation
491
     *
492
     * @return bool
493
     */
494 104
    protected function isTranslationDirty(Model $translation)
495
    {
496 104
        $dirtyAttributes = $translation->getDirty();
497 104
        unset($dirtyAttributes[$this->getLocaleKey()]);
498
499 104
        return count($dirtyAttributes) > 0;
500
    }
501
502
    /**
503
     * @param string $locale
504
     *
505
     * @return \Illuminate\Database\Eloquent\Model
506
     */
507 132
    public function getNewTranslation($locale)
508
    {
509 132
        $modelName = $this->getTranslationModelName();
510 132
        $translation = new $modelName();
511 132
        $translation->setAttribute($this->getLocaleKey(), $locale);
512 132
        $this->translations->add($translation);
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
513
514 132
        return $translation;
515
    }
516
517
    /**
518
     * @param $key
519
     *
520
     * @return bool
521
     */
522 152
    public function __isset($key)
523
    {
524 152
        return $this->isTranslationAttribute($key) || parent::__isset($key);
525
    }
526
527
    /**
528
     * @param \Illuminate\Database\Eloquent\Builder $query
529
     * @param string                                $locale
530
     *
531
     * @return \Illuminate\Database\Eloquent\Builder|static
532
     */
533 8
    public function scopeTranslatedIn(Builder $query, $locale = null)
534
    {
535 8
        $locale = $locale ?: $this->locale();
536
537
        return $query->whereHas('translations', function (Builder $q) use ($locale) {
538 8
            $q->where($this->getLocaleKey(), '=', $locale);
539 8
        });
540
    }
541
542
    /**
543
     * @param \Illuminate\Database\Eloquent\Builder $query
544
     * @param string                                $locale
545
     *
546
     * @return \Illuminate\Database\Eloquent\Builder|static
547
     */
548 8
    public function scopeNotTranslatedIn(Builder $query, $locale = null)
549
    {
550 8
        $locale = $locale ?: $this->locale();
551
552
        return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) {
553 8
            $q->where($this->getLocaleKey(), '=', $locale);
554 8
        });
555
    }
556
557
    /**
558
     * @param \Illuminate\Database\Eloquent\Builder $query
559
     *
560
     * @return \Illuminate\Database\Eloquent\Builder|static
561
     */
562 4
    public function scopeTranslated(Builder $query)
563
    {
564 4
        return $query->has('translations');
565
    }
566
567
    /**
568
     * Adds scope to get a list of translated attributes, using the current locale.
569
     * Example usage: Country::listsTranslations('name')->get()->toArray()
570
     * Will return an array with items:
571
     *  [
572
     *      'id' => '1',                // The id of country
573
     *      'name' => 'Griechenland'    // The translated name
574
     *  ].
575
     *
576
     * @param \Illuminate\Database\Eloquent\Builder $query
577
     * @param string                                $translationField
578
     */
579 12
    public function scopeListsTranslations(Builder $query, $translationField)
580
    {
581 12
        $withFallback = $this->useFallback();
582 12
        $translationTable = $this->getTranslationsTable();
583 12
        $localeKey = $this->getLocaleKey();
584
585
        $query
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
586 12
            ->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField)
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
587 12
            ->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName())
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
588 12
            ->where($translationTable.'.'.$localeKey, $this->locale());
589 12
        if ($withFallback) {
590
            $query->orWhere(function (Builder $q) use ($translationTable, $localeKey) {
591 4
                $q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale())
592
                  ->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use (
593 4
                      $translationTable,
594 4
                      $localeKey
595
                  ) {
596 4
                      $q->select($translationTable.'.'.$this->getRelationKey())
597 4
                        ->from($translationTable)
598 4
                        ->where($translationTable.'.'.$localeKey, $this->locale());
599 4
                  });
600 4
            });
601
        }
602 12
    }
603
604
    /**
605
     * This scope eager loads the translations for the default and the fallback locale only.
606
     * We can use this as a shortcut to improve performance in our application.
607
     *
608
     * @param Builder $query
609
     */
610 12
    public function scopeWithTranslation(Builder $query)
611
    {
612 12
        $query->with([
613
            'translations' => function (Relation $query) {
614 12
                if ($this->useFallback()) {
615 8
                    $locale = $this->locale();
616 8
                    $countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de
617 8
                    $locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]);
618
619 8
                    return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales);
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Illuminate\Database\Eloquent\Relations\Relation. Did you maybe mean whereInMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
620
                }
621
622 4
                return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale());
623 12
            },
624
        ]);
625 12
    }
626
627
    /**
628
     * This scope filters results by checking the translation fields.
629
     *
630
     * @param \Illuminate\Database\Eloquent\Builder $query
631
     * @param string                                $key
632
     * @param string                                $value
633
     * @param string                                $locale
634
     *
635
     * @return \Illuminate\Database\Eloquent\Builder|static
636
     */
637 12
    public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null)
638
    {
639
        return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) {
640 12
            $query->where($this->getTranslationsTable().'.'.$key, $value);
641 12
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
642 4
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
643
            }
644 12
        });
645
    }
646
647
    /**
648
     * This scope filters results by checking the translation fields.
649
     *
650
     * @param \Illuminate\Database\Eloquent\Builder $query
651
     * @param string                                $key
652
     * @param string                                $value
653
     * @param string                                $locale
654
     *
655
     * @return \Illuminate\Database\Eloquent\Builder|static
656
     */
657 4
    public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null)
658
    {
659
        return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) {
660 4
            $query->where($this->getTranslationsTable().'.'.$key, $value);
661 4
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
662
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale);
663
            }
664 4
        });
665
    }
666
667
    /**
668
     * This scope filters results by checking the translation fields.
669
     *
670
     * @param \Illuminate\Database\Eloquent\Builder $query
671
     * @param string                                $key
672
     * @param string                                $value
673
     * @param string                                $locale
674
     *
675
     * @return \Illuminate\Database\Eloquent\Builder|static
676
     */
677 12
    public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null)
678
    {
679
        return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) {
680 12
            $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value);
681 12
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
682 4
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale);
683
            }
684 12
        });
685
    }
686
687
    /**
688
     * This scope filters results by checking the translation fields.
689
     *
690
     * @param \Illuminate\Database\Eloquent\Builder $query
691
     * @param string                                $key
692
     * @param string                                $value
693
     * @param string                                $locale
694
     *
695
     * @return \Illuminate\Database\Eloquent\Builder|static
696
     */
697 4
    public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null)
698
    {
699
        return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) {
700 4
            $query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value);
701 4
            if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
702
                $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale);
703
            }
704 4
        });
705
    }
706
707
    /**
708
     * This scope sorts results by the given translation field.
709
     *
710
     * @param \Illuminate\Database\Eloquent\Builder $query
711
     * @param string                                $key
712
     * @param string                                $sortmethod
713
     *
714
     * @return \Illuminate\Database\Eloquent\Builder|static
715
     */
716 8
    public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc')
717
    {
718 8
        $translationTable = $this->getTranslationsTable();
719 8
        $localeKey = $this->getLocaleKey();
720 8
        $table = $this->getTable();
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
721 8
        $keyName = $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
722
723
        return $query
724
            ->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
725
                $join
726 8
                    ->on($translationTable.'.'.$this->getRelationKey(), '=', $table.'.'.$keyName)
727 8
                    ->where($translationTable.'.'.$localeKey, $this->locale());
728 8
            })
729 8
            ->orderBy($translationTable.'.'.$key, $sortmethod)
730 8
            ->select($table.'.*')
731 8
            ->with('translations');
732
    }
733
734
    /**
735
     * @return array
736
     */
737 48
    public function attributesToArray()
738
    {
739 48
        $attributes = parent::attributesToArray();
740
741
        if (
742 48
            (! $this->relationLoaded('translations') && ! $this->toArrayAlwaysLoadsTranslations() && is_null(self::$autoloadTranslations))
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
743 48
            || self::$autoloadTranslations === false
744
        ) {
745 16
            return $attributes;
746
        }
747
748 32
        $hiddenAttributes = $this->getHidden();
0 ignored issues
show
Bug introduced by
It seems like getHidden() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
749
750 32
        foreach ($this->translatedAttributes as $field) {
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
751 32
            if (in_array($field, $hiddenAttributes)) {
752 4
                continue;
753
            }
754
755 32
            $attributes[$field] = $this->getAttributeOrFallback(null, $field);
756
        }
757
758 32
        return $attributes;
759
    }
760
761
    /**
762
     * @return array
763
     */
764 4
    public function getTranslationsArray()
765
    {
766 4
        $translations = [];
767
768 4
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean autoloadTranslations?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
769 4
            foreach ($this->translatedAttributes as $attr) {
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist. Did you mean attributes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
770 4
                $translations[$translation->{$this->getLocaleKey()}][$attr] = $translation->{$attr};
771
            }
772
        }
773
774 4
        return $translations;
775
    }
776
777
    /**
778
     * @return string
779
     */
780 56
    private function getTranslationsTable()
781
    {
782 56
        return app()->make($this->getTranslationModelName())->getTable();
783
    }
784
785
    /**
786
     * @return string
787
     */
788 392
    protected function locale()
789
    {
790 392
        if ($this->defaultLocale) {
791 4
            return $this->defaultLocale;
792
        }
793
794 392
        return config('translatable.locale')
795 392
            ?: app()->make('translator')->getLocale();
796
    }
797
798
    /**
799
     * Set the default locale on the model.
800
     *
801
     * @param $locale
802
     *
803
     * @return $this
804
     */
805 4
    public function setDefaultLocale($locale)
806
    {
807 4
        $this->defaultLocale = $locale;
808
809 4
        return $this;
810
    }
811
812
    /**
813
     * Get the default locale on the model.
814
     *
815
     * @return mixed
816
     */
817
    public function getDefaultLocale()
818
    {
819
        return $this->defaultLocale;
820
    }
821
822
    /**
823
     * Deletes all translations for this model.
824
     *
825
     * @param string|array|null $locales The locales to be deleted (array or single string)
826
     *                                   (e.g., ["en", "de"] would remove these translations).
827
     */
828 12
    public function deleteTranslations($locales = null)
829
    {
830 12
        if ($locales === null) {
831 4
            $translations = $this->translations()->get();
832
        } else {
833 8
            $locales = (array) $locales;
834 8
            $translations = $this->translations()->whereIn($this->getLocaleKey(), $locales)->get();
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Illuminate\Database\Eloquent\Relations\HasMany. Did you maybe mean whereInMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
835
        }
836 12
        foreach ($translations as $translation) {
837 8
            $translation->delete();
838
        }
839
840
        // we need to manually "reload" the collection built from the relationship
841
        // otherwise $this->translations()->get() would NOT be the same as $this->translations
842 12
        $this->load('translations');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
843 12
    }
844
845
    /**
846
     * @param $key
847
     *
848
     * @return array
849
     */
850 392
    private function getAttributeAndLocale($key)
851
    {
852 392
        if (str_contains($key, ':')) {
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
853 44
            return explode(':', $key);
854
        }
855
856 392
        return [$key, $this->locale()];
857
    }
858
859
    /**
860
     * @return bool
861
     */
862 32
    private function toArrayAlwaysLoadsTranslations()
863
    {
864 32
        return config('translatable.to_array_always_loads_translations', true);
865
    }
866
867
    public static function enableAutoloadTranslations()
868
    {
869
        self::$autoloadTranslations = true;
870
    }
871
872 4
    public static function defaultAutoloadTranslations()
873
    {
874 4
        self::$autoloadTranslations = null;
875 4
    }
876
877 4
    public static function disableAutoloadTranslations()
878
    {
879 4
        self::$autoloadTranslations = false;
880 4
    }
881
}
882