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 |
||
22 | class Translator extends \Symfony\Component\Translation\Translator implements \Kdyby\Translation\ITranslator |
||
23 | { |
||
24 | |||
25 | use \Kdyby\StrictObjects\Scream; |
||
26 | |||
27 | /** |
||
28 | * @var \Kdyby\Translation\IUserLocaleResolver |
||
29 | */ |
||
30 | private $localeResolver; |
||
31 | |||
32 | /** |
||
33 | * @var \Kdyby\Translation\CatalogueCompiler |
||
34 | */ |
||
35 | private $catalogueCompiler; |
||
36 | |||
37 | /** |
||
38 | * @var \Kdyby\Translation\FallbackResolver |
||
39 | */ |
||
40 | private $fallbackResolver; |
||
41 | |||
42 | /** |
||
43 | * @var \Kdyby\Translation\IResourceLoader |
||
44 | */ |
||
45 | private $translationsLoader; |
||
46 | |||
47 | /** |
||
48 | * @var \Psr\Log\LoggerInterface|NULL |
||
49 | */ |
||
50 | private $psrLogger; |
||
51 | |||
52 | /** |
||
53 | * @var \Kdyby\Translation\Diagnostics\Panel|NULL |
||
54 | */ |
||
55 | private $panel; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $availableResourceLocales = []; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $defaultLocale; |
||
66 | |||
67 | /** |
||
68 | * @var string|NULL |
||
69 | */ |
||
70 | private $localeWhitelist; |
||
71 | |||
72 | /** |
||
73 | * @var \Symfony\Component\Translation\Formatter\MessageFormatterInterface |
||
74 | */ |
||
75 | private $formatter; |
||
76 | |||
77 | /** |
||
78 | * @param \Kdyby\Translation\IUserLocaleResolver $localeResolver |
||
79 | * @param \Symfony\Component\Translation\Formatter\MessageFormatterInterface $formatter |
||
80 | * @param \Kdyby\Translation\CatalogueCompiler $catalogueCompiler |
||
81 | * @param \Kdyby\Translation\FallbackResolver $fallbackResolver |
||
82 | * @param \Kdyby\Translation\IResourceLoader $loader |
||
83 | * @throws \InvalidArgumentException |
||
84 | */ |
||
85 | public function __construct( |
||
102 | |||
103 | /** |
||
104 | * @internal |
||
105 | * @param \Kdyby\Translation\Diagnostics\Panel $panel |
||
106 | */ |
||
107 | public function injectPanel(Panel $panel) |
||
111 | |||
112 | /** |
||
113 | * @param \Psr\Log\LoggerInterface|NULL $logger |
||
114 | */ |
||
115 | public function injectPsrLogger(LoggerInterface $logger = NULL) |
||
119 | |||
120 | /** |
||
121 | * Translates the given string. |
||
122 | * |
||
123 | * @param string|\Kdyby\Translation\Phrase|mixed $message The message id |
||
124 | * @param mixed ...$arg An array of parameters for the message |
||
125 | * @throws \InvalidArgumentException |
||
126 | * @return string |
||
127 | */ |
||
128 | public function translate($message, ...$arg): string |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function trans($message, array $parameters = [], $domain = NULL, $locale = NULL) |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function transChoice($message, $number, array $parameters = [], $domain = NULL, $locale = NULL) |
||
259 | |||
260 | /** |
||
261 | * @param string $format |
||
262 | * @param \Symfony\Component\Translation\Loader\LoaderInterface $loader |
||
263 | */ |
||
264 | public function addLoader($format, LoaderInterface $loader) |
||
269 | |||
270 | /** |
||
271 | * @return \Symfony\Component\Translation\Loader\LoaderInterface[] |
||
272 | */ |
||
273 | protected function getLoaders() |
||
277 | |||
278 | /** |
||
279 | * @param array $whitelist |
||
280 | */ |
||
281 | public function setLocaleWhitelist(array $whitelist = NULL) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function addResource($format, $resource, $locale, $domain = NULL) |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function setFallbackLocales(array $locales) |
||
315 | |||
316 | /** |
||
317 | * Returns array of locales from given resources |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public function getAvailableLocales() |
||
327 | |||
328 | /** |
||
329 | * Sets the current locale. |
||
330 | * |
||
331 | * @param string|NULL $locale The locale |
||
332 | * |
||
333 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
334 | */ |
||
335 | public function setLocale($locale) |
||
339 | |||
340 | /** |
||
341 | * Returns the current locale. |
||
342 | * |
||
343 | * @return string|NULL The locale |
||
344 | */ |
||
345 | public function getLocale() |
||
353 | |||
354 | /** |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getDefaultLocale() |
||
361 | |||
362 | /** |
||
363 | * @param string $locale |
||
364 | * @return \Kdyby\Translation\Translator |
||
365 | */ |
||
366 | public function setDefaultLocale($locale) |
||
372 | |||
373 | /** |
||
374 | * @param string $messagePrefix |
||
375 | * @return \Kdyby\Translation\ITranslator |
||
376 | */ |
||
377 | public function domain($messagePrefix) |
||
381 | |||
382 | /** |
||
383 | * @return \Kdyby\Translation\TemplateHelpers |
||
384 | */ |
||
385 | public function createTemplateHelpers() |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | protected function loadCatalogue($locale) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | protected function computeFallbackLocales($locale) |
||
413 | |||
414 | /** |
||
415 | * Asserts that the locale is valid, throws an Exception if not. |
||
416 | * |
||
417 | * @param string $locale Locale to tests |
||
418 | * @throws \InvalidArgumentException If the locale contains invalid characters |
||
419 | */ |
||
420 | protected function assertValidLocale($locale) |
||
426 | |||
427 | /** |
||
428 | * @param string $message |
||
429 | * @return array |
||
430 | */ |
||
431 | private function extractMessageDomain($message) |
||
442 | |||
443 | /** |
||
444 | * @param string|NULL $message |
||
445 | * @param string|NULL $domain |
||
446 | * @param string|NULL $locale |
||
447 | */ |
||
448 | protected function logMissingTranslation($message, $domain, $locale) |
||
466 | |||
467 | /** |
||
468 | * @param array|NULL $whitelist |
||
469 | * @return null|string |
||
470 | */ |
||
471 | public static function buildWhitelistRegexp($whitelist) |
||
475 | |||
476 | } |
||
477 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.