AbstractPersonalTranslatable   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 50
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getTranslations() 4 4 1
A addTranslation() 9 9 2
A getTranslation() 10 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Model\Gedmo;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Gedmo\Mapping\Annotation as Gedmo;
18
19
/**
20
 * This is your base class if you want to use gedmo personal translation
21
 * ie: if you want to have a dedicated translation table by model table.
22
 *
23
 * @author Nicolas Bastien <[email protected]>
24
 *
25
 * @see https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md : Personal translations
26
 */
27 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...
28
{
29
    /**
30
     * @var ArrayCollection|AbstractPersonalTranslation[]
31
     */
32
    protected $translations;
33
34
    public function __construct()
35
    {
36
        $this->translations = new ArrayCollection();
37
    }
38
39
    /**
40
     * @return ArrayCollection|AbstractPersonalTranslation[]
41
     */
42
    public function getTranslations()
43
    {
44
        return $this->translations;
45
    }
46
47
    /**
48
     * @param string $field
49
     * @param string $locale
50
     *
51
     * @return string|null
52
     */
53
    public function getTranslation($field, $locale)
54
    {
55
        foreach ($this->getTranslations() as $translation) {
56
            if (0 === strcmp($translation->getField(), $field) && 0 === strcmp($translation->getLocale(), $locale)) {
57
                return $translation->getContent();
58
            }
59
        }
60
61
        return null;
62
    }
63
64
    /**
65
     * @return $this
66
     */
67
    public function addTranslation(AbstractPersonalTranslation $translation)
68
    {
69
        if (!$this->translations->contains($translation)) {
70
            $translation->setObject($this);
71
            $this->translations->add($translation);
72
        }
73
74
        return $this;
75
    }
76
}
77