1 | <?php |
||
16 | class TranslatorFactory |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * factory method, creates a translator |
||
21 | * |
||
22 | * @param TranslatorInterface $translator translator |
||
23 | * @param I18nCacheUtils $cacheUtils cache utils |
||
24 | * |
||
25 | * @return TranslatorInterface translator |
||
26 | */ |
||
27 | public static function createTranslator(TranslatorInterface $translator, I18nCacheUtils $cacheUtils) |
||
31 | |||
32 | /** |
||
33 | * as we cannot add resources files directly (the function from where i lifted this is private), |
||
34 | * we copied this here and add it as Translator would (and does) |
||
35 | * |
||
36 | * @param TranslatorInterface $translator translator |
||
37 | * @param array $resources added resources |
||
38 | * |
||
39 | * @return TranslatorInterface translator |
||
40 | */ |
||
41 | private static function addResourceFiles(TranslatorInterface $translator, array $resources) |
||
53 | } |
||
54 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: