1 | <?php |
||
19 | class MissedBookingEmailNotificationHandler implements NotificationHandlerInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var MailServiceInterface |
||
24 | */ |
||
25 | protected $mailService; |
||
26 | |||
27 | /** |
||
28 | * @var ModuleOptions |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * @var Giphy |
||
34 | */ |
||
35 | protected $giphyApi; |
||
36 | |||
37 | /** |
||
38 | * @param MailServiceInterface $mailService |
||
39 | * @param ModuleOptions $options |
||
40 | * @param Giphy $giphyApi |
||
41 | */ |
||
42 | public function __construct(MailServiceInterface $mailService, ModuleOptions $options, Giphy $giphyApi) |
||
48 | |||
49 | /** |
||
50 | * @param NotificationInterface $notification |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function shouldHandle(NotificationInterface $notification) |
||
59 | |||
60 | /** |
||
61 | * @param NotificationInterface $notification |
||
62 | * @param UserInterface $user |
||
63 | */ |
||
64 | public function handle(NotificationInterface $notification, UserInterface $user) |
||
87 | } |
||
88 |
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: