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:
Complex classes like Translator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Translator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Translator extends \Symfony\Component\Translation\Translator implements \Kdyby\Translation\ITranslator |
||
22 | { |
||
23 | |||
24 | use \Kdyby\StrictObjects\Scream; |
||
25 | |||
26 | /** |
||
27 | * @var \Kdyby\Translation\IUserLocaleResolver |
||
28 | */ |
||
29 | private $localeResolver; |
||
30 | |||
31 | /** |
||
32 | * @var \Kdyby\Translation\CatalogueCompiler |
||
33 | */ |
||
34 | private $catalogueCompiler; |
||
35 | |||
36 | /** |
||
37 | * @var \Kdyby\Translation\FallbackResolver |
||
38 | */ |
||
39 | private $fallbackResolver; |
||
40 | |||
41 | /** |
||
42 | * @var \Kdyby\Translation\IResourceLoader |
||
43 | */ |
||
44 | private $translationsLoader; |
||
45 | |||
46 | /** |
||
47 | * @var \Psr\Log\LoggerInterface|NULL |
||
48 | */ |
||
49 | private $psrLogger; |
||
50 | |||
51 | /** |
||
52 | * @var \Kdyby\Translation\Diagnostics\Panel|NULL |
||
53 | */ |
||
54 | private $panel; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $availableResourceLocales = []; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $defaultLocale; |
||
65 | |||
66 | /** |
||
67 | * @var string|NULL |
||
68 | */ |
||
69 | private $localeWhitelist; |
||
70 | |||
71 | /** |
||
72 | * @var \Symfony\Component\Translation\Formatter\MessageFormatter |
||
73 | */ |
||
74 | private $formatter; |
||
75 | |||
76 | /** |
||
77 | * @param \Kdyby\Translation\IUserLocaleResolver $localeResolver |
||
78 | * @param \Symfony\Component\Translation\Formatter\MessageFormatter $formatter |
||
79 | * @param \Kdyby\Translation\CatalogueCompiler $catalogueCompiler |
||
80 | * @param \Kdyby\Translation\FallbackResolver $fallbackResolver |
||
81 | * @param \Kdyby\Translation\IResourceLoader $loader |
||
82 | * @throws \InvalidArgumentException |
||
83 | */ |
||
84 | public function __construct( |
||
101 | |||
102 | /** |
||
103 | * @internal |
||
104 | * @param \Kdyby\Translation\Diagnostics\Panel $panel |
||
105 | */ |
||
106 | public function injectPanel(Panel $panel) |
||
110 | |||
111 | /** |
||
112 | * @param \Psr\Log\LoggerInterface|NULL $logger |
||
113 | */ |
||
114 | public function injectPsrLogger(LoggerInterface $logger = NULL) |
||
118 | |||
119 | /** |
||
120 | * Translates the given string. |
||
121 | * |
||
122 | * @param string|\Kdyby\Translation\Phrase|mixed $message The message id |
||
123 | * @param int|array|NULL $count The number to use to find the indice of the message |
||
124 | * @param string|array|NULL $parameters An array of parameters for the message |
||
125 | * @param string|NULL $domain The domain for the message |
||
126 | * @param string|NULL $locale The locale |
||
127 | * @throws \InvalidArgumentException |
||
128 | * @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString |
||
129 | */ |
||
130 | public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function trans($message, array $parameters = [], $domain = NULL, $locale = NULL) |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function transChoice($message, $number, array $parameters = [], $domain = NULL, $locale = NULL) |
||
256 | |||
257 | /** |
||
258 | * @param string $format |
||
259 | * @param \Symfony\Component\Translation\Loader\LoaderInterface $loader |
||
260 | */ |
||
261 | public function addLoader($format, LoaderInterface $loader) |
||
266 | |||
267 | /** |
||
268 | * @return \Symfony\Component\Translation\Loader\LoaderInterface[] |
||
269 | */ |
||
270 | protected function getLoaders() |
||
274 | |||
275 | /** |
||
276 | * @param array $whitelist |
||
277 | */ |
||
278 | public function setLocaleWhitelist(array $whitelist = NULL) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function addResource($format, $resource, $locale, $domain = NULL) |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function setFallbackLocales(array $locales) |
||
312 | |||
313 | /** |
||
314 | * Returns array of locales from given resources |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | public function getAvailableLocales() |
||
324 | |||
325 | /** |
||
326 | * Sets the current locale. |
||
327 | * |
||
328 | * @param string|NULL $locale The locale |
||
329 | * |
||
330 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
331 | */ |
||
332 | public function setLocale($locale) |
||
336 | |||
337 | /** |
||
338 | * Returns the current locale. |
||
339 | * |
||
340 | * @return string|NULL The locale |
||
341 | */ |
||
342 | public function getLocale() |
||
350 | |||
351 | /** |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getDefaultLocale() |
||
358 | |||
359 | /** |
||
360 | * @param string $locale |
||
361 | * @return \Kdyby\Translation\Translator |
||
362 | */ |
||
363 | public function setDefaultLocale($locale) |
||
369 | |||
370 | /** |
||
371 | * @param string $messagePrefix |
||
372 | * @return \Kdyby\Translation\ITranslator |
||
373 | */ |
||
374 | public function domain($messagePrefix) |
||
378 | |||
379 | /** |
||
380 | * @return \Kdyby\Translation\TemplateHelpers |
||
381 | */ |
||
382 | public function createTemplateHelpers() |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | protected function loadCatalogue($locale) |
||
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | protected function computeFallbackLocales($locale) |
||
410 | |||
411 | /** |
||
412 | * Asserts that the locale is valid, throws an Exception if not. |
||
413 | * |
||
414 | * @param string $locale Locale to tests |
||
415 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
416 | */ |
||
417 | protected function assertValidLocale($locale) |
||
423 | |||
424 | /** |
||
425 | * @param string $message |
||
426 | * @return array |
||
427 | */ |
||
428 | private function extractMessageDomain($message) |
||
439 | |||
440 | /** |
||
441 | * @param string|NULL $message |
||
442 | * @param string|NULL $domain |
||
443 | * @param string|NULL $locale |
||
444 | */ |
||
445 | protected function logMissingTranslation($message, $domain, $locale) |
||
463 | |||
464 | /** |
||
465 | * @param array|NULL $whitelist |
||
466 | * @return null|string |
||
467 | */ |
||
468 | public static function buildWhitelistRegexp($whitelist) |
||
472 | |||
473 | } |
||
474 |
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.