1 | <?php |
||
23 | final class TranslationExtension extends \Twig_Extension |
||
24 | { |
||
25 | /** |
||
26 | * @var TranslatorInterface |
||
27 | */ |
||
28 | private $translator; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $debug; |
||
34 | |||
35 | /** |
||
36 | * @param TranslatorInterface $translator |
||
37 | * @param bool $debug |
||
38 | */ |
||
39 | 11 | public function __construct(TranslatorInterface $translator, $debug = false) |
|
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | 8 | public function getFilters() |
|
55 | |||
56 | /** |
||
57 | * @return array |
||
58 | */ |
||
59 | 8 | public function getNodeVisitors() |
|
72 | |||
73 | /** |
||
74 | * @param string $message |
||
75 | * @param string $defaultMessage |
||
76 | * @param int $count |
||
77 | * @param array $arguments |
||
78 | * @param null|string $domain |
||
79 | * @param null|string $locale |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = [], $domain = null, $locale = null) |
||
95 | |||
96 | /** |
||
97 | * @param $v |
||
98 | * |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function desc($v) |
||
105 | |||
106 | /** |
||
107 | * @param $v |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function meaning($v) |
||
115 | |||
116 | public function getName() |
||
120 | } |
||
121 |
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: