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

AbstractPersonalTranslatable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 8
c 3
b 2
f 0
lcom 1
cbo 3
dl 55
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
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\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