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 |
||
23 | class StartWebSocketServer extends Command |
||
24 | { |
||
25 | 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 } '; |
||
26 | |||
27 | protected $description = 'Start the Laravel WebSocket Server'; |
||
28 | |||
29 | /** @var \React\EventLoop\LoopInterface */ |
||
30 | protected $loop; |
||
31 | |||
32 | public function __construct() |
||
33 | { |
||
34 | parent::__construct(); |
||
35 | |||
36 | $this->loop = LoopFactory::create(); |
||
37 | } |
||
38 | |||
39 | public function handle() |
||
40 | { |
||
41 | $this |
||
42 | ->configureStatisticsLogger() |
||
43 | ->configureHttpLogger() |
||
44 | ->configureMessageLogger() |
||
45 | ->configureConnectionLogger() |
||
46 | ->registerEchoRoutes() |
||
47 | ->startWebSocketServer(); |
||
48 | } |
||
49 | |||
50 | protected function configureStatisticsLogger() |
||
51 | { |
||
52 | $connector = new Connector($this->loop, [ |
||
53 | 'dns' => $this->getDnsResolver(), |
||
54 | 'tls' => [ |
||
55 | 'verify_peer' => config('app.env') === 'production', |
||
56 | 'verify_peer_name' => config('app.env') === 'production', |
||
57 | ], |
||
58 | ]); |
||
59 | |||
60 | $browser = new Browser($this->loop, $connector); |
||
61 | |||
62 | app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { |
||
63 | return new HttpStatisticsLogger(app(ChannelManager::class), $browser); |
||
64 | }); |
||
65 | |||
66 | $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { |
||
67 | StatisticsLogger::save(); |
||
68 | }); |
||
69 | |||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | protected function configureHttpLogger() |
||
83 | |||
84 | View Code Duplication | protected function configureMessageLogger() |
|
94 | |||
95 | View Code Duplication | protected function configureConnectionLogger() |
|
105 | |||
106 | protected function registerEchoRoutes() |
||
112 | |||
113 | protected function startWebSocketServer() |
||
129 | |||
130 | protected function getDnsResolver(): ReactDnsResolver |
||
131 | { |
||
132 | if (! config('websockets.statistics.perform_dns_lookup')) { |
||
145 | } |
||
146 |
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.