Skip to content

Commit

Permalink
run static analysis with PHPStan at level 9 (highest)
Browse files Browse the repository at this point in the history
Signed-off-by: Demin Yin <deminy@deminy.net>
  • Loading branch information
deminy committed Dec 13, 2023
1 parent 8f706e3 commit 9ba6084
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 31 deletions.
5 changes: 5 additions & 0 deletions examples/clients/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
foreach ($connections as $connection) {
Coroutine::create(function () use ($connection) {
$stmt = $connection->prepare('SELECT pg_sleep(3)');
if ($stmt === false) {
echo 'Failed to prepare the statement.', PHP_EOL;
return;
}

$stmt->execute(); // The query finishes in 3 seconds.
$stmt->fetchAll();
// The result array returned is:
Expand Down
2 changes: 1 addition & 1 deletion examples/clients/tcp.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$client = new Client(SWOOLE_TCP);
$client->connect('server', $port);
$client->send("client side message to port {$port}");
echo $client->recv(), PHP_EOL;
echo $client->recv(), PHP_EOL; // @phpstan-ignore echo.nonString
$client->close();
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/clients/udp.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
$client = new Client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
$client->connect('server', 9506);
$client->send('Hello Swoole!');
echo $client->recv(), PHP_EOL;
echo $client->recv(), PHP_EOL; // @phpstan-ignore echo.nonString
$client->close();
});
2 changes: 1 addition & 1 deletion examples/clients/websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$client = new Client('server', 9504);
$client->upgrade('/');
$client->push('Swoole');
echo $client->recv()->data, PHP_EOL;
echo $client->recv()->data, PHP_EOL; // @phpstan-ignore property.nonObject
$client->close();
});
});
6 changes: 3 additions & 3 deletions examples/csp/barrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
run(function () {
$barrier = Barrier::make();

go(function () use ($barrier) {
go(function () use ($barrier) { // @phpstan-ignore closure.unusedUse
sleep(1);
});

go(function () use ($barrier) {
go(function () use ($barrier) { // @phpstan-ignore closure.unusedUse
sleep(2);
});
go(function () use ($barrier) {
go(function () use ($barrier) { // @phpstan-ignore closure.unusedUse
sleep(3);
});

Expand Down
14 changes: 8 additions & 6 deletions examples/csp/context.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ public function __destruct()
};

// The Context object of a coroutine works the same as an \ArrayObject object.
Coroutine::getContext()['co1_obj1'] = new $class('co1_obj1');
Coroutine::getContext(Coroutine::getCid())->co1_obj2 = new $class('co1_obj2'); // Coroutine::getCid() returns 1.
Coroutine::getContext()['co1_obj1'] = new $class('co1_obj1'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 1.
Coroutine::getContext($cid)->co1_obj2 = new $class('co1_obj2'); // @phpstan-ignore property.nonObject

Coroutine::create(function () use ($class) {
// The Context object of a coroutine works the same as an \ArrayObject object.
Coroutine::getContext()['co2_obj1'] = new $class('co2_obj1');
Coroutine::getContext(Coroutine::getCid())->co2_obj2 = new $class('co2_obj2'); // Coroutine::getCid() returns 2.
Coroutine::getContext()['co2_obj1'] = new $class('co2_obj1'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 2.
Coroutine::getContext($cid)->co2_obj2 = new $class('co2_obj2'); // @phpstan-ignore property.nonObject

echo 'Name of the 1st object in the Context object of coroutine #1: ', Coroutine::getContext(1)['co1_obj1']->name, PHP_EOL;
echo 'Name of the 2nd object in the Context object of coroutine #2: ', Coroutine::getContext(2)->co2_obj2->name, PHP_EOL;
echo 'Name of the 1st object in the Context object of coroutine #1: ', Coroutine::getContext(1)['co1_obj1']->name, PHP_EOL; // @phpstan-ignore-line
echo 'Name of the 2nd object in the Context object of coroutine #2: ', Coroutine::getContext(2)->co2_obj2->name, PHP_EOL; // @phpstan-ignore property.nonObject

echo PHP_EOL, 'Coroutine #2 is exiting. The context object of it will be destroyed.', PHP_EOL;
});
Expand Down
2 changes: 1 addition & 1 deletion examples/csp/coroutines/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
if (($i % 100_000) === 0) {
printf(
'%07d active coroutines; total time: %f seconds; memory usage: %d.' . PHP_EOL,
count(Coroutine::listCoroutines()),
count(Coroutine::listCoroutines()), // @phpstan-ignore argument.type
microtime(true) - $startTime,
memory_get_usage()
);
Expand Down
2 changes: 1 addition & 1 deletion examples/csp/coroutines/exit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
run(function () {
try {
exit(911);
} catch (Swoole\ExitException $e) {
} catch (Swoole\ExitException $e) { // @phpstan-ignore catch.neverThrown
echo <<<EOT
Calling exit() inside a coroutine throws out a \\Swoole\\ExitException exception instead of terminating code execution
directly.
Expand Down
2 changes: 1 addition & 1 deletion examples/csp/coroutines/for.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
}

// Note that there are 2,001 coroutines created, including the main coroutine created by function call run().
echo count(Coroutine::listCoroutines()), ' active coroutines when reaching the end of the PHP script.', PHP_EOL;
echo count(Coroutine::listCoroutines()), ' active coroutines when reaching the end of the PHP script.', PHP_EOL; // @phpstan-ignore argument.type
});
2 changes: 1 addition & 1 deletion examples/csp/deadlocks/coroutine-yielded-3.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Coroutine::set(
[
Constant::OPTION_EXIT_CONDITION => function () {
return Coroutine::stats()['coroutine_num'] === 0;
return Coroutine::stats()['coroutine_num'] === 0; // @phpstan-ignore offsetAccess.nonOffsetAccessible
},
]
);
Expand Down
8 changes: 8 additions & 0 deletions examples/csp/deadlocks/file-locking.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@
$filename = sys_get_temp_dir() . '/swoole-file-locking-' . uniqid() . '.tmp';

$fp1 = fopen($filename, 'w');
if ($fp1 === false) {
throw new RuntimeException('Failed to open file.');
}

flock($fp1, LOCK_EX); // To acquire an exclusive lock (writer).

echo '2', PHP_EOL; // This will be printed out.

$fp2 = fopen($filename, 'w');
if ($fp2 === false) {
throw new RuntimeException('Failed to open file.');
}

flock($fp2, LOCK_EX); // Trying to acquire an exclusive lock (writer) again on the same file.

// Whatever code you put here (within the coroutine) will never be executed.
Expand Down
2 changes: 1 addition & 1 deletion examples/csp/scheduling/non-preemptive.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
function () {
go(function () {
$i = 0;
while (true) {
while (true) { // @phpstan-ignore while.alwaysTrue
echo $i++, PHP_EOL;
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/csp/scheduling/preemptive.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
function () {
go(function () {
$i = 0;
while (true) {
while (true) { // @phpstan-ignore while.alwaysTrue
echo $i++, PHP_EOL;
}
});
Expand Down
5 changes: 5 additions & 0 deletions examples/hooks/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
go(function () {
$mysqli = new mysqli('mysql', 'username', 'password', 'test');
$stmt = $mysqli->prepare('SELECT SLEEP(3)');
if ($stmt === false) {
echo 'Failed to prepare the statement.', PHP_EOL;
return;
}

$stmt->execute();
$stmt->close();
$mysqli->close();
Expand Down
4 changes: 2 additions & 2 deletions examples/io/blocking-vs-non-blocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

use function Swoole\Coroutine\go;

function blocking()
function blocking(): string
{
go(function () {
echo '1';
Expand All @@ -33,7 +33,7 @@ function blocking()
return '3';
}

function nonBlocking()
function nonBlocking(): string
{
go(function () {
echo '4';
Expand Down
10 changes: 7 additions & 3 deletions examples/pool/process-pool/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
for ($i = 0; $i < 3; $i++) {
// On the server side, you will see output messages like the following:
// Process #0 received message "s:35:"Message #0 via class Swoole\MsgQueue!";". (MSGQUEUE)
$mq->push(sprintf('Message #%d via class %s!', $i, MsgQueue::class)); // @phpstan-ignore class.notFound
$mq->push(sprintf('Message #%d via class %s!', $i, MsgQueue::class)); // @phpstan-ignore class.notFound,class.notFound
}
});
}
Expand All @@ -45,6 +45,10 @@
if (function_exists('msg_get_queue')) {
go(function () {
$mq = msg_get_queue(0x7000001);
if ($mq === false) {
echo 'Failed to get message queue.', PHP_EOL;
return;
}
for ($i = 0; $i < 3; $i++) {
// On the server side, you will see output messages like the following:
// Process #0 received message "s:35:"Message #0 via function msg_send()!";". (MSGQUEUE)
Expand All @@ -61,7 +65,7 @@
$requestMessage = 'TCP socket';
$client->send(pack('N', strlen($requestMessage)) . $requestMessage);
$responseMessage = $client->recv();
$responseMessage = substr($responseMessage, 4, unpack('N', substr($responseMessage, 0, 4))[1]);
$responseMessage = substr($responseMessage, 4, unpack('N', substr($responseMessage, 0, 4))[1]); // @phpstan-ignore-line
echo $responseMessage, PHP_EOL;
$client->close();
});
Expand All @@ -75,7 +79,7 @@
$requestMessage = 'Unix socket';
$client->send(pack('N', strlen($requestMessage)) . $requestMessage);
$responseMessage = $client->recv();
$responseMessage = substr($responseMessage, 4, unpack('N', substr($responseMessage, 0, 4))[1]);
$responseMessage = substr($responseMessage, 4, unpack('N', substr($responseMessage, 0, 4))[1]); // @phpstan-ignore-line
echo $responseMessage, PHP_EOL;
$client->close();
});
Expand Down
4 changes: 2 additions & 2 deletions examples/pool/process-pool/pool-standalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

$pool->on('workerStart', function (Pool $pool, int $workerId) use ($counter) {
# For standalone process pool, business logic should be implemented inside this "workerStart" callback.
echo "Process #{$workerId} (process ID in the OS: {$pool->getProcess()->pid}) started.", PHP_EOL;
echo "Process #{$workerId} (process ID in the OS: {$pool->getProcess()->pid}) started.", PHP_EOL; // @phpstan-ignore property.nonObject
$counter->add(1);
});
$pool->on('workerStop', function (Pool $pool, int $workerId) use ($counter) {
echo "Process #{$workerId} (process ID in the OS: {$pool->getProcess()->pid}) stopped.", PHP_EOL;
echo "Process #{$workerId} (process ID in the OS: {$pool->getProcess()->pid}) stopped.", PHP_EOL; // @phpstan-ignore property.nonObject
if ($counter->get() >= 3) {
$pool->shutdown();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/http1-integrated.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function (Server $server, int $workerId) {
if ($workerId === 0) {
// Here we start the second cron job that runs every 63 seconds.
Coroutine::create(function () {
while (true) {
while (true) { // @phpstan-ignore while.alwaysTrue
Coroutine::sleep(63);
echo '[HTTP1-ADVANCED]: This message is printed out every 63 seconds. (', date('H:i:s'), ')', PHP_EOL;
}
Expand Down
6 changes: 4 additions & 2 deletions examples/servers/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

use Swoole\Redis\Server;

$server = new Server('0.0.0.0', 6379);
$server->data = []; // We use an array as the data storage for the Redis server.
$server = new Server('0.0.0.0', 6379);

// We use an array as the data storage for the Redis server.
$server->data = []; // @phpstan-ignore property.notFound

$server->setHandler('SET', function (int $fd, array $data) use ($server) {
$server->data[$data[0]] = $data[1];
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/server-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
use Swoole\Http\Server;
use Swoole\Timer;

function printMessage(string $message, bool $newLine = false)
function printMessage(string $message, bool $newLine = false): void
{
echo ($newLine ? PHP_EOL : ''), 'INFO (', date('H:i:s'), "): {$message}", PHP_EOL;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/websocket-integrated.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function () {

$process = new Process(
function () {
while (true) {
while (true) { // @phpstan-ignore while.alwaysTrue
sleep(31);
echo 'Cron executed (in file ', __FILE__, ').', PHP_EOL; // To simulate cron executions.
}
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 0
level: 9
paths:
- ./examples

0 comments on commit 9ba6084

Please sign in to comment.