Skip to content

Commit

Permalink
Typed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jun 6, 2024
1 parent b78c80c commit 8d961b1
Show file tree
Hide file tree
Showing 32 changed files with 119 additions and 104 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

* Support `psr/log v3` (@sirn-se)
* Client `receive()` nevere return `null` (@sirn-se)
* Typed class properties (@sirn-se)

## `v2.2`

Expand Down
9 changes: 8 additions & 1 deletion docs/Migrate_2_3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[Documentation](Index.md) / Migration v2 -> v3

Version 3.x has few changes compared to previous version.

# Breaking changes

## setLogger
Expand All @@ -12,7 +14,8 @@ MiddlewareHandler->setLogger(LoggerInterface $logger): void

These methods now return `void` instead of `self.`
This means method return can not be chained.
The change make the comlient with `psr/log v3`.

The change make v3 complient with `psr/log v3`.

## receive

Expand All @@ -22,3 +25,7 @@ Client->receive(): Message

The method no longer has `Message|null` as return type.
It never returned `null` before, so only the method profile has changed.

# Extending

When extending classes in this repo, you might need to implement typed properties in child class.
25 changes: 13 additions & 12 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use InvalidArgumentException;
use Phrity\Net\{
StreamCollection,
StreamFactory,
Uri
};
Expand Down Expand Up @@ -52,20 +53,20 @@ class Client implements LoggerAwareInterface, Stringable
use StringableTrait;

// Settings
private $logger;
private $timeout = 60;
private $frameSize = 4096;
private $persistent = false;
private $context = [];
private $headers = [];
private LoggerInterface $logger;
private int $timeout = 60;
private int $frameSize = 4096;
private bool $persistent = false;
private array $context = [];
private array $headers = [];

// Internal resources
private $streamFactory;
private $socketUri;
private $connection;
private $middlewares = [];
private $streams;
private $running = false;
private StreamFactory $streamFactory;
private Uri $socketUri;
private Connection|null $connection = null;
private array $middlewares = [];
private StreamCollection|null $streams = null;
private bool $running = false;


/* ---------- Magic methods ------------------------------------------------------------------------------------ */
Expand Down
26 changes: 13 additions & 13 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ class Connection implements LoggerAwareInterface, Stringable
use SendMethodsTrait;
use StringableTrait;

private $stream;
private $httpHandler;
private $messageHandler;
private $middlewareHandler;
private $logger;
private $frameSize = 4096;
private $timeout = 60;
private $localName;
private $remoteName;
private $handshakeRequest;
private $handshakeResponse;
private $meta = [];
private $closed = false;
private SocketStream $stream;
private HttpHandler $httpHandler;
private MessageHandler $messageHandler;
private MiddlewareHandler $middlewareHandler;
private LoggerInterface $logger;
private int $frameSize = 4096;
private int $timeout = 60;
private string $localName;
private string $remoteName;
private Request|null $handshakeRequest = null;
private Response|null $handshakeResponse = null;
private array $meta = [];
private bool $closed = false;


/* ---------- Magic methods ------------------------------------------------------------------------------------ */
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/CloseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
class CloseException extends Exception
{
protected $status;
protected $content;
protected int|null $status;
protected string $content;

public function __construct(int|null $status = null, string $content = '')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/HandshakeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class HandshakeException extends Exception implements ConnectionLevelInterface
{
private $response;
private Response $response;

public function __construct(string $message, Response $response)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Frame/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Frame implements Stringable
{
use StringableTrait;

private $opcode;
private $payload;
private $final;
private string $opcode;
private string $payload;
private bool $final;

public function __construct(string $opcode, string $payload, bool $final)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Frame/FrameHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class FrameHandler implements LoggerAwareInterface, Stringable
use OpcodeTrait;
use StringableTrait;

private $stream;
private $logger;
private $pushMasked;
private $pullMaskedRequired;
private SocketStream $stream;
private LoggerInterface $logger;
private bool $pushMasked;
private bool $pullMaskedRequired;

public function __construct(SocketStream $stream, bool $pushMasked, bool $pullMaskedRequired)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Http/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class HttpHandler implements LoggerAwareInterface, Stringable
{
use StringableTrait;

private $stream;
private $ssl;
private $logger;
private SocketStream $stream;
private bool $ssl;
private LoggerInterface $logger;

public function __construct(SocketStream $stream, bool $ssl = false)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ abstract class Message implements MessageInterface, Stringable
{
use StringableTrait;

protected $version = '1.1';
protected $headers = [];
protected string $version = '1.1';
protected array $headers = [];

/**
* Retrieves the HTTP protocol version as a string.
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
class Request extends Message implements RequestInterface
{
private $target;
private $method;
private $uri;
private string $target = '';
private string $method;
private Uri $uri;

public function __construct(string $method = 'GET', UriInterface|string|null $uri = null)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class Response extends Message implements ResponseInterface
{
private static $codes = [
private static array $codes = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
Expand Down Expand Up @@ -84,8 +84,8 @@ class Response extends Message implements ResponseInterface
511 => 'Network Authentication Required',
];

private $code;
private $reason;
private int $code;
private string $reason;

public function __construct(int $code = 200, string $reasonPhrase = '')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
class Binary extends Message
{
protected $opcode = 'binary';
protected string $opcode = 'binary';
}
4 changes: 2 additions & 2 deletions src/Message/Close.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
class Close extends Message
{
protected $opcode = 'close';
protected $status = null;
protected string $opcode = 'close';
protected int|null $status = null;

public function __construct(int|null $status = null, string $content = '')
{
Expand Down
6 changes: 3 additions & 3 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ abstract class Message implements Stringable
{
use StringableTrait;

protected $opcode;
protected $content;
protected $timestamp;
protected string $opcode;
protected string $content;
protected DateTimeInterface $timestamp;

public function __construct(string $content = '')
{
Expand Down
8 changes: 4 additions & 4 deletions src/Message/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class MessageHandler implements LoggerAwareInterface, Stringable
{
use StringableTrait;

private const DEFAULT_SIZE = 4096;
private const int DEFAULT_SIZE = 4096;

private $frameHandler;
private $logger;
private $readBuffer;
private FrameHandler $frameHandler;
private LoggerInterface $logger;
private array|null $readBuffer = null;

public function __construct(FrameHandler $frameHandler)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
class Ping extends Message
{
protected $opcode = 'ping';
protected string $opcode = 'ping';
}
2 changes: 1 addition & 1 deletion src/Message/Pong.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
class Pong extends Message
{
protected $opcode = 'pong';
protected string $opcode = 'pong';
}
2 changes: 1 addition & 1 deletion src/Message/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
class Text extends Message
{
protected $opcode = 'text';
protected string $opcode = 'text';
}
10 changes: 5 additions & 5 deletions src/Middleware/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Callback implements
use LoggerAwareTrait;
use StringableTrait;

private $incoming;
private $outgoing;
private $httpIncoming;
private $httpOutgoing;
private $tick;
private Closure|null $incoming;
private Closure|null $outgoing;
private Closure|null $httpIncoming;
private Closure|null $httpOutgoing;
private Closure|null $tick;

public function __construct(
Closure|null $incoming = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/FollowRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class FollowRedirect implements LoggerAwareInterface, ProcessHttpIncomingInterfa
use LoggerAwareTrait;
use StringableTrait;

private $limit;
private $attempts = 1;
private int $limit;
private int $attempts = 1;

public function __construct(int $limit = 10)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Middleware/MiddlewareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class MiddlewareHandler implements LoggerAwareInterface, Stringable
use StringableTrait;

// Processor collections
private $incoming = [];
private $outgoing = [];
private $httpIncoming = [];
private $httpOutgoing = [];
private $tick = [];
private array $incoming = [];
private array $outgoing = [];
private array $httpIncoming = [];
private array $httpOutgoing = [];
private array $tick = [];

// Handlers
private $httpHandler;
private $messageHandler;
private $logger;
private HttpHandler $httpHandler;
private MessageHandler $messageHandler;
private LoggerInterface $logger;

/**
* Create MiddlewareHandler.
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/PingInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PingInterval implements LoggerAwareInterface, ProcessOutgoingInterface, Pr
use LoggerAwareTrait;
use StringableTrait;

private $interval;
private int|null $interval;

public function __construct(int|null $interval = null)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Middleware/ProcessHttpStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class ProcessHttpStack implements Stringable
{
use StringableTrait;

private $connection;
private $httpHandler;
private $processors;
private Connection $connection;
private HttpHandler $httpHandler;
private array $processors;

/**
* Create ProcessStack.
Expand Down
6 changes: 3 additions & 3 deletions src/Middleware/ProcessStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class ProcessStack implements Stringable
{
use StringableTrait;

private $connection;
private $messageHandler;
private $processors;
private Connection $connection;
private MessageHandler $messageHandler;
private array $processors;

/**
* Create ProcessStack.
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/ProcessTickStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class ProcessTickStack implements Stringable
{
use StringableTrait;

private $connection;
private $processors;
private Connection $connection;
private array $processors;

/**
* Create ProcessStack.
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/SubprotocolNegotiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class SubprotocolNegotiation implements
use LoggerAwareTrait;
use StringableTrait;

private $subprotocols;
private $require;
private array $subprotocols;
private bool $require;

public function __construct(array $subprotocols, bool $require = false)
{
Expand Down
Loading

0 comments on commit 8d961b1

Please sign in to comment.