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) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Handle the incoming API message. |
||
| 67 | * |
||
| 68 | * @param string|int $appId |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function apiMessage($appId) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Handle the new conection. |
||
| 82 | * |
||
| 83 | * @param string|int $appId |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function connection($appId) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Handle disconnections. |
||
| 122 | * |
||
| 123 | * @param string|int $appId |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function disconnection($appId) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Save all the stored statistics. |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function save() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Flush the stored statistics. |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function flush() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the saved statistics. |
||
| 216 | * |
||
| 217 | * @return PromiseInterface[array] |
||
| 218 | */ |
||
| 219 | public function getStatistics(): PromiseInterface |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the saved statistics for an app. |
||
| 244 | * |
||
| 245 | * @param string|int $appId |
||
| 246 | * @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
||
| 247 | */ |
||
| 248 | public function getAppStatistics($appId): PromiseInterface |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Reset the statistics to a specific connection count. |
||
| 262 | * |
||
| 263 | * @param string|int $appId |
||
| 264 | * @param int $currentConnectionCount |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | public function resetStatistics($appId, int $currentConnectionCount) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Remove all app traces from the database if no connections have been set |
||
| 300 | * in the meanwhile since last save. |
||
| 301 | * |
||
| 302 | * @param string|int $appId |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function resetAppTraces($appId) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Ensure the app id is stored in the Redis database. |
||
| 342 | * |
||
| 343 | * @param string|int $appId |
||
| 344 | * @return \Clue\React\Redis\Client |
||
| 345 | */ |
||
| 346 | protected function ensureAppIsInSet($appId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get a new RedisLock instance to avoid race conditions. |
||
| 357 | * |
||
| 358 | * @return \Illuminate\Cache\CacheLock |
||
| 359 | */ |
||
| 360 | protected function lock() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Transform a key-value pair to a Statistic instance. |
||
| 367 | * |
||
| 368 | * @param string|int $appId |
||
| 369 | * @param array $stats |
||
| 370 | * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
||
| 371 | */ |
||
| 372 | protected function arrayToStatisticInstance($appId, array $stats) |
||
| 380 | } |
||
| 381 |
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.