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() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Ensure the app id is stored in the Redis database. |
||
| 138 | * |
||
| 139 | * @param mixed $appId |
||
| 140 | * @return \Illuminate\Redis\RedisManager |
||
| 141 | */ |
||
| 142 | protected function ensureAppIsSet($appId) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Reset the statistics to a specific connection count. |
||
| 151 | * |
||
| 152 | * @param mixed $appId |
||
| 153 | * @param int $currentConnectionCount |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function resetStatistics($appId, int $currentConnectionCount) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Remove all app traces from the database if no connections have been set |
||
| 166 | * in the meanwhile since last save. |
||
| 167 | * |
||
| 168 | * @param mixed $appId |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function resetAppTraces($appId) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Get the Redis hash name for the app. |
||
| 183 | * |
||
| 184 | * @param mixed $appId |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | protected function getHash($appId): string |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get a new RedisLock instance to avoid race conditions. |
||
| 194 | * |
||
| 195 | * @return \Illuminate\Cache\CacheLock |
||
| 196 | */ |
||
| 197 | protected function lock() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Create a new record using the Statistic Driver. |
||
| 204 | * |
||
| 205 | * @param array $statistic |
||
| 206 | * @param mixed $appId |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | protected function createRecord(array $statistic, $appId): void |
||
| 218 | } |
||
| 219 |
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.