These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Traits\Gedmo; |
||
15 | |||
16 | use Sonata\TranslationBundle\Model\Gedmo\AbstractPersonalTranslation; |
||
17 | |||
18 | /** |
||
19 | * If you don't want to use trait, you can extend AbstractPersonalTranslatable instead. |
||
20 | * |
||
21 | * @author Nicolas Bastien <[email protected]> |
||
22 | */ |
||
23 | View Code Duplication | trait PersonalTranslatableTrait |
|
0 ignored issues
–
show
|
|||
24 | { |
||
25 | use TranslatableTrait; |
||
26 | |||
27 | /** |
||
28 | * @return ArrayCollection|AbstractPersonalTranslation[] |
||
29 | */ |
||
30 | public function getTranslations() |
||
31 | { |
||
32 | return $this->translations; |
||
0 ignored issues
–
show
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...
|
|||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param $field |
||
37 | * @param $locale |
||
38 | * |
||
39 | * @return string|null |
||
40 | */ |
||
41 | public function getTranslation($field, $locale) |
||
42 | { |
||
43 | foreach ($this->getTranslations() as $translation) { |
||
44 | if (0 === strcmp($translation->getField(), $field) && 0 === strcmp($translation->getLocale(), $locale)) { |
||
45 | return $translation->getContent(); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return null; |
||
50 | } |
||
51 | |||
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 |
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.