Completed
Pull Request — master (#21)
by Valentyn
01:42
created

TranslatableTrait::addTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Translation;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
trait TranslatableTrait
8
{
9
    private $isTranslationsMappedByLocale = false;
10
11 3
    public function addTranslation(EntityTranslationInterface $translation): self
12
    {
13 3
        $this->translations->set($translation->getLocale(), $translation);
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean isTranslationsMappedByLocale?

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...
14
15 3
        return $this;
16
    }
17
18 6
    public function getTranslation(string $locale, bool $useFallbackLocale = true): ?EntityTranslationInterface
19
    {
20 6
        if ($this->isTranslationsMappedByLocale === false) {
21 6
            $this->mapTranslationsByLocale();
22
        }
23
24 6
        $translation = $this->translations->get($locale);
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean isTranslationsMappedByLocale?

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...
25
26 6
        if ($translation === null && $useFallbackLocale === true) {
27 2
            return $this->getFallbackTranslation();
28
        }
29
30 5
        return $translation;
31
    }
32
33 2
    private function getFallbackTranslation()
34
    {
35 2
        if (null === $translation = $this->translations->first()) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean isTranslationsMappedByLocale?

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...
36
            throw new \ErrorException(sprintf('You are trying to get translation for %s with ID %s but there\'s no translations found.', self::class, $this->id));
0 ignored issues
show
Bug introduced by
The property id 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...
37
        }
38
39 2
        return $translation;
40
    }
41
42 6
    private function mapTranslationsByLocale(): void
43
    {
44 6
        $mappedTranslations = new ArrayCollection();
45
46 6
        foreach ($this->translations as $translation) {
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean isTranslationsMappedByLocale?

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...
47 5
            $mappedTranslations->set($translation->getLocale(), $translation);
48
        }
49
50 6
        $this->isTranslationsMappedByLocale = true;
51 6
        $this->translations = $mappedTranslations;
0 ignored issues
show
Bug introduced by
The property translations does not seem to exist. Did you mean isTranslationsMappedByLocale?

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...
52
    }
53
}