Translatable::save()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 30
rs 8.439
cc 5
eloc 15
nc 5
nop 1
1
<?php namespace Modules\Core\Internationalisation;
2
3
trait Translatable
4
{
5
    use \Dimsav\Translatable\Translatable;
6
7
    public function save(array $options = array())
8
    {
9
        $tempTranslations = $this->translations;
0 ignored issues
show
Bug introduced by
The property translations 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...
10
        if ($this->exists) {
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...
11
            if (count($this->getDirty()) > 0) {
0 ignored issues
show
Bug introduced by
It seems like getDirty() 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...
12
                // If $this->exists and dirty, parent::save() has to return true. If not,
13
                // an error has occurred. Therefore we shouldn't save the translations.
14
                if (parent::save($options)) {
15
                    $this->translations = $tempTranslations;
16
17
                    return $this->saveTranslations();
18
                }
19
20
                return false;
21
            } else {
22
                // If $this->exists and not dirty, parent::save() skips saving and returns
23
                // false. So we have to save the translations
24
                $this->translations = $tempTranslations;
25
26
                return $this->saveTranslations();
27
            }
28
        } elseif (parent::save($options)) {
29
            // We save the translations only if the instance is saved in the database.
30
            $this->translations = $tempTranslations;
31
32
            return $this->saveTranslations();
33
        }
34
35
        return false;
36
    }
37
}
38