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 RedisStatisticsLogger implements StatisticsLogger |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The Channel manager. |
||
| 15 | * |
||
| 16 | * @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager |
||
| 17 | */ |
||
| 18 | protected $channelManager; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The statistics driver instance. |
||
| 22 | * |
||
| 23 | * @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver |
||
| 24 | */ |
||
| 25 | protected $driver; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Redis manager instance. |
||
| 29 | * |
||
| 30 | * @var \Illuminate\Redis\RedisManager |
||
| 31 | */ |
||
| 32 | protected $redis; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Initialize the logger. |
||
| 36 | * |
||
| 37 | * @param \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager $channelManager |
||
| 38 | * @param \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver $driver |
||
| 39 | * @return void |
||
|
|
|||
| 40 | */ |
||
| 41 | public function __construct(ChannelManager $channelManager, StatisticsDriver $driver) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Handle the incoming websocket message. |
||
| 50 | * |
||
| 51 | * @param mixed $appId |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function webSocketMessage($appId) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Handle the incoming API message. |
||
| 62 | * |
||
| 63 | * @param mixed $appId |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function apiMessage($appId) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Handle the new conection. |
||
| 74 | * |
||
| 75 | * @param mixed $appId |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function connection($appId) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Handle disconnections. |
||
| 94 | * |
||
| 95 | * @param mixed $appId |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function disconnection($appId) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Save all the stored statistics. |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function save() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Ensure the app id is stored in the Redis database. |
||
| 143 | * |
||
| 144 | * @param mixed $appId |
||
| 145 | * @return \Illuminate\Redis\RedisManager |
||
| 146 | */ |
||
| 147 | protected function ensureAppIsSet($appId) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Reset the statistics to a specific connection count. |
||
| 156 | * |
||
| 157 | * @param mixed $appId |
||
| 158 | * @param int $currentConnectionCount |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function resetStatistics($appId, int $currentConnectionCount) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Remove all app traces from the database if no connections have been set |
||
| 171 | * in the meanwhile since last save. |
||
| 172 | * |
||
| 173 | * @param mixed $appId |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function resetAppTraces($appId) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get the Redis hash name for the app. |
||
| 188 | * |
||
| 189 | * @param mixed $appId |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | protected function getHash($appId): string |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get a new RedisLock instance to avoid race conditions. |
||
| 199 | * |
||
| 200 | * @return \Illuminate\Cache\CacheLock |
||
| 201 | */ |
||
| 202 | protected function lock() |
||
| 206 | } |
||
| 207 |
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.