Completed
Pull Request — 2.x (#124)
by Christian
02:27
created

PersonalTranslatableTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 44
loc 44
rs 10

3 Methods

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

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
/*
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\Traits\Gedmo;
13
14
use Sonata\TranslationBundle\Traits\TranslatableTrait;
15
16
/**
17
 * If you don't want to use trait, you can extend AbstractPersonalTranslatable instead.
18
 *
19
 * @author Nicolas Bastien <[email protected]>
20
 */
21 View Code Duplication
trait PersonalTranslatableTrait
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...
22
{
23
    use TranslatableTrait;
24
25
    /**
26
     * @return ArrayCollection|AbstractPersonalTranslation[]
27
     */
28
    public function getTranslations()
29
    {
30
        return $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...
31
    }
32
33
    /**
34
     * @param $field
35
     * @param $locale
36
     *
37
     * @return null|string
38
     */
39
    public function getTranslation($field, $locale)
40
    {
41
        foreach ($this->getTranslations() as $translation) {
42
            if (strcmp($translation->getField(), $field) === 0 && strcmp($translation->getLocale(), $locale) === 0) {
43
                return $translation->getContent();
44
            }
45
        }
46
47
        return;
48
    }
49
50
    /**
51
     * @param AbstractPersonalTranslation $translation
52
     *
53
     * @return $this
54
     */
55
    public function addTranslation(AbstractPersonalTranslation $translation)
56
    {
57
        if (!$this->translations->contains($translation)) {
58
            $translation->setObject($this);
59
            $this->translations->add($translation);
60
        }
61
62
        return $this;
63
    }
64
}
65