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 |
||
| 11 | class RedisCollector extends MemoryCollector |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The Redis manager instance. |
||
| 15 | * |
||
| 16 | * @var \Illuminate\Redis\RedisManager |
||
| 17 | */ |
||
| 18 | protected $redis; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The set name for the Redis storage. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected static $redisSetName = 'laravel-websockets:apps'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The lock name to use on Redis to avoid multiple |
||
| 29 | * collector-to-store actions that may result |
||
| 30 | * in multiple data points set to the store. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected static $redisLockName = 'laravel-websockets:collector:lock'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Initialize the logger. |
||
| 38 | * |
||
| 39 | * @return void |
||
|
|
|||
| 40 | */ |
||
| 41 | public function __construct() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Handle the incoming websocket message. |
||
| 52 | * |
||
| 53 | * @param string|int $appId |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function webSocketMessage($appId) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Handle the incoming API message. |
||
| 64 | * |
||
| 65 | * @param string|int $appId |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function apiMessage($appId) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Handle the new conection. |
||
| 76 | * |
||
| 77 | * @param string|int $appId |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function connection($appId) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Handle disconnections. |
||
| 116 | * |
||
| 117 | * @param string|int $appId |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function disconnection($appId) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Save all the stored statistics. |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function save() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Flush the stored statistics. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function flush() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the saved statistics. |
||
| 204 | * |
||
| 205 | * @return PromiseInterface[array] |
||
| 206 | */ |
||
| 207 | public function getStatistics(): PromiseInterface |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the saved statistics for an app. |
||
| 232 | * |
||
| 233 | * @param string|int $appId |
||
| 234 | * @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
||
| 235 | */ |
||
| 236 | public function getAppStatistics($appId): PromiseInterface |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Reset the statistics to a specific connection count. |
||
| 250 | * |
||
| 251 | * @param string|int $appId |
||
| 252 | * @param int $currentConnectionCount |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function resetStatistics($appId, int $currentConnectionCount) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Remove all app traces from the database if no connections have been set |
||
| 288 | * in the meanwhile since last save. |
||
| 289 | * |
||
| 290 | * @param string|int $appId |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function resetAppTraces($appId) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Ensure the app id is stored in the Redis database. |
||
| 330 | * |
||
| 331 | * @param string|int $appId |
||
| 332 | * @return \Clue\React\Redis\Client |
||
| 333 | */ |
||
| 334 | protected function ensureAppIsInSet($appId) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get a new RedisLock instance to avoid race conditions. |
||
| 345 | * |
||
| 346 | * @return \Illuminate\Cache\CacheLock |
||
| 347 | */ |
||
| 348 | protected function lock() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Transform a key-value pair to a Statistic instance. |
||
| 355 | * |
||
| 356 | * @param string|int $appId |
||
| 357 | * @param array $stats |
||
| 358 | * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
||
| 359 | */ |
||
| 360 | protected function arrayToStatisticInstance($appId, array $stats) |
||
| 368 | } |
||
| 369 |
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.