1 | <?php |
||
8 | abstract class BaseFormatter |
||
9 | { |
||
10 | protected $twig; |
||
11 | protected $eventDispatcher; |
||
12 | |||
13 | public function setTemplating(\Twig_Environment $twig) |
||
17 | |||
18 | public function setDispatcher(EventDispatcherInterface $eventDispatcher) |
||
22 | |||
23 | protected function renderTwigTemplate($data, $user, $template) |
||
33 | |||
34 | public function format(NotificationInterface $notification) |
||
47 | } |
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: