diff --git a/examples/clients/postgresql.php b/examples/clients/postgresql.php index 62a50db..a8572e2 100755 --- a/examples/clients/postgresql.php +++ b/examples/clients/postgresql.php @@ -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: diff --git a/examples/clients/tcp.php b/examples/clients/tcp.php index dd564a0..6ca90bb 100755 --- a/examples/clients/tcp.php +++ b/examples/clients/tcp.php @@ -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(); }); } diff --git a/examples/clients/udp.php b/examples/clients/udp.php index cddfae3..2b053ab 100755 --- a/examples/clients/udp.php +++ b/examples/clients/udp.php @@ -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(); }); diff --git a/examples/clients/websocket.php b/examples/clients/websocket.php index 220b8e4..9e6f5ec 100755 --- a/examples/clients/websocket.php +++ b/examples/clients/websocket.php @@ -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(); }); }); diff --git a/examples/csp/barrier.php b/examples/csp/barrier.php index 4de80cd..d26a1d9 100755 --- a/examples/csp/barrier.php +++ b/examples/csp/barrier.php @@ -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); }); diff --git a/examples/csp/context.php b/examples/csp/context.php index 40b3ff5..4e09163 100755 --- a/examples/csp/context.php +++ b/examples/csp/context.php @@ -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; }); diff --git a/examples/csp/coroutines/benchmark.php b/examples/csp/coroutines/benchmark.php index 0e7431d..9f5483e 100755 --- a/examples/csp/coroutines/benchmark.php +++ b/examples/csp/coroutines/benchmark.php @@ -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() ); diff --git a/examples/csp/coroutines/exit.php b/examples/csp/coroutines/exit.php index 7907dc0..56f60d7 100755 --- a/examples/csp/coroutines/exit.php +++ b/examples/csp/coroutines/exit.php @@ -19,7 +19,7 @@ run(function () { try { exit(911); - } catch (Swoole\ExitException $e) { + } catch (Swoole\ExitException $e) { // @phpstan-ignore catch.neverThrown echo << function () { - return Coroutine::stats()['coroutine_num'] === 0; + return Coroutine::stats()['coroutine_num'] === 0; // @phpstan-ignore offsetAccess.nonOffsetAccessible }, ] ); diff --git a/examples/csp/deadlocks/file-locking.php b/examples/csp/deadlocks/file-locking.php index e4a7421..68724d0 100755 --- a/examples/csp/deadlocks/file-locking.php +++ b/examples/csp/deadlocks/file-locking.php @@ -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. diff --git a/examples/csp/scheduling/non-preemptive.php b/examples/csp/scheduling/non-preemptive.php index c864378..1277338 100755 --- a/examples/csp/scheduling/non-preemptive.php +++ b/examples/csp/scheduling/non-preemptive.php @@ -18,7 +18,7 @@ function () { go(function () { $i = 0; - while (true) { + while (true) { // @phpstan-ignore while.alwaysTrue echo $i++, PHP_EOL; } }); diff --git a/examples/csp/scheduling/preemptive.php b/examples/csp/scheduling/preemptive.php index 17afc04..c4913e8 100755 --- a/examples/csp/scheduling/preemptive.php +++ b/examples/csp/scheduling/preemptive.php @@ -20,7 +20,7 @@ function () { go(function () { $i = 0; - while (true) { + while (true) { // @phpstan-ignore while.alwaysTrue echo $i++, PHP_EOL; } }); diff --git a/examples/hooks/mysqli.php b/examples/hooks/mysqli.php index 1887716..42246e5 100755 --- a/examples/hooks/mysqli.php +++ b/examples/hooks/mysqli.php @@ -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(); diff --git a/examples/io/blocking-vs-non-blocking.php b/examples/io/blocking-vs-non-blocking.php index 4febd17..3feaf5a 100755 --- a/examples/io/blocking-vs-non-blocking.php +++ b/examples/io/blocking-vs-non-blocking.php @@ -23,7 +23,7 @@ use function Swoole\Coroutine\go; -function blocking() +function blocking(): string { go(function () { echo '1'; @@ -33,7 +33,7 @@ function blocking() return '3'; } -function nonBlocking() +function nonBlocking(): string { go(function () { echo '4'; diff --git a/examples/pool/process-pool/client.php b/examples/pool/process-pool/client.php index c794042..1810381 100755 --- a/examples/pool/process-pool/client.php +++ b/examples/pool/process-pool/client.php @@ -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 } }); } @@ -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) @@ -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(); }); @@ -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(); }); diff --git a/examples/pool/process-pool/pool-standalone.php b/examples/pool/process-pool/pool-standalone.php index f1367d3..f109e46 100755 --- a/examples/pool/process-pool/pool-standalone.php +++ b/examples/pool/process-pool/pool-standalone.php @@ -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(); } diff --git a/examples/servers/http1-integrated.php b/examples/servers/http1-integrated.php index 1f02572..7cec1f0 100755 --- a/examples/servers/http1-integrated.php +++ b/examples/servers/http1-integrated.php @@ -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; } diff --git a/examples/servers/redis.php b/examples/servers/redis.php index 856afad..0593789 100755 --- a/examples/servers/redis.php +++ b/examples/servers/redis.php @@ -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]; diff --git a/examples/servers/server-events.php b/examples/servers/server-events.php index 9737ca8..79d4c9a 100755 --- a/examples/servers/server-events.php +++ b/examples/servers/server-events.php @@ -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; } diff --git a/examples/servers/websocket-integrated.php b/examples/servers/websocket-integrated.php index 1debd22..01d64f9 100755 --- a/examples/servers/websocket-integrated.php +++ b/examples/servers/websocket-integrated.php @@ -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. } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 3de2006..cb92717 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,4 +1,4 @@ parameters: - level: 0 + level: 9 paths: - ./examples