| 1 | <?php | ||
| 16 | class JsonEventSubscriber implements EventSubscriberInterface | ||
| 17 | { | ||
| 18 | /** | ||
| 19 | * @var RelationFactory | ||
| 20 | */ | ||
| 21 | private $relationFactory; | ||
| 22 | |||
| 23 | /** | ||
| 24 |      * {@inheritdoc} | ||
| 25 | */ | ||
| 26 | public static function getSubscribedEvents() | ||
| 27 |     { | ||
| 28 | return [ | ||
| 29 | [ | ||
| 30 | 'event' => Events::POST_SERIALIZE, | ||
| 31 | 'format' => 'json', | ||
| 32 | 'method' => 'onPostSerialize', | ||
| 33 | ], | ||
| 34 | ]; | ||
| 35 | } | ||
| 36 | |||
| 37 | /** | ||
| 38 | * JsonEventSubscriber constructor. | ||
| 39 | * | ||
| 40 | * @param RelationFactory $relationFactory | ||
| 41 | */ | ||
| 42 | public function __construct(RelationFactory $relationFactory) | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Add Hal Api Relations to serialized objects. | ||
| 49 | * | ||
| 50 | * @param ObjectEvent $event | ||
| 51 | */ | ||
| 52 | public function onPostSerialize(ObjectEvent $event) | ||
| 62 | } | ||
| 63 | 
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: