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 |
||
| 9 | class Translator extends BaseTranslator |
||
| 10 | { |
||
| 11 | protected $container; |
||
| 12 | protected $options = [ |
||
| 13 | 'cache_dir' => 'test', |
||
| 14 | 'debug' => true, |
||
| 15 | 'resource_files' => [], |
||
| 16 | ]; |
||
| 17 | protected $loaderIds; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var MessageSelector |
||
| 21 | */ |
||
| 22 | private $selector; |
||
|
|
|||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | * |
||
| 27 | * @api |
||
| 28 | */ |
||
| 29 | public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = [], array $options = []) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | * |
||
| 39 | * @api |
||
| 40 | */ |
||
| 41 | public function trans($id, array $parameters = [], $domain = null, $locale = null) |
||
| 42 | { |
||
| 43 | if (null === $locale) { |
||
| 44 | $locale = $this->getLocale(); |
||
| 45 | } |
||
| 46 | View Code Duplication | if (null === $domain) { |
|
| 47 | $domain = 'messages'; |
||
| 48 | } elseif ('victoire' === $domain) { |
||
| 49 | $locale = $this->getVictoireLocale(); |
||
| 50 | } |
||
| 51 | if (!isset($this->catalogues[$locale])) { |
||
| 52 | $this->loadCatalogue($locale); |
||
| 53 | } |
||
| 54 | |||
| 55 | return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | * |
||
| 61 | * @api |
||
| 62 | */ |
||
| 63 | public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
||
| 64 | { |
||
| 65 | if (null === $locale) { |
||
| 66 | $locale = $this->getLocale(); |
||
| 67 | } |
||
| 68 | View Code Duplication | if (null === $domain) { |
|
| 69 | $domain = 'messages'; |
||
| 70 | } elseif ('victoire' === $domain) { |
||
| 71 | $locale = $this->getVictoireLocale(); |
||
| 72 | } |
||
| 73 | if (!isset($this->catalogues[$locale])) { |
||
| 74 | $this->loadCatalogue($locale); |
||
| 75 | } |
||
| 76 | $id = (string) $id; |
||
| 77 | $catalogue = $this->catalogues[$locale]; |
||
| 78 | while (!$catalogue->defines($id, $domain)) { |
||
| 79 | if ($cat = $catalogue->getFallbackCatalogue()) { |
||
| 80 | $catalogue = $cat; |
||
| 81 | $locale = $catalogue->getLocale(); |
||
| 82 | } else { |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * get the local in the session. |
||
| 92 | */ |
||
| 93 | public function getLocale() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * get the locale of the administration template. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getVictoireLocale() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return mixed|string |
||
| 114 | */ |
||
| 115 | public function getCurrentLocale() |
||
| 123 | } |
||
| 124 |