Skip to content

Commit f0a59bf

Browse files
Merge pull request #18 from aaheli8/main
4379 Feat: Add Code Analyzer
2 parents 828cb45 + e3b5ce0 commit f0a59bf

File tree

8 files changed

+110
-19
lines changed

8 files changed

+110
-19
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "CodeQL"
2+
3+
on: [pull_request]
4+
jobs:
5+
lint:
6+
name: CodeQL
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v3
12+
with:
13+
fetch-depth: 2
14+
15+
- run: git checkout HEAD^2
16+
17+
- name: Run CodeQL
18+
run: |
19+
docker run --rm -v $PWD:/app composer sh -c \
20+
"composer install --profile --ignore-platform-reqs && composer check"

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"type": "library",
55
"keywords": ["php","framework", "upf", "utopia", "websocket"],
66
"license": "MIT",
7+
"scripts": {
8+
"check": "./vendor/bin/phpstan analyse --level max src tests"
9+
},
710
"minimum-stability": "stable",
811
"autoload": {
912
"psr-4": {"Utopia\\WebSocket\\": "src/WebSocket"}
@@ -16,6 +19,7 @@
1619
"textalk/websocket": "1.5.2",
1720
"phpunit/phpunit": "^9.5.5",
1821
"vimeo/psalm": "^4.8.1",
19-
"workerman/workerman": "^4.0"
22+
"workerman/workerman": "^4.0",
23+
"phpstan/phpstan": "^1.8"
2024
}
2125
}

composer.lock

Lines changed: 61 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WebSocket/Adapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ abstract class Adapter
66
{
77
protected string $host;
88
protected int $port;
9+
/**
10+
* @var array<int|string,bool|int|string>
11+
*/
12+
913
protected array $config = [];
1014

1115
function __construct(string $host = '0.0.0.0', int $port = 80) {
@@ -27,7 +31,7 @@ public abstract function shutdown(): void;
2731

2832
/**
2933
* Sends a message to passed connections.
30-
* @param array $connections Array of connection ID's.
34+
* @param array<mixed,mixed> $connections Array of connection ID's.
3135
* @param string $message Message.
3236
* @return void
3337
*/
@@ -105,7 +109,7 @@ public abstract function getNative(): mixed;
105109

106110
/**
107111
* Returns all connections.
108-
* @return array
112+
* @return array<mixed>
109113
*/
110114
public abstract function getConnections(): array;
111115
}

src/WebSocket/Adapter/Swoole.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class Swoole extends Adapter
1818

1919
protected string $host;
2020
protected int $port;
21-
21+
/**
22+
* @var array<int|string,bool|int|string>
23+
*/
2224
private static array $connections = [];
2325

2426
public function __construct(string $host = '0.0.0.0', int $port = 80)

src/WebSocket/Adapter/Workerman.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ public function __construct(string $host = '0.0.0.0', int $port = 80)
2525

2626
$this->server = new Worker("websocket://{$this->host}:{$this->port}");
2727
}
28-
28+
2929
public function start(): void
3030
{
3131
Worker::runAll();
32-
call_user_func($this->callbackOnStart);
32+
$callable = ($this->callbackOnStart);
33+
if (!is_callable($callable)) {
34+
throw new \Exception();
35+
}
36+
\call_user_func($callable);
3337
}
3438

3539
public function shutdown(): void
3640
{
3741
Worker::stopAll();
3842
}
39-
43+
4044
public function send(array $connections, string $message): void
4145
{
4246
foreach ($connections as $connection) {
@@ -65,10 +69,10 @@ public function onWorkerStart(callable $callback): self
6569

6670
public function onOpen(callable $callback): self
6771
{
68-
$this->server->onConnect = function (mixed $connection) use ($callback): void {
72+
$this->server->onConnect = function ($connection) use ($callback): void {
6973
$connection->onWebSocketConnect = function(TcpConnection $connection) use ($callback): void
7074
{
71-
/** @var array $_SERVER */
75+
/** @var array<string> $_SERVER */
7276
call_user_func($callback, $connection->id, $_SERVER);
7377
};
7478
};

src/WebSocket/Server.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
class Server
1010
{
11+
1112
/**
1213
* Callbacks that will be executed when an error occurs
13-
*
14-
* @var array
14+
*
15+
* @var array<callable>
1516
*/
1617
protected $errorCallbacks = [];
1718

18-
1919
protected Adapter $adapter;
2020

2121
/**
@@ -59,7 +59,7 @@ public function shutdown(): void
5959

6060
/**
6161
* Sends a message to passed connections.
62-
* @param array $connections Array of connection ID's.
62+
* @param array<mixed,mixed> $connections Array of connection ID's.
6363
* @param string $message Message.
6464
* @return void
6565
*/
@@ -182,8 +182,7 @@ public function onClose(callable $callback): self
182182

183183
/**
184184
* Returns all connections.
185-
* @param callable $callback
186-
* @return array
185+
* @return array<mixed>
187186
*/
188187
public function getConnections(): array
189188
{
@@ -193,7 +192,6 @@ public function getConnections(): array
193192
/**
194193
* Register callback. Will be executed when error occurs.
195194
* @param callable $callback
196-
* @param Throwable $error
197195
* @return self
198196
*/
199197
public function error(callable $callback): self

tests/e2e/AdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testWorkerman(): void
2525
$this->testServer(8002);
2626
}
2727

28-
private function testServer(int $port)
28+
private function testServer(int $port) :void
2929
{
3030
$client = $this->getWebsocket('localhost', $port);
3131
$client->send('ping');

0 commit comments

Comments
 (0)