1 | <?php |
||
9 | class TwitterUserSerializer implements TwitterSerializer |
||
10 | { |
||
11 | /** |
||
12 | * Serialize a twitter user |
||
13 | * |
||
14 | * @param TwitterSerializable $object |
||
15 | * @return \stdClass |
||
16 | */ |
||
17 | 6 | public function serialize(TwitterSerializable $object) |
|
34 | |||
35 | /** |
||
36 | * Unserialize a twitter user |
||
37 | * |
||
38 | * @param \stdClass $obj |
||
39 | * @param array $context |
||
40 | * @return TwitterUser |
||
41 | */ |
||
42 | 3 | public function unserialize($obj, array $context = []) |
|
58 | 18 | ||
59 | /** |
||
60 | 18 | * @param TwitterSerializable $object |
|
61 | * @return boolean |
||
62 | */ |
||
63 | public function canSerialize(TwitterSerializable $object) |
||
67 | |||
68 | /** |
||
69 | * @param \stdClass $object |
||
70 | * @return boolean |
||
71 | */ |
||
72 | public function canUnserialize($object) |
||
79 | |||
80 | /** |
||
81 | * @return TwitterUserSerializer |
||
82 | */ |
||
83 | public static function build() |
||
87 | } |
||
88 |
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: