Conditions | 14 |
Paths | 1 |
Total Lines | 77 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
25 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
26 | { |
||
27 | $port = $input->getArgument('port'); |
||
28 | $output->writeln("Iniciando servidor WebSocket ReactPHP na porta {$port} (com tentativa de handshake)..."); |
||
29 | |||
30 | $loop = Loop::get(); |
||
31 | $socket = new Server("0.0.0.0:{$port}", $loop); |
||
32 | $clients = new \SplObjectStorage(); |
||
33 | |||
34 | $socket->on('connection', function (ConnectionInterface $conn) use ($clients, $output) { |
||
35 | $handshakeDone = false; |
||
36 | $buffer = ''; |
||
37 | |||
38 | $conn->on('data', function ($data) use ($conn, $clients, $output, &$handshakeDone, &$buffer) { |
||
39 | $buffer .= $data; |
||
40 | |||
41 | if (!$handshakeDone) { |
||
42 | // Tenta encontrar o final dos headers HTTP (\r\n\r\n) |
||
43 | if (strpos($buffer, "\r\n\r\n") !== false) { |
||
44 | $headers = []; |
||
45 | $headerLines = explode("\r\n", substr($buffer, 0, strpos($buffer, "\r\n\r\n"))); |
||
46 | |||
47 | // Analisa os headers |
||
48 | foreach ($headerLines as $line) { |
||
49 | if (strpos($line, ':') !== false) { |
||
50 | [$key, $value] = explode(':', $line, 2); |
||
51 | $headers[trim(strtolower($key))] = trim($value); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | // Verifica se é uma requisição de upgrade WebSocket |
||
56 | if (isset($headers['upgrade']) && strtolower($headers['upgrade']) === 'websocket' && |
||
57 | isset($headers['connection']) && strtolower($headers['connection']) === 'upgrade' && |
||
58 | isset($headers['sec-websocket-key']) && isset($headers['sec-websocket-version']) && $headers['sec-websocket-version'] === '13') { |
||
59 | |||
60 | $secWebSocketKey = $headers['sec-websocket-key']; |
||
61 | $magicString = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; |
||
62 | $hash = base64_encode(sha1($secWebSocketKey . $magicString, true)); |
||
63 | $response = "HTTP/1.1 101 Switching Protocols\r\n"; |
||
64 | $response .= "Upgrade: websocket\r\n"; |
||
65 | $response .= "Connection: Upgrade\r\n"; |
||
66 | $response .= "Sec-WebSocket-Accept: " . $hash . "\r\n\r\n"; |
||
67 | |||
68 | $conn->write($response); |
||
69 | $handshakeDone = true; |
||
70 | $clients->attach($conn); |
||
71 | $output->writeln("Nova conexão WebSocket estabelecida! ({$conn->resourceId})"); |
||
72 | |||
73 | // Remove os headers do buffer para processar dados WebSocket futuros |
||
74 | $buffer = substr($buffer, strpos($buffer, "\r\n\r\n") + 4); |
||
75 | |||
76 | } else { |
||
77 | $output->writeln("Requisição de handshake inválida. Fechando conexão."); |
||
78 | $conn->close(); |
||
79 | } |
||
80 | } |
||
81 | } else { |
||
82 | // Lógica para lidar com dados WebSocket (decodificação de frames, etc.) virá aqui |
||
83 | // Por enquanto, apenas reenvia os dados para outros clientes |
||
84 | foreach ($clients as $client) { |
||
85 | if ($client !== $conn) { |
||
86 | $client->write($data); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | }); |
||
91 | |||
92 | $conn->on('close', function () use ($conn, $clients, $output) { |
||
93 | $clients->detach($conn); |
||
94 | $output->writeln("Conexão fechada! ({$conn->resourceId})"); |
||
95 | }); |
||
96 | }); |
||
97 | |||
98 | $output->writeln('Servidor WebSocket ReactPHP iniciado (com tentativa de handshake)!'); |
||
99 | $loop->run(); |
||
100 | |||
101 | return Command::SUCCESS; |
||
102 | } |
||
103 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths