1 | <?php |
||
28 | final class KernelAwareInitializer implements ContextInitializer, EventSubscriberInterface |
||
29 | { |
||
30 | private $kernel; |
||
31 | |||
32 | /** |
||
33 | * Initializes initializer. |
||
34 | * |
||
35 | * @param KernelInterface $kernel |
||
36 | */ |
||
37 | public function __construct(KernelInterface $kernel) |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public static function getSubscribedEvents() |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function initializeContext(Context $context) |
||
64 | |||
65 | /** |
||
66 | * Reboots HttpKernel after each scenario. |
||
67 | */ |
||
68 | public function rebootKernel() |
||
73 | |||
74 | /** |
||
75 | * Checks whether the context uses the KernelDictionary trait. |
||
76 | * |
||
77 | * @param Context $context |
||
78 | * |
||
79 | * @return boolean |
||
80 | */ |
||
81 | private function usesKernelDictionary(Context $context) |
||
92 | } |
||
93 |
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: