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 |
||
26 | final class FallbackTranslator implements TranslatorInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var LegacyTranslatorInterface|NewTranslatorInterface |
||
30 | */ |
||
31 | private $symfonyTranslator; |
||
32 | |||
33 | /** |
||
34 | * @var Translator |
||
35 | */ |
||
36 | private $externalTranslator; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $defaultLocale; |
||
42 | |||
43 | /** |
||
44 | * $symfonyTranslator param can't be type hinted as we have to deal with both LegacyTranslatorInterface & NewTranslatorInterface. |
||
45 | * Once we won't support sf ^3.4 anymore, we will be able to type hint $symfonyTranslator with NewTranslatorInterface. |
||
46 | * |
||
47 | * @param LegacyTranslatorInterface|NewTranslatorInterface $symfonyTranslator |
||
48 | */ |
||
49 | 2 | public function __construct(string $defaultLocale, $symfonyTranslator, Translator $externalTranslator) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | View Code Duplication | public function trans($id, array $parameters = [], $domain = null, $locale = null): string |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | View Code Duplication | public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): string |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function setLocale($locale): void |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function getLocale(): string |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function getCatalogue($locale = null): MessageCatalogueInterface |
||
139 | |||
140 | /** |
||
141 | * Passes through all unknown calls onto the translator object. |
||
142 | */ |
||
143 | public function __call(string $method, array $args) |
||
147 | |||
148 | /** |
||
149 | * @param string $orgString This is the string in the default locale. It has the values of $parameters in the string already. |
||
150 | * @param string $locale you wan to translate to |
||
151 | */ |
||
152 | 1 | private function translateWithSubstitutedParameters(string $orgString, string $locale, array $parameters): string |
|
170 | } |
||
171 |
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.