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 |
||
31 | class Translator extends BaseTranslator implements ITranslator |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * @var IUserLocaleResolver |
||
36 | */ |
||
37 | private $localeResolver; |
||
38 | |||
39 | /** |
||
40 | * @var CatalogueCompiler |
||
41 | */ |
||
42 | private $catalogueCompiler; |
||
43 | |||
44 | /** |
||
45 | * @var FallbackResolver |
||
46 | */ |
||
47 | private $fallbackResolver; |
||
48 | |||
49 | /** |
||
50 | * @var IResourceLoader |
||
51 | */ |
||
52 | private $translationsLoader; |
||
53 | |||
54 | /** |
||
55 | * @var LoggerInterface |
||
56 | */ |
||
57 | private $psrLogger; |
||
58 | |||
59 | /** |
||
60 | * @var Panel |
||
61 | */ |
||
62 | private $panel; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $availableResourceLocales = []; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $defaultLocale; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $localeWhitelist; |
||
78 | |||
79 | /** |
||
80 | * @var MessageSelector |
||
81 | */ |
||
82 | private $selector; |
||
83 | |||
84 | /** |
||
85 | * @param IUserLocaleResolver $localeResolver |
||
86 | * @param MessageSelector $selector The message selector for pluralization |
||
87 | * @param CatalogueCompiler $catalogueCompiler |
||
88 | * @param FallbackResolver $fallbackResolver |
||
89 | * @param IResourceLoader $loader |
||
90 | */ |
||
91 | public function __construct(IUserLocaleResolver $localeResolver, MessageSelector $selector, |
||
102 | |||
103 | |||
104 | |||
105 | /** |
||
106 | * @internal |
||
107 | * @param Panel $panel |
||
108 | */ |
||
109 | public function injectPanel(Panel $panel) |
||
113 | |||
114 | |||
115 | |||
116 | /** |
||
117 | * @param LoggerInterface|NULL $logger |
||
118 | */ |
||
119 | public function injectPsrLogger(LoggerInterface $logger = NULL) |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * Translates the given string. |
||
128 | * |
||
129 | * @param string $message The message id |
||
130 | * @param integer $count The number to use to find the indice of the message |
||
131 | * @param array $parameters An array of parameters for the message |
||
132 | * @param string $domain The domain for the message |
||
133 | * @param string $locale The locale |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function trans($message, array $parameters = [], $domain = NULL, $locale = NULL) |
||
203 | |||
204 | |||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function transChoice($message, $number, array $parameters = [], $domain = NULL, $locale = NULL) |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * @param string $format |
||
247 | * @param LoaderInterface $loader |
||
248 | */ |
||
249 | public function addLoader($format, LoaderInterface $loader) |
||
254 | |||
255 | |||
256 | |||
257 | /** |
||
258 | * @return \Symfony\Component\Translation\Loader\LoaderInterface[] |
||
259 | */ |
||
260 | protected function getLoaders() |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * @param array $whitelist |
||
269 | * @return Translator |
||
270 | */ |
||
271 | public function setLocaleWhitelist(array $whitelist = NULL) |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function addResource($format, $resource, $locale, $domain = NULL) |
||
298 | |||
299 | |||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function setFallbackLocales(array $locales) |
||
309 | |||
310 | |||
311 | |||
312 | /** |
||
313 | * Returns array of locales from given resources |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function getAvailableLocales() |
||
323 | |||
324 | |||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | public function getLocale() |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getDefaultLocale() |
||
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * @param string $locale |
||
352 | * @return Translator |
||
353 | */ |
||
354 | public function setDefaultLocale($locale) |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * @param string $messagePrefix |
||
365 | * @return ITranslator |
||
366 | */ |
||
367 | public function domain($messagePrefix) |
||
371 | |||
372 | |||
373 | |||
374 | /** |
||
375 | * @return TemplateHelpers |
||
376 | */ |
||
377 | public function createTemplateHelpers() |
||
381 | |||
382 | |||
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | protected function loadCatalogue($locale) |
||
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | protected function computeFallbackLocales($locale) |
||
409 | |||
410 | |||
411 | |||
412 | /** |
||
413 | * Asserts that the locale is valid, throws an Exception if not. |
||
414 | * |
||
415 | * @param string $locale Locale to tests |
||
416 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
417 | */ |
||
418 | protected function assertValidLocale($locale) |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * @param string $message |
||
429 | * @return array |
||
430 | */ |
||
431 | private function extractMessageDomain($message) |
||
442 | |||
443 | |||
444 | |||
445 | /** |
||
446 | * @param string $message |
||
447 | * @param string $domain |
||
448 | * @param string $locale |
||
449 | */ |
||
450 | protected function logMissingTranslation($message, $domain, $locale) |
||
464 | |||
465 | |||
466 | |||
467 | /** |
||
468 | * @param array $whitelist |
||
469 | * @return null|string |
||
470 | */ |
||
471 | public static function buildWhitelistRegexp($whitelist) |
||
475 | |||
476 | |||
477 | |||
478 | /*************************** Nette\Object ***************************/ |
||
479 | |||
480 | |||
481 | |||
482 | /** |
||
483 | * Access to reflection. |
||
484 | * @return \Nette\Reflection\ClassType |
||
485 | */ |
||
486 | public static function getReflection() |
||
490 | |||
491 | |||
492 | |||
493 | /** |
||
494 | * Call to undefined method. |
||
495 | * |
||
496 | * @param string $name |
||
497 | * @param array $args |
||
498 | * |
||
499 | * @throws \Nette\MemberAccessException |
||
500 | * @return mixed |
||
501 | */ |
||
502 | public function __call($name, $args) |
||
506 | |||
507 | |||
508 | |||
509 | /** |
||
510 | * Call to undefined static method. |
||
511 | * |
||
512 | * @param string $name |
||
513 | * @param array $args |
||
514 | * |
||
515 | * @throws \Nette\MemberAccessException |
||
516 | * @return mixed |
||
517 | */ |
||
518 | public static function __callStatic($name, $args) |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * Adding method to class. |
||
527 | * |
||
528 | * @param $name |
||
529 | * @param null $callback |
||
530 | * |
||
531 | * @throws \Nette\MemberAccessException |
||
532 | * @return callable|null |
||
533 | */ |
||
534 | public static function extensionMethod($name, $callback = NULL) |
||
547 | |||
548 | |||
549 | |||
550 | /** |
||
551 | * Returns property value. Do not call directly. |
||
552 | * |
||
553 | * @param string $name |
||
554 | * |
||
555 | * @throws \Nette\MemberAccessException |
||
556 | * @return mixed |
||
557 | */ |
||
558 | public function &__get($name) |
||
562 | |||
563 | |||
564 | |||
565 | /** |
||
566 | * Sets value of a property. Do not call directly. |
||
567 | * |
||
568 | * @param string $name |
||
569 | * @param mixed $value |
||
570 | * |
||
571 | * @throws \Nette\MemberAccessException |
||
572 | * @return void |
||
573 | */ |
||
574 | public function __set($name, $value) |
||
578 | |||
579 | |||
580 | |||
581 | /** |
||
582 | * Is property defined? |
||
583 | * |
||
584 | * @param string $name |
||
585 | * |
||
586 | * @return bool |
||
587 | */ |
||
588 | public function __isset($name) |
||
592 | |||
593 | |||
594 | |||
595 | /** |
||
596 | * Access to undeclared property. |
||
597 | * |
||
598 | * @param string $name |
||
599 | * |
||
600 | * @throws \Nette\MemberAccessException |
||
601 | * @return void |
||
602 | */ |
||
603 | public function __unset($name) |
||
607 | |||
608 | } |
||
609 |
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.