1 | <?php |
||
24 | final class FlashHelper implements FlashHelperInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var SessionInterface |
||
28 | */ |
||
29 | private $session; |
||
30 | |||
31 | /** |
||
32 | * @var TranslatorInterface |
||
33 | */ |
||
34 | private $translator; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $defaultLocale; |
||
40 | |||
41 | /** |
||
42 | * @param SessionInterface $session |
||
43 | * @param TranslatorInterface $translator |
||
44 | * @param string $defaultLocale |
||
45 | */ |
||
46 | public function __construct(SessionInterface $session, TranslatorInterface $translator, $defaultLocale) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function addSuccessFlash(RequestConfiguration $requestConfiguration, $actionName, ResourceInterface $resource = null) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function addFlashFromEvent(RequestConfiguration $requestConfiguration, ResourceControllerEvent $event) |
||
93 | |||
94 | /** |
||
95 | * @param string $type |
||
96 | * @param string $message |
||
97 | * @param array $parameters |
||
98 | */ |
||
99 | private function addFlash($type, $message, array $parameters = []) |
||
107 | |||
108 | /** |
||
109 | * @param string $message |
||
110 | * @param array $parameters |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | private function prepareMessage($message, array $parameters) |
||
121 | |||
122 | /** |
||
123 | * @param string $actionName |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | private function getResourceMessage($actionName) |
||
131 | |||
132 | /** |
||
133 | * @param string $message |
||
134 | * @param string $locale |
||
135 | * @param array $parameters |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | private function isTranslationDefined($message, $locale, array $parameters) |
||
149 | } |
||
150 |
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: