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 |
||
| 19 | class StartWebSocketServer extends Command |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The name and signature of the console command. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $signature = 'websockets:serve |
||
| 27 | {--host=0.0.0.0} |
||
| 28 | {--port=6001} |
||
| 29 | {--statistics-interval= : Overwrite the statistics interval set in the config.} |
||
| 30 | {--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.} |
||
| 31 | {--test : Prepare the server, but do not start it.} |
||
| 32 | '; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The console command description. |
||
| 36 | * |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | protected $description = 'Start the Laravel WebSocket Server'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the loop instance. |
||
| 43 | * |
||
| 44 | * @var \React\EventLoop\LoopInterface |
||
| 45 | */ |
||
| 46 | protected $loop; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The Pusher server instance. |
||
| 50 | * |
||
| 51 | * @var \Ratchet\Server\IoServer |
||
| 52 | */ |
||
| 53 | public $server; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Track the last restart. |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | protected $lastRestart; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize the command. |
||
| 64 | * |
||
| 65 | * @return void |
||
|
|
|||
| 66 | */ |
||
| 67 | public function __construct() |
||
| 68 | { |
||
| 69 | parent::__construct(); |
||
| 70 | |||
| 71 | $this->loop = LoopFactory::create(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Run the command. |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function handle() |
||
| 80 | { |
||
| 81 | $this |
||
| 82 | ->configureStatisticsLogger() |
||
| 83 | ->configureHttpLogger() |
||
| 84 | ->configureMessageLogger() |
||
| 85 | ->configureConnectionLogger() |
||
| 86 | ->configureRestartTimer() |
||
| 87 | ->configurePubSub() |
||
| 88 | ->registerRoutes() |
||
| 89 | ->startWebSocketServer(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Configure the statistics logger class. |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | protected function configureStatisticsLogger() |
||
| 98 | { |
||
| 99 | $this->laravel->singleton(StatisticsLoggerInterface::class, function () { |
||
| 100 | $replicationDriver = config('websockets.replication.driver', 'local'); |
||
| 101 | |||
| 102 | $class = config("websockets.replication.{$replicationDriver}.statistics_logger", \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class); |
||
| 103 | |||
| 104 | return new $class( |
||
| 105 | $this->laravel->make(ChannelManager::class), |
||
| 106 | $this->laravel->make(StatisticsDriver::class) |
||
| 107 | ); |
||
| 108 | }); |
||
| 109 | |||
| 110 | $this->loop->addPeriodicTimer($this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds'), function () { |
||
| 111 | $this->line('Saving statistics...'); |
||
| 112 | |||
| 113 | StatisticsLogger::save(); |
||
| 114 | }); |
||
| 115 | |||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Configure the HTTP logger class. |
||
| 121 | * |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | View Code Duplication | protected function configureHttpLogger() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Configure the logger for messages. |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | View Code Duplication | protected function configureMessageLogger() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Configure the connection logger. |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | View Code Duplication | protected function configureConnectionLogger() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Configure the Redis PubSub handler. |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function configureRestartTimer() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Configure the replicators. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function configurePubSub() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Register the routes. |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | protected function registerRoutes() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Start the server. |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | protected function startWebSocketServer() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Build the server instance. |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | protected function buildServer() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the last time the server restarted. |
||
| 265 | * |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | protected function getLastRestart() |
||
| 272 | } |
||
| 273 |
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.