Skip to content

Commit d1a05fe

Browse files
author
ahmard
committed
Added phpstan & phpunit
Added PayloadInterface.php for websocket connection
1 parent a29f9bc commit d1a05fe

10 files changed

+92
-20
lines changed

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
"php": ">=8.1",
1919
"guzzlehttp/psr7": "^2.0"
2020
},
21+
"scripts": {
22+
"analyse": "phpstan analyse",
23+
"test": "phpunit"
24+
},
2125
"require-dev": {
22-
"openswoole/ide-helper": "^4.10"
26+
"openswoole/ide-helper": "^4.10",
27+
"phpstan/phpstan": "^1.4",
28+
"phpunit/phpunit": "^9.5"
2329
}
2430
}

phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 8
3+
paths: ['src', 'tests']
4+
checkMissingIterableValueType: false

phpunit.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
>
11+
<testsuites>
12+
<testsuite name="PHP RTC Websocket Server Tests">
13+
<directory>./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/Http/HttpException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract public static function throw(
1212
string $message,
1313
int $code = 0,
1414
Throwable|null $previous = null
15-
);
15+
): never;
1616

1717
abstract public function getRequest(): RequestInterface;
1818
}

src/Http/RequestMiddlewareInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace RTC\Contracts\Http;
44

5-
use RTC\Http\Exceptions\MiddlewareException;
65
use SplQueue;
76

87
interface RequestMiddlewareInterface
@@ -21,7 +20,7 @@ public function push(MiddlewareInterface $middleware): void;
2120
* Executes next middleware in the queue
2221
*
2322
* @return void
24-
* @throws MiddlewareException
23+
* @throws HttpException
2524
*/
2625
public function next(): void;
2726

@@ -49,7 +48,7 @@ public function getCurrent(): null|MiddlewareInterface;
4948
/**
5049
* Retrieve middlewares queue object
5150
*
52-
* @return SplQueue
51+
* @return SplQueue<MiddlewareInterface>
5352
*/
5453
public function getQueue(): SplQueue;
5554
}

src/Http/ResponseInterface.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public function write(string $data): static;
2828
public function cookie(
2929
string $key,
3030
string $value = '',
31-
int $expire = 0,
31+
int $expire = 0,
3232
string $path = '/',
3333
string $domain = '',
34-
bool $secure = false,
35-
bool $httponly = false,
34+
bool $secure = false,
35+
bool $httponly = false,
3636
string $samesite = '',
3737
string $priority = ''
3838
): static;

src/Http/Router/CollectorInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace RTC\Contracts\Http\Router;
44

5+
use FastRoute\RouteCollector;
6+
57
interface CollectorInterface
68
{
79
public function collectFile(string $filePath, array $routesInfo = []): static;
@@ -14,7 +16,7 @@ public function prefixDelimiter(string $delimiter): static;
1416

1517
public function register(): static;
1618

17-
public function getFastRouteCollector(bool $createNew = false): \FastRoute\RouteCollector;
19+
public function getFastRouteCollector(bool $createNew = false): RouteCollector;
1820

1921
public function getCollectedRoutes(): array;
2022

src/Websocket/FrameInterface.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@ interface FrameInterface
99
public function __construct(Frame $frame);
1010

1111
/**
12-
* Returns Swoole frame object
12+
* Gets message sent from client
1313
*
14-
* @return Frame
14+
* @return mixed
1515
*/
16-
public function getFrame(): Frame;
16+
public function getMessage(): mixed;
1717

1818
/**
19-
* Returns json-decoded message array
19+
* Returns command name sent by client
2020
*
21-
* @return array
21+
* @return string|null
2222
*/
23-
public function getMessage(): array;
23+
public function getCommand(): string|null;
2424

2525
/**
26-
* Returns json-decoded message array
26+
* Returns payload-sent time
2727
*
2828
* @return string
2929
*/
30-
public function getRawMessage(): string;
30+
public function getTime(): string;
31+
32+
/**
33+
* Returns json-decoded message array
34+
*
35+
* @return PayloadInterface
36+
*/
37+
public function getPayload(): PayloadInterface;
3138
}

src/Websocket/PayloadInterface.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RTC\Contracts\Websocket;
4+
5+
use Swoole\WebSocket\Frame;
6+
7+
interface PayloadInterface
8+
{
9+
public function __construct(Frame $frame);
10+
11+
/**
12+
* Returns payload sent from client
13+
*
14+
* @return string
15+
*/
16+
public function getRaw(): string;
17+
18+
/**
19+
* Returns json-decoded client-sent payload
20+
*
21+
* @return array
22+
*/
23+
public function getDecoded(): array;
24+
25+
/**
26+
* Returns Swoole frame object
27+
*
28+
* @return Frame
29+
*/
30+
public function getSwooleFrame(): Frame;
31+
32+
/**
33+
* Returns time which the server receives this payload
34+
*
35+
* @return string
36+
*/
37+
public function getServerTime(): string;
38+
39+
}

src/Websocket/WebsocketHandlerInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RTC\Contracts\Websocket;
44

55
use RTC\Server\Server;
6-
use RTC\Websocket\Frame;
76
use Throwable;
87

98
interface WebsocketHandlerInterface
@@ -14,11 +13,11 @@ public function __construct(Server $server);
1413
* Method that will be called when message is received
1514
*
1615
* @param ConnectionInterface $connection
17-
* @param Frame $frame
16+
* @param FrameInterface $frame
1817
* @return void
1918
* @throws Throwable
2019
*/
21-
public function onMessage(ConnectionInterface $connection, Frame $frame): void;
20+
public function onMessage(ConnectionInterface $connection, FrameInterface $frame);
2221

2322
/**
2423
* Method that will be called when new connection is received

0 commit comments

Comments
 (0)