Code cleanup, added phpstan
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
				
			|||||||
    "description": "Native ReactPHP WebSocket implementation",
 | 
					    "description": "Native ReactPHP WebSocket implementation",
 | 
				
			||||||
    "type": "library",
 | 
					    "type": "library",
 | 
				
			||||||
    "license": "GPL-3.0-or-later",
 | 
					    "license": "GPL-3.0-or-later",
 | 
				
			||||||
 | 
					    "keywords": [ "reactphp", "websockets" ],
 | 
				
			||||||
    "autoload": {
 | 
					    "autoload": {
 | 
				
			||||||
        "psr-4": {
 | 
					        "psr-4": {
 | 
				
			||||||
            "NoccyLabs\\React\\WebSocket\\": "src/"
 | 
					            "NoccyLabs\\React\\WebSocket\\": "src/"
 | 
				
			||||||
@@ -18,6 +19,7 @@
 | 
				
			|||||||
        "react/http": "^1.9.0"
 | 
					        "react/http": "^1.9.0"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "require-dev": {
 | 
					    "require-dev": {
 | 
				
			||||||
        "phpunit/phpunit": "^11.0"
 | 
					        "phpunit/phpunit": "^11.0",
 | 
				
			||||||
 | 
					        "phpstan/phpstan": "^1.10"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								phpstan.neon
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								phpstan.neon
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					parameters:
 | 
				
			||||||
 | 
					  level: 5
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					  excludePaths:
 | 
				
			||||||
 | 
					    - doc
 | 
				
			||||||
 | 
					    - vendor
 | 
				
			||||||
 | 
					    - tests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Paths to include in the analysis
 | 
				
			||||||
 | 
					  paths:
 | 
				
			||||||
 | 
					    - src
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,4 +60,9 @@ class ConnectionGroup implements EventEmitterInterface, IteratorAggregate, Count
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->name;
 | 
					        return $this->name;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function write(string $payload)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -4,15 +4,19 @@ namespace NoccyLabs\React\WebSocket\Group;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use Evenement\EventEmitterInterface;
 | 
					use Evenement\EventEmitterInterface;
 | 
				
			||||||
use Evenement\EventEmitterTrait;
 | 
					use Evenement\EventEmitterTrait;
 | 
				
			||||||
use NoccyLabs\React\WebSocket\WebSocketInterface;
 | 
					 | 
				
			||||||
use React\EventLoop\Loop;
 | 
					use React\EventLoop\Loop;
 | 
				
			||||||
use WeakReference;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class GroupManager implements EventEmitterInterface
 | 
					class GroupManager implements EventEmitterInterface
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    use EventEmitterTrait;
 | 
					    use EventEmitterTrait;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var string emitted when a new group is created
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    const EVENT_CREATED = 'created';
 | 
					    const EVENT_CREATED = 'created';
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var string emitted after the last member leaves, when the group is destroyed
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    const EVENT_DESTROYED = 'destroyed';
 | 
					    const EVENT_DESTROYED = 'destroyed';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /** @var array<string,ConnectionGroup> */
 | 
					    /** @var array<string,ConnectionGroup> */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,6 +54,10 @@ class WebSocketConnection implements WebSocketInterface
 | 
				
			|||||||
        $this->groupManager = $groupManager;
 | 
					        $this->groupManager = $groupManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->inStream->on('data', $this->onWebSocketData(...));
 | 
					        $this->inStream->on('data', $this->onWebSocketData(...));
 | 
				
			||||||
 | 
					        $this->inStream->on('close', function () {
 | 
				
			||||||
 | 
					            $this->close();
 | 
				
			||||||
 | 
					            $this->emit('close', []);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private function onWebSocketData($data)
 | 
					    private function onWebSocketData($data)
 | 
				
			||||||
@@ -87,6 +91,7 @@ class WebSocketConnection implements WebSocketInterface
 | 
				
			|||||||
                $this->sendPong($payload);
 | 
					                $this->sendPong($payload);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            case self::OP_PONG:
 | 
					            case self::OP_PONG:
 | 
				
			||||||
 | 
					                $this->checkPong($payload);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            case self::OP_CLOSE:
 | 
					            case self::OP_CLOSE:
 | 
				
			||||||
                // TODO implement
 | 
					                // 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
 | 
					    private function sendPong(string $data): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->send(self::OP_PONG, $data, true);
 | 
					        $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
 | 
					    public function setGroup(?string $name): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if ($this->group) {
 | 
					        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));
 | 
					 | 
				
			||||||
    //     }
 | 
					 | 
				
			||||||
    // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user