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 |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the amount of connections aggregated on multiple instances. |
||
| 293 | * |
||
| 294 | * @param mixed $appId |
||
| 295 | * @return null|int|\React\Promise\PromiseInterface |
||
| 296 | */ |
||
| 297 | public function getGlobalConnectionsCount($appId) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Handle a message received from Redis on a specific channel. |
||
| 304 | * |
||
| 305 | * @param string $redisChannel |
||
| 306 | * @param string $payload |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function onMessage(string $redisChannel, string $payload) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Build the Redis connection URL from Laravel database config. |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function getConnectionUri() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the Subscribe client instance. |
||
| 388 | * |
||
| 389 | * @return Client |
||
| 390 | */ |
||
| 391 | public function getSubscribeClient() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the Publish client instance. |
||
| 398 | * |
||
| 399 | * @return Client |
||
| 400 | */ |
||
| 401 | public function getPublishClient() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the unique identifier for the server. |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getServerId() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the Pub/Sub Topic name to subscribe based on the |
||
| 418 | * app ID and channel name. |
||
| 419 | * |
||
| 420 | * @param mixed $appId |
||
| 421 | * @param string|null $channel |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | protected function getTopicName($appId, string $channel = null): string |
||
| 436 | } |
||
| 437 |
Adding a
@returnannotation 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.