1 | <?php |
||
23 | final class UserImpersonatedListener |
||
24 | { |
||
25 | /** |
||
26 | * @var CartStorageInterface |
||
27 | */ |
||
28 | private $cartStorage; |
||
29 | |||
30 | /** |
||
31 | * @var ChannelContextInterface |
||
32 | */ |
||
33 | private $channelContext; |
||
34 | |||
35 | /** |
||
36 | * @var OrderRepositoryInterface |
||
37 | */ |
||
38 | private $orderRepository; |
||
39 | |||
40 | /** |
||
41 | * @param CartStorageInterface $cartStorage |
||
42 | * @param ChannelContextInterface $channelContext |
||
43 | * @param OrderRepositoryInterface $orderRepository |
||
44 | */ |
||
45 | public function __construct( |
||
54 | |||
55 | /** |
||
56 | * @param UserEvent $event |
||
57 | */ |
||
58 | public function onUserImpersonated(UserEvent $event): void |
||
76 | } |
||
77 |
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: