diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed5d6b1..e6b0c78 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index b908d68..396d643 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -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'); diff --git a/CHANGELOG.md b/CHANGELOG.md index f619509..ad33cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 3a98a47..38ca5b3 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) @@ -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 diff --git a/composer.json b/composer.json index c484620..73311aa 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Enum/CryptoMethod.php b/src/Enum/CryptoMethod.php index 343f7a6..96ded19 100644 --- a/src/Enum/CryptoMethod.php +++ b/src/Enum/CryptoMethod.php @@ -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())), )); } diff --git a/src/Exception/SocketConnectionException.php b/src/Exception/SocketConnectionException.php index 9a9cd7d..c55c446 100644 --- a/src/Exception/SocketConnectionException.php +++ b/src/Exception/SocketConnectionException.php @@ -4,6 +4,4 @@ namespace InitPHP\Socket\Exception; -class SocketConnectionException extends SocketException -{ -} +class SocketConnectionException extends SocketException {} diff --git a/src/Exception/SocketException.php b/src/Exception/SocketException.php index 3383f3d..afe6e7e 100644 --- a/src/Exception/SocketException.php +++ b/src/Exception/SocketException.php @@ -6,6 +6,4 @@ use RuntimeException; -class SocketException extends RuntimeException implements SocketExceptionInterface -{ -} +class SocketException extends RuntimeException implements SocketExceptionInterface {} diff --git a/src/Exception/SocketExceptionInterface.php b/src/Exception/SocketExceptionInterface.php index 18237e5..11a6ab3 100644 --- a/src/Exception/SocketExceptionInterface.php +++ b/src/Exception/SocketExceptionInterface.php @@ -6,6 +6,4 @@ use Throwable; -interface SocketExceptionInterface extends Throwable -{ -} +interface SocketExceptionInterface extends Throwable {} diff --git a/src/Exception/SocketInvalidArgumentException.php b/src/Exception/SocketInvalidArgumentException.php index d1d75e8..13fa4e0 100644 --- a/src/Exception/SocketInvalidArgumentException.php +++ b/src/Exception/SocketInvalidArgumentException.php @@ -6,6 +6,4 @@ use InvalidArgumentException; -class SocketInvalidArgumentException extends InvalidArgumentException implements SocketExceptionInterface -{ -} +class SocketInvalidArgumentException extends InvalidArgumentException implements SocketExceptionInterface {} diff --git a/src/Exception/SocketListenException.php b/src/Exception/SocketListenException.php index 51a988e..c284381 100644 --- a/src/Exception/SocketListenException.php +++ b/src/Exception/SocketListenException.php @@ -4,6 +4,4 @@ namespace InitPHP\Socket\Exception; -class SocketListenException extends SocketException -{ -} +class SocketListenException extends SocketException {} diff --git a/src/Server/ServerConnection.php b/src/Server/ServerConnection.php index 9e8d3e6..168542c 100644 --- a/src/Server/ServerConnection.php +++ b/src/Server/ServerConnection.php @@ -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 { diff --git a/src/Socket.php b/src/Socket.php index da0c6f0..0abd421 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -26,9 +26,7 @@ */ final class Socket { - private function __construct() - { - } + private function __construct() {} /** * Create a server bound to $host:$port. diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index a26955f..4d5ae91 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -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; } diff --git a/tests/Integration/ServerLifecycleTest.php b/tests/Integration/ServerLifecycleTest.php index 7feac41..9a49c43 100644 --- a/tests/Integration/ServerLifecycleTest.php +++ b/tests/Integration/ServerLifecycleTest.php @@ -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 @@ -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')); diff --git a/tests/Integration/StreamClientIoTest.php b/tests/Integration/StreamClientIoTest.php index 0a0e6f0..b1fac22 100644 --- a/tests/Integration/StreamClientIoTest.php +++ b/tests/Integration/StreamClientIoTest.php @@ -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) @@ -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. @@ -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(); @@ -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(); diff --git a/tests/Integration/TcpDisconnectTest.php b/tests/Integration/TcpDisconnectTest.php index 62a1190..abe9557 100644 --- a/tests/Integration/TcpDisconnectTest.php +++ b/tests/Integration/TcpDisconnectTest.php @@ -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. @@ -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; } @@ -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 diff --git a/tests/Integration/TcpEchoTest.php b/tests/Integration/TcpEchoTest.php index d5b9ee0..d1545de 100644 --- a/tests/Integration/TcpEchoTest.php +++ b/tests/Integration/TcpEchoTest.php @@ -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. @@ -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')); diff --git a/tests/Integration/TcpServerCloseTest.php b/tests/Integration/TcpServerCloseTest.php index 523fe59..849d011 100644 --- a/tests/Integration/TcpServerCloseTest.php +++ b/tests/Integration/TcpServerCloseTest.php @@ -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()); diff --git a/tests/Integration/UdpServerCloseTest.php b/tests/Integration/UdpServerCloseTest.php index fc9b153..780bbe1 100644 --- a/tests/Integration/UdpServerCloseTest.php +++ b/tests/Integration/UdpServerCloseTest.php @@ -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()); diff --git a/tests/Unit/Server/AbstractServerBroadcastTest.php b/tests/Unit/Server/AbstractServerBroadcastTest.php index e131b2b..dd7d198 100644 --- a/tests/Unit/Server/AbstractServerBroadcastTest.php +++ b/tests/Unit/Server/AbstractServerBroadcastTest.php @@ -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); } } diff --git a/tests/Unit/Server/AbstractStreamServerTest.php b/tests/Unit/Server/AbstractStreamServerTest.php index 1992b46..2e8b415 100644 --- a/tests/Unit/Server/AbstractStreamServerTest.php +++ b/tests/Unit/Server/AbstractStreamServerTest.php @@ -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); } } diff --git a/tests/Unit/Server/UdpServerOptionsTest.php b/tests/Unit/Server/UdpServerOptionsTest.php index c3062c9..5a5f954 100644 --- a/tests/Unit/Server/UdpServerOptionsTest.php +++ b/tests/Unit/Server/UdpServerOptionsTest.php @@ -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