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 | use Kdyby\StrictObjects\Scream; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var IUserLocaleResolver |
||
| 38 | */ |
||
| 39 | private $localeResolver; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var CatalogueCompiler |
||
| 43 | */ |
||
| 44 | private $catalogueCompiler; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var FallbackResolver |
||
| 48 | */ |
||
| 49 | private $fallbackResolver; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var IResourceLoader |
||
| 53 | */ |
||
| 54 | private $translationsLoader; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var LoggerInterface|NULL |
||
| 58 | */ |
||
| 59 | private $psrLogger; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Panel|NULL |
||
| 63 | */ |
||
| 64 | private $panel; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $availableResourceLocales = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $defaultLocale; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string|NULL |
||
| 78 | */ |
||
| 79 | private $localeWhitelist; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var MessageSelector |
||
| 83 | */ |
||
| 84 | private $selector; |
||
|
|
|||
| 85 | |||
| 86 | /** |
||
| 87 | * @param IUserLocaleResolver $localeResolver |
||
| 88 | * @param MessageSelector $selector The message selector for pluralization |
||
| 89 | * @param CatalogueCompiler $catalogueCompiler |
||
| 90 | * @param FallbackResolver $fallbackResolver |
||
| 91 | * @param IResourceLoader $loader |
||
| 92 | * @throws \InvalidArgumentException |
||
| 93 | */ |
||
| 94 | public function __construct( |
||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @internal |
||
| 116 | * @param Panel $panel |
||
| 117 | */ |
||
| 118 | public function injectPanel(Panel $panel) |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @param LoggerInterface|NULL $logger |
||
| 127 | */ |
||
| 128 | public function injectPsrLogger(LoggerInterface $logger = NULL) |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Translates the given string. |
||
| 137 | * |
||
| 138 | * @param string|\Kdyby\Translation\Phrase|mixed $message The message id |
||
| 139 | * @param int|array|NULL $count The number to use to find the indice of the message |
||
| 140 | * @param string|array|NULL $parameters An array of parameters for the message |
||
| 141 | * @param string|NULL $domain The domain for the message |
||
| 142 | * @param string|NULL $locale The locale |
||
| 143 | * @throws \InvalidArgumentException |
||
| 144 | * @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString |
||
| 145 | */ |
||
| 146 | public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function trans($message, array $parameters = [], $domain = NULL, $locale = NULL) |
||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function transChoice($message, $number, array $parameters = [], $domain = NULL, $locale = NULL) |
||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $format |
||
| 271 | * @param LoaderInterface $loader |
||
| 272 | */ |
||
| 273 | public function addLoader($format, LoaderInterface $loader) |
||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * @return \Symfony\Component\Translation\Loader\LoaderInterface[] |
||
| 283 | */ |
||
| 284 | protected function getLoaders() |
||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array $whitelist |
||
| 293 | * @return Translator |
||
| 294 | */ |
||
| 295 | public function setLocaleWhitelist(array $whitelist = NULL) |
||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function addResource($format, $resource, $locale, $domain = NULL) |
||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function setFallbackLocales(array $locales) |
||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns array of locales from given resources |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function getAvailableLocales() |
||
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Sets the current locale. |
||
| 352 | * |
||
| 353 | * @param string|NULL $locale The locale |
||
| 354 | * |
||
| 355 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
| 356 | */ |
||
| 357 | public function setLocale($locale) |
||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the current locale. |
||
| 366 | * |
||
| 367 | * @return string|NULL The locale |
||
| 368 | */ |
||
| 369 | public function getLocale() |
||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getDefaultLocale() |
||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $locale |
||
| 392 | * @return Translator |
||
| 393 | */ |
||
| 394 | public function setDefaultLocale($locale) |
||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $messagePrefix |
||
| 405 | * @return ITranslator |
||
| 406 | */ |
||
| 407 | public function domain($messagePrefix) |
||
| 411 | |||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @return TemplateHelpers |
||
| 416 | */ |
||
| 417 | public function createTemplateHelpers() |
||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritdoc} |
||
| 426 | */ |
||
| 427 | protected function loadCatalogue($locale) |
||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * {@inheritdoc} |
||
| 444 | */ |
||
| 445 | protected function computeFallbackLocales($locale) |
||
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Asserts that the locale is valid, throws an Exception if not. |
||
| 454 | * |
||
| 455 | * @param string $locale Locale to tests |
||
| 456 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
| 457 | */ |
||
| 458 | protected function assertValidLocale($locale) |
||
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $message |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | private function extractMessageDomain($message) |
||
| 482 | |||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string|NULL $message |
||
| 487 | * @param string|NULL $domain |
||
| 488 | * @param string|NULL $locale |
||
| 489 | */ |
||
| 490 | protected function logMissingTranslation($message, $domain, $locale) |
||
| 508 | |||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * @param array|NULL $whitelist |
||
| 513 | * @return null|string |
||
| 514 | */ |
||
| 515 | public static function buildWhitelistRegexp($whitelist) |
||
| 519 | |||
| 520 | } |
||
| 521 |