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 |
||
18 | class Translator extends SymfonyTranslator |
||
19 | { |
||
20 | /** |
||
21 | * The locales manager. |
||
22 | * |
||
23 | * @var LocalesManager |
||
24 | */ |
||
25 | private $manager; |
||
26 | |||
27 | /** |
||
28 | * The loaded domains. |
||
29 | * |
||
30 | * @var string[] |
||
31 | */ |
||
32 | private $domains = [ 'messages' ]; |
||
33 | |||
34 | /** |
||
35 | * @param array $data Constructor data. |
||
36 | */ |
||
37 | public function __construct(array $data) |
||
61 | |||
62 | /** |
||
63 | * Adds a resource. |
||
64 | * |
||
65 | * @see SymfonyTranslator::addResource() Keep track of the translation domains. |
||
66 | * @param string $format The name of the loader (@see addLoader()). |
||
67 | * @param mixed $resource The resource name. |
||
68 | * @param string $locale The locale. |
||
69 | * @param string|null $domain The domain. |
||
70 | * @return void |
||
71 | */ |
||
72 | public function addResource($format, $resource, $locale, $domain = null) |
||
80 | |||
81 | /** |
||
82 | * Retrieve the loaded domains. |
||
83 | * |
||
84 | * @return string[] |
||
85 | */ |
||
86 | public function availableDomains() |
||
90 | |||
91 | /** |
||
92 | * Retrieve a translation object from a (mixed) message. |
||
93 | * |
||
94 | * @uses SymfonyTranslator::trans() |
||
95 | * @param mixed $val The string or translation-object to retrieve. |
||
96 | * @param array $parameters An array of parameters for the message. |
||
97 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
98 | * @return Translation|null The translation object or NULL if the value is not translatable. |
||
99 | */ |
||
100 | View Code Duplication | public function translation($val, array $parameters = [], $domain = null) |
|
115 | |||
116 | /** |
||
117 | * Translates the given (mixed) message. |
||
118 | * |
||
119 | * @uses SymfonyTranslator::trans() |
||
120 | * @uses Translator::translation() |
||
121 | * @param mixed $val The string or translation-object to retrieve. |
||
122 | * @param array $parameters An array of parameters for the message. |
||
123 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
124 | * @param string|null $locale The locale or NULL to use the default. |
||
125 | * @return string The translated string |
||
126 | */ |
||
127 | View Code Duplication | public function translate($val, array $parameters = [], $domain = null, $locale = null) |
|
144 | |||
145 | /** |
||
146 | * Retrieve a translation object from a (mixed) message by choosing a translation according to a number. |
||
147 | * |
||
148 | * @uses SymfonyTranslator::transChoice() |
||
149 | * @param mixed $val The string or translation-object to retrieve. |
||
150 | * @param integer $number The number to use to find the indice of the message. |
||
151 | * @param array $parameters An array of parameters for the message. |
||
152 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
153 | * @return Translation|null The translation object or NULL if the value is not translatable. |
||
154 | */ |
||
155 | View Code Duplication | public function translationChoice($val, $number, array $parameters = [], $domain = null) |
|
170 | |||
171 | /** |
||
172 | * Translates the given (mixed) choice message by choosing a translation according to a number. |
||
173 | * |
||
174 | * @uses SymfonyTranslator::transChoice() |
||
175 | * @uses Translator::translationChoice() |
||
176 | * @param mixed $val The string or translation-object to retrieve. |
||
177 | * @param integer $number The number to use to find the indice of the message. |
||
178 | * @param array $parameters An array of parameters for the message. |
||
179 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
180 | * @param string|null $locale The locale or NULL to use the default. |
||
181 | * @return string The translated string |
||
182 | */ |
||
183 | View Code Duplication | public function translateChoice($val, $number, array $parameters = [], $domain = null, $locale = null) |
|
200 | |||
201 | /** |
||
202 | * Retrieve the available locales information. |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function locales() |
||
210 | |||
211 | /** |
||
212 | * Retrieve the available locales (language codes). |
||
213 | * |
||
214 | * @return string[] |
||
215 | */ |
||
216 | public function availableLocales() |
||
220 | |||
221 | /** |
||
222 | * Sets the current locale. |
||
223 | * |
||
224 | * @see SymfonyTranslator::setLocale() Ensure that the method also changes the locales manager's language. |
||
225 | * @param string $locale The locale. |
||
226 | * @return void |
||
227 | */ |
||
228 | public function setLocale($locale) |
||
234 | |||
235 | /** |
||
236 | * Set the locales manager. |
||
237 | * |
||
238 | * @param LocalesManager $manager The locales manager. |
||
239 | * @return void |
||
240 | */ |
||
241 | private function setManager(LocalesManager $manager) |
||
245 | |||
246 | /** |
||
247 | * Determine if the value is translatable. |
||
248 | * |
||
249 | * @param mixed $val The value to be checked. |
||
250 | * @return boolean |
||
251 | */ |
||
252 | private function isValidTranslation($val) |
||
283 | } |
||
284 |
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.