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 |
||
16 | class RedisClient extends LocalClient |
||
17 | { |
||
18 | /** |
||
19 | * The running loop. |
||
20 | * |
||
21 | * @var LoopInterface |
||
22 | */ |
||
23 | protected $loop; |
||
24 | |||
25 | /** |
||
26 | * The unique server identifier. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $serverId; |
||
31 | |||
32 | /** |
||
33 | * The pub client. |
||
34 | * |
||
35 | * @var Client |
||
36 | */ |
||
37 | protected $publishClient; |
||
38 | |||
39 | /** |
||
40 | * The sub client. |
||
41 | * |
||
42 | * @var Client |
||
43 | */ |
||
44 | protected $subscribeClient; |
||
45 | |||
46 | /** |
||
47 | * The Redis manager instance. |
||
48 | * |
||
49 | * @var \Illuminate\Redis\RedisManager |
||
50 | */ |
||
51 | protected $redis; |
||
52 | |||
53 | /** |
||
54 | * Mapping of subscribed channels, where the key is the channel name, |
||
55 | * and the value is the amount of connections which are subscribed to |
||
56 | * that channel. Used to keep track of whether we still need to stay |
||
57 | * subscribed to those channels with Redis. |
||
58 | * |
||
59 | * @var int[] |
||
60 | */ |
||
61 | protected $subscribedChannels = []; |
||
62 | |||
63 | /** |
||
64 | * Create a new Redis client. |
||
65 | * |
||
66 | * @return void |
||
|
|||
67 | */ |
||
68 | public function __construct() |
||
73 | |||
74 | /** |
||
75 | * Boot the RedisClient, initializing the connections. |
||
76 | * |
||
77 | * @param LoopInterface $loop |
||
78 | * @param string|null $factoryClass |
||
79 | * @return ReplicationInterface |
||
80 | */ |
||
81 | public function boot(LoopInterface $loop, $factoryClass = null): ReplicationInterface |
||
100 | |||
101 | /** |
||
102 | * Publish a message to a channel on behalf of a websocket user. |
||
103 | * |
||
104 | * @param string $appId |
||
105 | * @param string $channel |
||
106 | * @param stdClass $payload |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function publish($appId, string $channel, stdClass $payload): bool |
||
127 | |||
128 | /** |
||
129 | * Subscribe to a channel on behalf of websocket user. |
||
130 | * |
||
131 | * @param string $appId |
||
132 | * @param string $channel |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function subscribe($appId, string $channel): bool |
||
154 | |||
155 | /** |
||
156 | * Unsubscribe from a channel on behalf of a websocket user. |
||
157 | * |
||
158 | * @param string $appId |
||
159 | * @param string $channel |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function unsubscribe($appId, string $channel): bool |
||
186 | |||
187 | /** |
||
188 | * Subscribe to the app's pubsub keyspace. |
||
189 | * |
||
190 | * @param mixed $appId |
||
191 | * @return bool |
||
192 | */ |
||
193 | View Code Duplication | public function subscribeToApp($appId): bool |
|
201 | |||
202 | /** |
||
203 | * Unsubscribe from the app's pubsub keyspace. |
||
204 | * |
||
205 | * @param mixed $appId |
||
206 | * @return bool |
||
207 | */ |
||
208 | View Code Duplication | public function unsubscribeFromApp($appId): bool |
|
216 | |||
217 | /** |
||
218 | * Add a member to a channel. To be called when they have |
||
219 | * subscribed to the channel. |
||
220 | * |
||
221 | * @param string $appId |
||
222 | * @param string $channel |
||
223 | * @param string $socketId |
||
224 | * @param string $data |
||
225 | * @return void |
||
226 | */ |
||
227 | View Code Duplication | public function joinChannel($appId, string $channel, string $socketId, string $data) |
|
239 | |||
240 | /** |
||
241 | * Remove a member from the channel. To be called when they have |
||
242 | * unsubscribed from the channel. |
||
243 | * |
||
244 | * @param string $appId |
||
245 | * @param string $channel |
||
246 | * @param string $socketId |
||
247 | * @return void |
||
248 | */ |
||
249 | View Code Duplication | public function leaveChannel($appId, string $channel, string $socketId) |
|
260 | |||
261 | /** |
||
262 | * Retrieve the full information about the members in a presence channel. |
||
263 | * |
||
264 | * @param string $appId |
||
265 | * @param string $channel |
||
266 | * @return PromiseInterface |
||
267 | */ |
||
268 | public function channelMembers($appId, string $channel): PromiseInterface |
||
278 | |||
279 | /** |
||
280 | * Get the amount of users subscribed for each presence channel. |
||
281 | * |
||
282 | * @param string $appId |
||
283 | * @param array $channelNames |
||
284 | * @return PromiseInterface |
||
285 | */ |
||
286 | public function channelMemberCounts($appId, array $channelNames): PromiseInterface |
||
299 | |||
300 | /** |
||
301 | * Get the amount of unique connections. |
||
302 | * |
||
303 | * @param mixed $appId |
||
304 | * @return null|int|\React\Promise\PromiseInterface |
||
305 | */ |
||
306 | public function appConnectionsCount($appId) |
||
312 | |||
313 | /** |
||
314 | * Handle a message received from Redis on a specific channel. |
||
315 | * |
||
316 | * @param string $redisChannel |
||
317 | * @param string $payload |
||
318 | * @return void |
||
319 | */ |
||
320 | public function onMessage(string $redisChannel, string $payload) |
||
368 | |||
369 | /** |
||
370 | * Build the Redis connection URL from Laravel database config. |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function getConnectionUri() |
||
396 | |||
397 | /** |
||
398 | * Get the Subscribe client instance. |
||
399 | * |
||
400 | * @return Client |
||
401 | */ |
||
402 | public function getSubscribeClient() |
||
406 | |||
407 | /** |
||
408 | * Get the Publish client instance. |
||
409 | * |
||
410 | * @return Client |
||
411 | */ |
||
412 | public function getPublishClient() |
||
416 | |||
417 | /** |
||
418 | * Get the unique identifier for the server. |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | public function getServerId() |
||
426 | |||
427 | /** |
||
428 | * Get the Pub/Sub Topic name to subscribe based on the |
||
429 | * app ID and channel name. |
||
430 | * |
||
431 | * @param mixed $appId |
||
432 | * @param string|null $channel |
||
433 | * @return string |
||
434 | */ |
||
435 | protected function getTopicName($appId, string $channel = null): string |
||
447 | } |
||
448 |
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.