Code cleanup, added phpstan

This commit is contained in:
Chris 2024-02-21 23:48:13 +01:00
parent a6f70dbb76
commit 70e353bd0c
5 changed files with 47 additions and 23 deletions

View File

@ -3,6 +3,7 @@
"description": "Native ReactPHP WebSocket implementation",
"type": "library",
"license": "GPL-3.0-or-later",
"keywords": [ "reactphp", "websockets" ],
"autoload": {
"psr-4": {
"NoccyLabs\\React\\WebSocket\\": "src/"
@ -18,6 +19,7 @@
"react/http": "^1.9.0"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
"phpunit/phpunit": "^11.0",
"phpstan/phpstan": "^1.10"
}
}

12
phpstan.neon Normal file
View File

@ -0,0 +1,12 @@
parameters:
level: 5
excludePaths:
- doc
- vendor
- tests
# Paths to include in the analysis
paths:
- src

View File

@ -60,4 +60,9 @@ class ConnectionGroup implements EventEmitterInterface, IteratorAggregate, Count
{
return $this->name;
}
public function write(string $payload)
{
}
}

View File

@ -4,15 +4,19 @@ namespace NoccyLabs\React\WebSocket\Group;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
use NoccyLabs\React\WebSocket\WebSocketInterface;
use React\EventLoop\Loop;
use WeakReference;
class GroupManager implements EventEmitterInterface
{
use EventEmitterTrait;
/**
* @var string emitted when a new group is created
*/
const EVENT_CREATED = 'created';
/**
* @var string emitted after the last member leaves, when the group is destroyed
*/
const EVENT_DESTROYED = 'destroyed';
/** @var array<string,ConnectionGroup> */

View File

@ -54,6 +54,10 @@ class WebSocketConnection implements WebSocketInterface
$this->groupManager = $groupManager;
$this->inStream->on('data', $this->onWebSocketData(...));
$this->inStream->on('close', function () {
$this->close();
$this->emit('close', []);
});
}
private function onWebSocketData($data)
@ -87,6 +91,7 @@ class WebSocketConnection implements WebSocketInterface
$this->sendPong($payload);
return;
case self::OP_PONG:
$this->checkPong($payload);
return;
case self::OP_CLOSE:
// TODO implement
@ -103,11 +108,27 @@ class WebSocketConnection implements WebSocketInterface
}
}
/**
* Sends a ping, and closes the connection on timeout.
*
*/
public function ping(): void
{
// TODO save the state somehow
$payload = "ping";
$this->send(self::OP_PING, $payload, true);
}
private function sendPong(string $data): void
{
$this->send(self::OP_PONG, $data, true);
}
private function checkPong(string $data): void
{
// TODO reset the state and any ping timers
}
public function setGroup(?string $name): void
{
if ($this->group) {
@ -215,24 +236,4 @@ class WebSocketConnection implements WebSocketInterface
}
// private function hexdump($data): void
// {
// printf("%4d .\n", strlen($data));
// $rows = str_split($data, 16);
// $offs = 0;
// foreach ($rows as $row) {
// $h = []; $a = [];
// for ($n = 0; $n < 16; $n++) {
// if ($n < strlen($row)) {
// $h[] = sprintf("%02x%s", ord($row[$n]), ($n==7)?" ":" ");
// $a[] = sprintf("%s%s", (ctype_print($row[$n])?$row[$n]:"."), ($n==7)?" ":"");
// } else {
// $h[] = (($n==7)?" ":" ");
// $a[] = (($n==7)?" ":" ");
// }
// }
// printf("%04x | %s | %s\n", 16 * $offs++, join("", $h), join("", $a));
// }
// }
}