Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
15 | class RedisClient extends LocalClient |
||
16 | { |
||
17 | /** |
||
18 | * The running loop. |
||
19 | * |
||
20 | * @var LoopInterface |
||
21 | */ |
||
22 | protected $loop; |
||
23 | |||
24 | /** |
||
25 | * The unique server identifier. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $serverId; |
||
30 | |||
31 | /** |
||
32 | * The pub client. |
||
33 | * |
||
34 | * @var Client |
||
35 | */ |
||
36 | protected $publishClient; |
||
37 | |||
38 | /** |
||
39 | * The sub client. |
||
40 | * |
||
41 | * @var Client |
||
42 | */ |
||
43 | protected $subscribeClient; |
||
44 | |||
45 | /** |
||
46 | * Mapping of subscribed channels, where the key is the channel name, |
||
47 | * and the value is the amount of connections which are subscribed to |
||
48 | * that channel. Used to keep track of whether we still need to stay |
||
49 | * subscribed to those channels with Redis. |
||
50 | * |
||
51 | * @var int[] |
||
52 | */ |
||
53 | protected $subscribedChannels = []; |
||
54 | |||
55 | /** |
||
56 | * Create a new Redis client. |
||
57 | * |
||
58 | * @return void |
||
|
|||
59 | */ |
||
60 | public function __construct() |
||
64 | |||
65 | /** |
||
66 | * Boot the RedisClient, initializing the connections. |
||
67 | * |
||
68 | * @param LoopInterface $loop |
||
69 | * @param string|null $factoryClass |
||
70 | * @return ReplicationInterface |
||
71 | */ |
||
72 | public function boot(LoopInterface $loop, $factoryClass = null): ReplicationInterface |
||
91 | |||
92 | /** |
||
93 | * Publish a message to a channel on behalf of a websocket user. |
||
94 | * |
||
95 | * @param string $appId |
||
96 | * @param string $channel |
||
97 | * @param stdClass $payload |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function publish($appId, string $channel, stdClass $payload): bool |
||
118 | |||
119 | /** |
||
120 | * Subscribe to a channel on behalf of websocket user. |
||
121 | * |
||
122 | * @param string $appId |
||
123 | * @param string $channel |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function subscribe($appId, string $channel): bool |
||
145 | |||
146 | /** |
||
147 | * Unsubscribe from a channel on behalf of a websocket user. |
||
148 | * |
||
149 | * @param string $appId |
||
150 | * @param string $channel |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function unsubscribe($appId, string $channel): bool |
||
177 | |||
178 | /** |
||
179 | * Subscribe to the app's pubsub keyspace. |
||
180 | * |
||
181 | * @param mixed $appId |
||
182 | * @return bool |
||
183 | */ |
||
184 | View Code Duplication | public function subscribeToApp($appId): bool |
|
192 | |||
193 | /** |
||
194 | * Unsubscribe from the app's pubsub keyspace. |
||
195 | * |
||
196 | * @param mixed $appId |
||
197 | * @return bool |
||
198 | */ |
||
199 | View Code Duplication | public function unsubscribeFromApp($appId): bool |
|
207 | |||
208 | /** |
||
209 | * Add a member to a channel. To be called when they have |
||
210 | * subscribed to the channel. |
||
211 | * |
||
212 | * @param string $appId |
||
213 | * @param string $channel |
||
214 | * @param string $socketId |
||
215 | * @param string $data |
||
216 | * @return void |
||
217 | */ |
||
218 | View Code Duplication | public function joinChannel($appId, string $channel, string $socketId, string $data) |
|
230 | |||
231 | /** |
||
232 | * Remove a member from the channel. To be called when they have |
||
233 | * unsubscribed from the channel. |
||
234 | * |
||
235 | * @param string $appId |
||
236 | * @param string $channel |
||
237 | * @param string $socketId |
||
238 | * @return void |
||
239 | */ |
||
240 | View Code Duplication | public function leaveChannel($appId, string $channel, string $socketId) |
|
251 | |||
252 | /** |
||
253 | * Retrieve the full information about the members in a presence channel. |
||
254 | * |
||
255 | * @param string $appId |
||
256 | * @param string $channel |
||
257 | * @return PromiseInterface |
||
258 | */ |
||
259 | public function channelMembers($appId, string $channel): PromiseInterface |
||
269 | |||
270 | /** |
||
271 | * Get the amount of users subscribed for each presence channel. |
||
272 | * |
||
273 | * @param string $appId |
||
274 | * @param array $channelNames |
||
275 | * @return PromiseInterface |
||
276 | */ |
||
277 | public function channelMemberCounts($appId, array $channelNames): PromiseInterface |
||
291 | |||
292 | /** |
||
293 | * Get the amount of connections aggregated on multiple instances. |
||
294 | * |
||
295 | * @param mixed $appId |
||
296 | * @return null|int|\React\Promise\PromiseInterface |
||
297 | */ |
||
298 | public function getGlobalConnectionsCount($appId) |
||
302 | |||
303 | /** |
||
304 | * Handle a message received from Redis on a specific channel. |
||
305 | * |
||
306 | * @param string $redisChannel |
||
307 | * @param string $payload |
||
308 | * @return void |
||
309 | */ |
||
310 | public function onMessage(string $redisChannel, string $payload) |
||
358 | |||
359 | /** |
||
360 | * Build the Redis connection URL from Laravel database config. |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | protected function getConnectionUri() |
||
386 | |||
387 | /** |
||
388 | * Get the Subscribe client instance. |
||
389 | * |
||
390 | * @return Client |
||
391 | */ |
||
392 | public function getSubscribeClient() |
||
396 | |||
397 | /** |
||
398 | * Get the Publish client instance. |
||
399 | * |
||
400 | * @return Client |
||
401 | */ |
||
402 | public function getPublishClient() |
||
406 | |||
407 | /** |
||
408 | * Get the unique identifier for the server. |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | public function getServerId() |
||
416 | |||
417 | /** |
||
418 | * Get the Pub/Sub Topic name to subscribe based on the |
||
419 | * app ID and channel name. |
||
420 | * |
||
421 | * @param mixed $appId |
||
422 | * @param string|null $channel |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getTopicName($appId, string $channel = null): string |
||
437 | } |
||
438 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.