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 |
||
24 | class StartWebSocketServer extends Command |
||
25 | { |
||
26 | protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} {--debug : Forces the loggers to be enabled and thereby overriding the app.debug config setting } '; |
||
27 | |||
28 | protected $description = 'Start the Laravel WebSocket Server'; |
||
29 | |||
30 | /** @var \React\EventLoop\LoopInterface */ |
||
31 | protected $loop; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $lastRestart; |
||
35 | |||
36 | public function __construct() |
||
37 | { |
||
38 | parent::__construct(); |
||
39 | |||
40 | $this->loop = LoopFactory::create(); |
||
41 | } |
||
42 | |||
43 | public function handle() |
||
44 | { |
||
45 | $this |
||
46 | ->configureStatisticsLogger() |
||
47 | ->configureHttpLogger() |
||
48 | ->configureMessageLogger() |
||
49 | ->configureConnectionLogger() |
||
50 | ->configureRestartTimer() |
||
51 | ->registerEchoRoutes() |
||
52 | ->registerCustomRoutes() |
||
53 | ->startWebSocketServer(); |
||
54 | } |
||
55 | |||
56 | protected function configureStatisticsLogger() |
||
57 | { |
||
58 | $connector = new Connector($this->loop, [ |
||
59 | 'dns' => $this->getDnsResolver(), |
||
60 | 'tls' => [ |
||
61 | 'verify_peer' => config('app.env') === 'production', |
||
62 | 'verify_peer_name' => config('app.env') === 'production', |
||
63 | ], |
||
64 | ]); |
||
65 | |||
66 | $browser = new Browser($this->loop, $connector); |
||
67 | |||
68 | app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { |
||
69 | return new HttpStatisticsLogger(app(ChannelManager::class), $browser); |
||
70 | }); |
||
71 | |||
72 | $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { |
||
73 | StatisticsLogger::save(); |
||
74 | }); |
||
75 | |||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | View Code Duplication | protected function configureHttpLogger() |
|
|
|||
80 | { |
||
81 | app()->singleton(HttpLogger::class, function () { |
||
82 | return (new HttpLogger($this->output)) |
||
83 | ->enable($this->option('debug') ?: config('app.debug')) |
||
84 | ->verbose($this->output->isVerbose()); |
||
85 | }); |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | View Code Duplication | protected function configureMessageLogger() |
|
91 | { |
||
92 | app()->singleton(WebsocketsLogger::class, function () { |
||
93 | return (new WebsocketsLogger($this->output)) |
||
94 | ->enable($this->option('debug') ?: config('app.debug')) |
||
95 | ->verbose($this->output->isVerbose()); |
||
96 | }); |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | View Code Duplication | protected function configureConnectionLogger() |
|
102 | { |
||
103 | app()->bind(ConnectionLogger::class, function () { |
||
104 | return (new ConnectionLogger($this->output)) |
||
105 | ->enable(config('app.debug')) |
||
106 | ->verbose($this->output->isVerbose()); |
||
107 | }); |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | public function configureRestartTimer() |
||
113 | { |
||
114 | $this->lastRestart = $this->getLastRestart(); |
||
115 | |||
116 | $this->loop->addPeriodicTimer(10, function () { |
||
117 | if ($this->getLastRestart() !== $this->lastRestart) { |
||
118 | $this->loop->stop(); |
||
119 | } |
||
120 | }); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | protected function registerEchoRoutes() |
||
126 | { |
||
127 | WebSocketsRouter::echo(); |
||
128 | |||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | protected function registerCustomRoutes() |
||
133 | { |
||
134 | WebSocketsRouter::customRoutes(); |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | protected function startWebSocketServer() |
||
140 | { |
||
141 | $this->info("Starting the WebSocket server on port {$this->option('port')}..."); |
||
142 | |||
143 | $routes = WebSocketsRouter::getRoutes(); |
||
144 | |||
145 | /* 🛰 Start the server 🛰 */ |
||
146 | (new WebSocketServerFactory()) |
||
147 | ->setLoop($this->loop) |
||
148 | ->useRoutes($routes) |
||
149 | ->setHost($this->option('host')) |
||
150 | ->setPort($this->option('port')) |
||
151 | ->setConsoleOutput($this->output) |
||
152 | ->createServer() |
||
153 | ->run(); |
||
154 | } |
||
155 | |||
156 | protected function getDnsResolver(): ResolverInterface |
||
171 | |||
172 | protected function getLastRestart() |
||
173 | { |
||
174 | return Cache::get('beyondcode:websockets:restart', 0); |
||
175 | } |
||
176 | } |
||
177 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.