1 | <?php |
||
9 | class TwitterDisconnectSerializer implements TwitterSerializer |
||
10 | { |
||
11 | /** |
||
12 | * @param TwitterSerializable $object |
||
13 | * @return \stdClass |
||
14 | */ |
||
15 | 6 | public function serialize(TwitterSerializable $object) |
|
31 | |||
32 | /** |
||
33 | * @param \stdClass $obj |
||
34 | * @param array $context |
||
35 | * @return TwitterDisconnect |
||
36 | */ |
||
37 | 3 | public function unserialize($obj, array $context = []) |
|
51 | 6 | ||
52 | /** |
||
53 | 6 | * @param TwitterSerializable $object |
|
54 | * @return boolean |
||
55 | */ |
||
56 | public function canSerialize(TwitterSerializable $object) |
||
60 | |||
61 | /** |
||
62 | * @param \stdClass $object |
||
63 | * @return boolean |
||
64 | */ |
||
65 | public function canUnserialize($object) |
||
69 | |||
70 | /** |
||
71 | * @return TwitterDisconnectSerializer |
||
72 | */ |
||
73 | public static function build() |
||
77 | } |
||
78 |
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: