1 | <?php declare(strict_types=1); |
||
10 | class LoopDecorator implements LoopInterface, EventEmitterInterface |
||
11 | { |
||
12 | use EventEmitterTrait; |
||
13 | |||
14 | /** |
||
15 | * @var LoopInterface |
||
16 | */ |
||
17 | protected $loop; |
||
18 | |||
19 | /** |
||
20 | * @param LoopInterface $loop |
||
21 | */ |
||
22 | 24 | public function __construct(LoopInterface $loop) |
|
26 | |||
27 | /** |
||
28 | * Register a listener to be notified when a stream is ready to read. |
||
29 | * |
||
30 | * @param stream $stream The PHP stream resource to check. |
||
31 | * @param callable $listener Invoked when the stream is ready. |
||
32 | */ |
||
33 | 3 | public function addReadStream($stream, callable $listener) |
|
41 | |||
42 | /** |
||
43 | * Register a listener to be notified when a stream is ready to write. |
||
44 | * |
||
45 | * @param stream $stream The PHP stream resource to check. |
||
46 | * @param callable $listener Invoked when the stream is ready. |
||
47 | */ |
||
48 | 3 | public function addWriteStream($stream, callable $listener) |
|
56 | |||
57 | /** |
||
58 | * Remove the read event listener for the given stream. |
||
59 | * |
||
60 | * @param stream $stream The PHP stream resource. |
||
61 | */ |
||
62 | 3 | public function removeReadStream($stream) |
|
67 | |||
68 | /** |
||
69 | * Remove the write event listener for the given stream. |
||
70 | * |
||
71 | * @param stream $stream The PHP stream resource. |
||
72 | */ |
||
73 | 3 | public function removeWriteStream($stream) |
|
78 | |||
79 | /** |
||
80 | * Remove all listeners for the given stream. |
||
81 | * |
||
82 | * @param stream $stream The PHP stream resource. |
||
83 | */ |
||
84 | 1 | public function removeStream($stream) |
|
89 | |||
90 | /** |
||
91 | * Enqueue a callback to be invoked once after the given interval. |
||
92 | * |
||
93 | * The execution order of timers scheduled to execute at the same time is |
||
94 | * not guaranteed. |
||
95 | * |
||
96 | * @param numeric $interval The number of seconds to wait before execution. |
||
97 | * @param callable $callback The callback to invoke. |
||
98 | * |
||
99 | * @return TimerInterface |
||
100 | */ |
||
101 | 2 | public function addTimer($interval, callable $callback) |
|
116 | 2 | ||
117 | /** |
||
118 | * Enqueue a callback to be invoked repeatedly after the given interval. |
||
119 | * |
||
120 | * The execution order of timers scheduled to execute at the same time is |
||
121 | * not guaranteed. |
||
122 | * |
||
123 | * @param numeric $interval The number of seconds to wait before execution. |
||
124 | * @param callable $callback The callback to invoke. |
||
125 | * |
||
126 | * @return TimerInterface |
||
127 | */ |
||
128 | public function addPeriodicTimer($interval, callable $callback) |
||
141 | 2 | ||
142 | 2 | /** |
|
143 | 2 | * Cancel a pending timer. |
|
144 | * |
||
145 | * @param TimerInterface $timer The timer to cancel. |
||
146 | */ |
||
147 | public function cancelTimer(TimerInterface $timer) |
||
153 | 4 | ||
154 | 4 | /** |
|
155 | * Check if a given timer is active. |
||
156 | * |
||
157 | * @param TimerInterface $timer The timer to check. |
||
158 | * |
||
159 | * @return bool True if the timer is still enqueued for execution. |
||
160 | */ |
||
161 | public function isTimerActive(TimerInterface $timer) |
||
165 | |||
166 | 2 | /** |
|
167 | * Schedule a callback to be invoked on a future tick of the event loop. |
||
168 | * |
||
169 | * Callbacks are guaranteed to be executed in the order they are enqueued. |
||
170 | * |
||
171 | * @param callable $listener The callback to invoke. |
||
172 | */ |
||
173 | public function futureTick(callable $listener) |
||
182 | 2 | ||
183 | 2 | /** |
|
184 | * Perform a single iteration of the event loop. |
||
185 | */ |
||
186 | public function tick() |
||
192 | |||
193 | 4 | /** |
|
194 | * Run the event loop until there are no more tasks to perform. |
||
195 | 4 | */ |
|
196 | 4 | public function run() |
|
202 | |||
203 | /** |
||
204 | * Instruct a running event loop to stop. |
||
205 | 1 | */ |
|
206 | public function stop() |
||
212 | } |
||
213 |
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: