1 | <?php |
||
11 | class TwitterDirectMessageSerializer implements TwitterSerializer |
||
12 | { |
||
13 | /** |
||
14 | * @var TwitterUserSerializer |
||
15 | */ |
||
16 | private $userSerializer; |
||
17 | |||
18 | /** |
||
19 | * @var TwitterEntitiesSerializer |
||
20 | */ |
||
21 | private $twitterEntitiesSerializer; |
||
22 | |||
23 | /** |
||
24 | * @param TwitterUserSerializer $userSerializer |
||
25 | * @param TwitterEntitiesSerializer $twitterEntitiesSerializer |
||
26 | */ |
||
27 | 15 | public function __construct( |
|
34 | |||
35 | /** |
||
36 | * @param TwitterSerializable $object |
||
37 | * @return \stdClass |
||
38 | */ |
||
39 | 6 | public function serialize(TwitterSerializable $object) |
|
58 | |||
59 | /** |
||
60 | * @param \stdClass $directMessage |
||
61 | 3 | * @param array $context |
|
62 | * @return TwitterDirectMessage |
||
63 | 3 | */ |
|
64 | 3 | public function unserialize($directMessage, array $context = []) |
|
80 | 6 | ||
81 | 4 | /** |
|
82 | * @param TwitterSerializable $object |
||
83 | * @return boolean |
||
84 | */ |
||
85 | public function canSerialize(TwitterSerializable $object) |
||
89 | |||
90 | /** |
||
91 | * @param \stdClass $object |
||
92 | * @return boolean |
||
93 | */ |
||
94 | public function canUnserialize($object) |
||
98 | |||
99 | /** |
||
100 | * @return TwitterDirectMessageSerializer |
||
101 | */ |
||
102 | public static function build() |
||
109 | } |
||
110 |
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: