Completed
Push — master ( d17154...1fc820 )
by Sullivan
11:02 queued 08:59
created

AbstractPersonalTranslatable::addTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 9
loc 9
rs 9.6666
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\Model\Gedmo;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Gedmo\Mapping\Annotation as Gedmo;
16
17
/**
18
 * This is your base class if you want to use gedmo personal translation
19
 * ie: if you want to have a dedicated translation table by model table.
20
 *
21
 * @author Nicolas Bastien <[email protected]>
22
 *
23
 * @see https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md : Personal translations
24
 */
25 View Code Duplication
abstract class AbstractPersonalTranslatable extends AbstractTranslatable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
{
27
    /**
28
     * @var ArrayCollection|AbstractPersonalTranslation[]
29
     */
30
    protected $translations;
31
32
    /**
33
     * Constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->translations = new ArrayCollection();
38
    }
39
40
    /**
41
     * @return ArrayCollection|AbstractPersonalTranslation[]
42
     */
43
    public function getTranslations()
44
    {
45
        return $this->translations;
46
    }
47
48
    /**
49
     * @param string $field
50
     * @param string $locale
51
     *
52
     * @return null|string
53
     */
54
    public function getTranslation($field, $locale)
55
    {
56
        foreach ($this->getTranslations() as $translation) {
57
            if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) {
58
                return $translation->getContent();
59
            }
60
        }
61
62
        return;
63
    }
64
65
    /**
66
     * @param AbstractPersonalTranslation $translation
67
     *
68
     * @return $this
69
     */
70
    public function addTranslation(AbstractPersonalTranslation $translation)
71
    {
72
        if (!$this->translations->contains($translation)) {
73
            $translation->setObject($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Sonata\TranslationB...ctPersonalTranslatable>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            $this->translations->add($translation);
75
        }
76
77
        return $this;
78
    }
79
}
80