Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
extensions: sockets, openssl
coverage: none
tools: composer:v2
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3']
php: ['8.2', '8.3', '8.4', '8.5']
steps:
- name: Checkout
uses: actions/checkout@v7
Expand Down
50 changes: 29 additions & 21 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,44 @@

declare(strict_types=1);

/*
* Shared php-cs-fixer configuration for every InitPHP package (ADR-005).
*
* This file is identical in all repositories as `.php-cs-fixer.dist.php`.
* The only package-specific part is the two lines below; scripts/check-cs-config.sh
* ignores exactly those two lines when it compares a package with this template.
* Keep each of them on a single line.
*/
$excludeDirs = [];
$excludePaths = [];

$finder = PhpCsFixer\Finder::create()
->in([__DIR__ . '/src', __DIR__ . '/tests'])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
->name('*.php');

foreach ($excludeDirs as $excludeDir) {
$finder->exclude($excludeDir);
}

foreach ($excludePaths as $excludePath) {
$finder->notPath($excludePath);
}

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRiskyAllowed(false)
->setRules([
'@PSR12' => true,
'@PSR12:risky' => true,
'@PHP81Migration' => true,
'declare_strict_types' => true,
'native_function_invocation' => [
'include' => ['@compiler_optimized'],
'scope' => 'namespaced',
'strict' => true,
],
'@PER-CS' => true,
'@PHP8x2Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
'sort_algorithm' => 'alpha',
],
'single_quote' => true,
'array_syntax' => ['syntax' => 'short'],
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters']],
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'phpdoc_align' => ['align' => 'left'],
'phpdoc_order' => true,
'phpdoc_separation' => true,
'trailing_comma_in_multiline' => [
'elements' => ['arguments', 'array_destructuring', 'arrays', 'match', 'parameters'],
],
])
->setFinder($finder)
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache');
->setCacheFile(__DIR__ . '/build/php-cs-fixer.cache');
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning 2.0.0](https://semver.org/).

## [Unreleased]

### Changed (breaking)

- **Requires PHP `^8.2`.** Support for PHP 8.1 is dropped; CI now runs PHP 8.2, 8.3, 8.4 and 8.5.
- **Coding standard moved to PER-CS** through the shared `.php-cs-fixer.dist.php`; the whole code base was reformatted.

## [2.0.0] — TBD

### Highlights
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![CI](https://github.com/InitPHP/Socket/actions/workflows/ci.yml/badge.svg)](https://github.com/InitPHP/Socket/actions/workflows/ci.yml)
[![Designed & Maintained with Tan](https://muhammetsafak.com/badges/designed-maintained-with-tan.svg)](https://muhammetsafak.com/tan/)

A lightweight TCP, UDP, TLS and SSL socket toolkit for PHP 8.1+. Both server
A lightweight TCP, UDP, TLS and SSL socket toolkit for PHP 8.2+. Both server
and client sides share a clean, typed API built around enums and a small
`Channel` strategy so each transport plugs in without `switch` ladders.

Expand All @@ -25,7 +25,7 @@ $server->live(function ($srv, $conn) {

## Requirements

- PHP **8.1+**
- PHP **8.2+**
- ext-sockets
- ext-openssl (TLS / SSL)
- ext-pcntl (only for the integration test suite)
Expand Down Expand Up @@ -258,7 +258,7 @@ composer cs-fix # Apply style fixes
composer qa # All of the above
```

CI runs the full QA pipeline on PHP 8.1, 8.2 and 8.3.
CI runs the full QA pipeline on PHP 8.2, 8.3, 8.4 and 8.5.

## Contributing

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
"docs": "https://github.com/InitPHP/Socket/tree/main/docs"
},
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-sockets": "*",
"ext-openssl": "*"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"phpstan/phpstan": "^1.11 || ^2.0",
"friendsofphp/php-cs-fixer": "^3.59"
"friendsofphp/php-cs-fixer": "^3.88"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Enum/CryptoMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function fromName(string $name): self
throw new SocketInvalidArgumentException(\sprintf(
'Unsupported crypto method "%s". Expected one of: %s.',
$name,
implode(', ', array_map(static fn (self $c): string => $c->value, self::cases())),
implode(', ', array_map(static fn(self $c): string => $c->value, self::cases())),
));
}

Expand Down
4 changes: 1 addition & 3 deletions src/Exception/SocketConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace InitPHP\Socket\Exception;

class SocketConnectionException extends SocketException
{
}
class SocketConnectionException extends SocketException {}
4 changes: 1 addition & 3 deletions src/Exception/SocketException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use RuntimeException;

class SocketException extends RuntimeException implements SocketExceptionInterface
{
}
class SocketException extends RuntimeException implements SocketExceptionInterface {}
4 changes: 1 addition & 3 deletions src/Exception/SocketExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use Throwable;

interface SocketExceptionInterface extends Throwable
{
}
interface SocketExceptionInterface extends Throwable {}
4 changes: 1 addition & 3 deletions src/Exception/SocketInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use InvalidArgumentException;

class SocketInvalidArgumentException extends InvalidArgumentException implements SocketExceptionInterface
{
}
class SocketInvalidArgumentException extends InvalidArgumentException implements SocketExceptionInterface {}
4 changes: 1 addition & 3 deletions src/Exception/SocketListenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace InitPHP\Socket\Exception;

class SocketListenException extends SocketException
{
}
class SocketListenException extends SocketException {}
4 changes: 1 addition & 3 deletions src/Server/ServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ final class ServerConnection implements SocketConnectionInterface
{
private int|string|null $id = null;

public function __construct(private readonly ChannelInterface $channel)
{
}
public function __construct(private readonly ChannelInterface $channel) {}

public function setId(int|string $id): static
{
Expand Down
4 changes: 1 addition & 3 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
*/
final class Socket
{
private function __construct()
{
}
private function __construct() {}

/**
* Create a server bound to $host:$port.
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function selfSignedCertPath(string $commonName = 'localhost'): string

$path = tempnam(sys_get_temp_dir(), 'initphp-socket-tls-') . '.pem';
file_put_contents($path, $certPem . $keyPem);
$this->registerCleanup(static fn (): bool => @unlink($path));
$this->registerCleanup(static fn(): bool => @unlink($path));

return $path;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/ServerLifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testTickBeforeListenThrows(): void
$server = new TcpServer('127.0.0.1', 9000);

$this->expectException(SocketException::class);
$server->tick(static fn () => null, 0.0);
$server->tick(static fn() => null, 0.0);
}

public function testStopFromInsideCallbackExitsLiveLoop(): void
Expand All @@ -71,7 +71,7 @@ public function testStopFromInsideCallbackExitsLiveLoop(): void

// Bring the client into the server's accept queue, then feed a byte
// so the next live() iteration actually fires the callback.
$server->tick(static fn () => null, 0.2);
$server->tick(static fn() => null, 0.2);
self::assertCount(1, $server->getClients());
self::assertSame(4, $client->write('stop'));

Expand Down
8 changes: 4 additions & 4 deletions tests/Integration/StreamClientIoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testConnectReadWriteAgainstPlainTcpServer(): void
$errStr = '';
$server = stream_socket_server("tcp://127.0.0.1:{$port}", $errNo, $errStr);
self::assertNotFalse($server, "stream_socket_server failed: {$errStr}");
$this->registerCleanup(static fn () => @fclose($server));
$this->registerCleanup(static fn() => @fclose($server));

$client = new PlainStreamClient('127.0.0.1', $port, 2.0);
$client->option('verify_peer', false)
Expand All @@ -35,7 +35,7 @@ public function testConnectReadWriteAgainstPlainTcpServer(): void

$peer = stream_socket_accept($server, 1.0);
self::assertNotFalse($peer);
$this->registerCleanup(static fn () => @fclose($peer));
$this->registerCleanup(static fn() => @fclose($peer));

self::assertSame(5, $client->write('hello'));
// Allow the kernel to deliver.
Expand Down Expand Up @@ -64,7 +64,7 @@ public function testCryptoCanBeDisabledOnPlainStream(): void
$errStr = '';
$server = stream_socket_server("tcp://127.0.0.1:{$port}", $errNo, $errStr);
self::assertNotFalse($server);
$this->registerCleanup(static fn () => @fclose($server));
$this->registerCleanup(static fn() => @fclose($server));

$client = new PlainStreamClient('127.0.0.1', $port);
$client->connect();
Expand All @@ -81,7 +81,7 @@ public function testConnectTwiceThrows(): void
$errStr = '';
$server = stream_socket_server("tcp://127.0.0.1:{$port}", $errNo, $errStr);
self::assertNotFalse($server);
$this->registerCleanup(static fn () => @fclose($server));
$this->registerCleanup(static fn() => @fclose($server));

$client = new PlainStreamClient('127.0.0.1', $port);
$client->connect();
Expand Down
8 changes: 4 additions & 4 deletions tests/Integration/TcpDisconnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testServerEvictsClientAfterPeerDisconnects(): void
$client->connect();

// Accept the client.
$server->tick(static fn () => null, 0.2);
$server->tick(static fn() => null, 0.2);
self::assertCount(1, $server->getClients());

// Client drops the connection.
Expand All @@ -30,7 +30,7 @@ public function testServerEvictsClientAfterPeerDisconnects(): void
// We may need a couple of iterations for the kernel to surface the close.
$eviction = false;
for ($i = 0; $i < 20 && !$eviction; ++$i) {
$server->tick(static fn () => null, 0.05);
$server->tick(static fn() => null, 0.05);
if (\count($server->getClients()) === 0) {
$eviction = true;
}
Expand All @@ -54,8 +54,8 @@ public function testBroadcastSkipsDeadClient(): void
$deadOnArrival->disconnect();

// Accept both.
$server->tick(static fn () => null, 0.2);
$server->tick(static fn () => null, 0.2);
$server->tick(static fn() => null, 0.2);
$server->tick(static fn() => null, 0.2);
self::assertCount(2, $server->getClients());

// Broadcast — the dead client write call should silently no-op and
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/TcpEchoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testServerAcceptsClientAndEchoesData(): void
$this->registerCleanup($client->disconnect(...));

// First tick: pick up the new connection from the listen backlog.
$server->tick(static fn () => null, 0.5);
$server->tick(static fn() => null, 0.5);
self::assertCount(1, $server->getClients());

// Client → server payload.
Expand Down Expand Up @@ -69,8 +69,8 @@ public function testBroadcastReachesEveryConnectedClient(): void
$this->registerCleanup($clientB->disconnect(...));

// Accept both pending connections.
$server->tick(static fn () => null, 0.2);
$server->tick(static fn () => null, 0.2);
$server->tick(static fn() => null, 0.2);
$server->tick(static fn() => null, 0.2);
self::assertCount(2, $server->getClients());

self::assertTrue($server->broadcast('beacon'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/TcpServerCloseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testCloseTearsDownEveryActiveClient(): void
}
// Accept all of them.
for ($i = 0; $i < 3; ++$i) {
$server->tick(static fn () => null, 0.1);
$server->tick(static fn() => null, 0.1);
}
self::assertCount(3, $server->getClients());

Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/UdpServerCloseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function testCloseTearsDownEveryConnection(): void
$this->registerCleanup($client->disconnect(...));

$client->write('greet');
$server->tick(static function () {
}, 0.3);
$server->tick(static function () {}, 0.3);
self::assertCount(1, $server->getClients());

self::assertTrue($server->close());
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Server/AbstractServerBroadcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function writesOf(ChannelInterface $channel): array
{
\assert($channel instanceof FakeChannel);

return array_map(static fn (array $call): string => $call[0], $channel->writeCalls);
return array_map(static fn(array $call): string => $call[0], $channel->writeCalls);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Server/AbstractStreamServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testTickBeforeListenThrows(): void
{
$server = new TlsServer('127.0.0.1', 9443);
$this->expectException(SocketException::class);
$server->tick(static fn () => null, 0.0);
$server->tick(static fn() => null, 0.0);
}

}
2 changes: 1 addition & 1 deletion tests/Unit/Server/UdpServerOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testTickBeforeListenThrows(): void
{
$server = new UdpServer('127.0.0.1', 9000);
$this->expectException(SocketException::class);
$server->tick(static fn () => null, 0.0);
$server->tick(static fn() => null, 0.0);
}

public function testGetHostAndPortReturnConfiguredValues(): void
Expand Down