1 | <?php |
||
22 | class NativeStateful implements Stateful |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $pattern; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $skipped; |
||
33 | |||
34 | /** |
||
35 | * NativeStateful constructor. |
||
36 | * @param string $pattern |
||
37 | * @param array $skipped |
||
38 | */ |
||
39 | 54 | public function __construct(string $pattern, array $skipped = []) |
|
44 | |||
45 | /** |
||
46 | * @param Readable $input |
||
47 | * @return \Traversable |
||
48 | */ |
||
49 | 54 | public function lex(Readable $input): \Traversable |
|
57 | |||
58 | /** |
||
59 | * @param string $pattern |
||
60 | * @param string $content |
||
61 | * @return \Traversable|TokenInterface[] |
||
62 | */ |
||
63 | 54 | protected function exec(string $pattern, string $content): \Traversable |
|
83 | |||
84 | /** |
||
85 | * @param \Traversable $iterator |
||
86 | * @param int $offset |
||
87 | * @return Unknown |
||
88 | */ |
||
89 | private function unknown(\Traversable $iterator, int $offset): TokenInterface |
||
98 | |||
99 | /** |
||
100 | * @param \Traversable $iterator |
||
101 | * @return string |
||
102 | */ |
||
103 | private function collapse(\Traversable $iterator, string $token): string |
||
119 | |||
120 | /** |
||
121 | * @param \Traversable $iterator |
||
122 | * @param int $offset |
||
123 | * @return Token |
||
124 | */ |
||
125 | 54 | private function token(\Traversable $iterator, int $offset) |
|
133 | } |
||
134 |
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: