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 |
||
27 | View Code Duplication | abstract class AbstractPersonalTranslatable extends AbstractTranslatable |
|
|
|||
28 | { |
||
29 | /** |
||
30 | * @var ArrayCollection|AbstractPersonalTranslation[] |
||
31 | */ |
||
32 | protected $translations; |
||
33 | |||
34 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * @return ArrayCollection|AbstractPersonalTranslation[] |
||
41 | */ |
||
42 | public function getTranslations() |
||
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) |
||
76 | } |
||
77 |
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.