TranslatedTrait::hasTranslate()   A
last analyzed

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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-translated-behavior
4
 * @copyright Copyright (c) 2015 LAV45!
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\translate;
10
11
use yii\db\ActiveRecord;
12
13
/**
14
 * Class TranslatedTrait
15
 * @package lav45\translate
16
 *
17
 * @mixin TranslatedBehavior
18
 */
19
trait TranslatedTrait
20
{
21 5
    public function transactions()
22
    {
23
        return [
24 5
            ActiveRecord::SCENARIO_DEFAULT => ActiveRecord::OP_ALL,
25
        ];
26
    }
27
28
    /**
29
     * @param string $language
30
     * @return bool
31
     */
32 2
    public function hasTranslate($language)
33
    {
34 2
        return isset($this['hasTranslate'][$language]);
35
    }
36
37
    /**
38
     * Returns a value indicating whether the named attribute has been changed
39
     * or attribute has been changed in translation relation model.
40
     * @param string $name the name of the attribute.
41
     * @param boolean $identical whether the comparison of new and old value is made for
42
     * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison.
43
     * @return boolean whether the attribute has been changed
44
     */
45 1
    public function isAttributeChanged($name, $identical = true)
46
    {
47 1
        if (parent::isAttributeChanged($name, $identical) === true) {
48 1
            return true;
49
        }
50 1
        $attributeName = $this->getTranslateAttributeName($name);
0 ignored issues
show
Bug introduced by
It seems like getTranslateAttributeName() 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...
51 1
        return $this->getTranslation()->isAttributeChanged($attributeName, $identical);
0 ignored issues
show
Bug introduced by
It seems like getTranslation() 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...
52
    }
53
}