1 | <?php |
||
23 | class NativeStateful extends Lexer implements Stateful |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $pattern; |
||
29 | |||
30 | /** |
||
31 | * NativeStateful constructor. |
||
32 | * @param string $pattern |
||
33 | * @param array $skipped |
||
34 | */ |
||
35 | 11 | public function __construct(string $pattern, array $skipped = []) |
|
40 | |||
41 | /** |
||
42 | * @param Readable $file |
||
43 | * @return \Traversable |
||
44 | * @throws \InvalidArgumentException |
||
45 | * @throws \RuntimeException |
||
46 | */ |
||
47 | 17 | protected function exec(Readable $file): \Traversable |
|
67 | |||
68 | /** |
||
69 | * @param \Traversable|\Generator $iterator |
||
70 | * @param int $offset |
||
71 | * @return Unknown |
||
72 | */ |
||
73 | 9 | private function unknown(\Traversable $iterator, int $offset): TokenInterface |
|
82 | |||
83 | /** |
||
84 | * @param \Traversable|\Iterator $iterator |
||
85 | * @param string $key |
||
86 | * @return string |
||
87 | */ |
||
88 | 9 | protected function reduce(\Traversable $iterator, string $key): string |
|
104 | |||
105 | /** |
||
106 | * @param \Traversable|\Iterator $iterator |
||
107 | * @param int $offset |
||
108 | * @return Token |
||
109 | */ |
||
110 | 17 | private function token(\Traversable $iterator, int $offset): TokenInterface |
|
118 | } |
||
119 |
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: